當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java Scanner remove()用法及代碼示例


當此 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() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。