当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。