Java Scanner remove() 方法



描述

此 Iterator 實現不支援 Java Scanner remove() 方法。

宣告

以下是 java.util.Scanner.remove() 方法的宣告

public void remove()

引數

返回值

此方法不返回值。

異常

UnsupportedOperationException − 如果呼叫此方法。

在字串示例上檢查 Scanner 的 Remove 方法

以下示例演示了 Java Scanner remove() 方法的使用,展示了此方法不受此物件支援。我們使用給定字串建立了一個 Scanner 物件。然後我們使用 nextLine() 方法列印了字串,並呼叫了 remove 方法,該方法丟擲異常,然後使用 close() 方法關閉了 Scanner。

package com.tutorialspoint;

import java.util.Scanner;

public class ScannerDemo {
   public static void main(String[] args) {

      String s = "Hello World! 3 + 3.0 = 6";

      // create a new scanner with the specified String Object
      Scanner scanner = new Scanner(s);

      // print the next line of the string
      System.out.println(scanner.nextLine());

      try {
         // invoking remove method will cause exception
         scanner.remove();		 
      } catch(UnsupportedOperationException e){
         System.out.println("Remove method is not supported.");
      }

      scanner.close();
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

Hello World! 3 + 3.0 = 6
Remove method is not supported.

在使用者輸入示例上檢查 Scanner 的 Remove 方法

以下示例演示了 Java Scanner remove() 方法的使用,展示了此方法不受此物件支援。我們使用 System.in 建立了一個 Scanner 物件。然後我們使用 nextLine() 方法列印了字串,並呼叫了 remove 方法,該方法丟擲異常,然後使用 close() 方法關閉了 Scanner。

package com.tutorialspoint;

import java.util.Scanner;

public class ScannerDemo {
   public static void main(String[] args) {

      // create a new scanner with the system input
      Scanner scanner = new Scanner(System.in);

      // print the next line of the string
      System.out.println(scanner.nextLine());

      try {
         // invoking remove method will cause exception
         scanner.remove();		 
      } catch(UnsupportedOperationException e){
         System.out.println("Remove method is not supported.");
      }

      scanner.close();
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果:(我們在其中輸入 Hello World 並按 Enter 鍵。)

Hello World
Hello World
Remove method is not supported.

在屬性檔案示例上檢查 Scanner 的 Remove 方法

以下示例演示了 Java Scanner remove() 方法的使用,展示了此方法不受此物件支援。我們使用檔案 properties.txt 建立了一個 Scanner 物件。然後我們使用 nextLine() 方法列印了字串,並呼叫了 remove 方法,該方法丟擲異常,然後使用 close() 方法關閉了 Scanner。

package com.tutorialspoint;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class ScannerDemo {
   public static void main(String[] args) throws FileNotFoundException {

      // create a new scanner with a file as input
      Scanner scanner = new Scanner(new File("properties.txt"));

      // print the next line of the string
      System.out.println(scanner.nextLine());

      try {
         // invoking remove method will cause exception
         scanner.remove();		 
      } catch(UnsupportedOperationException e){
         System.out.println("Remove method is not supported.");
      }

      scanner.close();
   }
}

假設我們在您的 CLASSPATH 中有一個名為 properties.txt 的檔案,其內容如下。此檔案將用作我們示例程式的輸入:

Hello World! 3 + 3.0 = 6

輸出

讓我們編譯並執行上述程式,這將產生以下結果。

Hello World! 3 + 3.0 = 6
Remove method is not supported.
java_util_scanner.htm
廣告