當此 Iterator 實現不支持刪除操作時,使用 Java Scanner 類的 remove() 方法。
用法
以下是 remove() 方法的聲明:
public void remove()
參數
此方法不接受任何參數。
返回
remove() 方法不返回任何值。
異常
UnsupportedOperationException - 如果調用此方法,它將拋出異常。
兼容版本
Java 1.5 及以上
例子1
import java.util.Scanner;
public class ScannerRemoveExample1{
public static void main(String args[]){
String s = "Hi All! This is JavaTpoint.";
//Create a scanner with the specified Object
Scanner scanner = new Scanner(s);
System.out.println("" + scanner.nextLine());
//Call remove to get results
scanner.remove();
scanner.close();
}
}
輸出:
Hi All! This is JavaTpoint. Exception in thread "main" java.lang.UnsupportedOperationException at java.base/java.util.Scanner.remove(Scanner.java:1490) at myPackage.ScannerRemoveExample1.main(ScannerRemoveExample1.java:10)
例子2
import java.util.Scanner;
public class ScannerRemoveExample2 {
public static void main(String args[]){
System.out.print("Enter Your Name:");
//Create a scanner with the specified Object
Scanner scan = new Scanner(System.in);
String name = scan.next();
System.out.println("Name:"+name);
System.out.println("Exception Found. ");
//Remove method call results in an exception
scan.remove();
scan.close();
}
}
輸出:
Enter Your Name:Java Name:Java Exception Found. Exception in thread "main" java.lang.UnsupportedOperationException at java.base/java.util.Scanner.remove(Scanner.java:1490) at myPackage.ScannerRemoveExample2.main(ScannerRemoveExample2.java:12)
相關用法
- Java Scanner remove()用法及代碼示例
- Java Scanner reset()用法及代碼示例
- Java Scanner radix()用法及代碼示例
- Java Scanner locale()用法及代碼示例
- Java Scanner nextByte()用法及代碼示例
- Java Scanner toString()用法及代碼示例
- Java Scanner findWithinHorizon()用法及代碼示例
- Java Scanner useRadix()用法及代碼示例
- Java Scanner delimiter()用法及代碼示例
- Java Scanner nextLong()用法及代碼示例
- Java Scanner nextShort()用法及代碼示例
- Java Scanner nextBoolean()用法及代碼示例
- Java Scanner nextBigInteger()用法及代碼示例
- Java Scanner hasNext()用法及代碼示例
- Java Scanner skip()用法及代碼示例
- Java Scanner useDelimiter()用法及代碼示例
- Java Scanner findInLine()用法及代碼示例
- Java Scanner nextInt()用法及代碼示例
- Java Scanner hasNextFloat()用法及代碼示例
- Java Scanner next()用法及代碼示例
注:本文由純淨天空篩選整理自 Java Scanner remove() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。