
- Java.util 包類
- Java.util - 首頁
- Java.util - ArrayDeque
- Java.util - ArrayList
- Java.util - Arrays
- Java.util - BitSet
- Java.util - Calendar
- Java.util - Collections
- Java.util - Currency
- Java.util - Date
- Java.util - Dictionary
- Java.util - EnumMap
- Java.util - EnumSet
- Java.util - Formatter
- Java.util - GregorianCalendar
- Java.util - HashMap
- Java.util - HashSet
- Java.util - Hashtable
- Java.util - IdentityHashMap
- Java.util - LinkedHashMap
- Java.util - LinkedHashSet
- Java.util - LinkedList
- Java.util - ListResourceBundle
- Java.util - Locale
- Java.util - Observable
- Java.util - PriorityQueue
- Java.util - Properties
- Java.util - PropertyPermission
- Java.util - PropertyResourceBundle
- Java.util - Random
- Java.util - ResourceBundle
- Java.util - ResourceBundle.Control
- Java.util - Scanner
- Java.util - ServiceLoader
- Java.util - SimpleTimeZone
- Java.util - Stack
- Java.util - StringTokenizer
- Java.util - Timer
- Java.util - TimerTask
- Java.util - TimeZone
- Java.util - TreeMap
- Java.util - TreeSet
- Java.util - UUID
- Java.util - Vector
- Java.util - WeakHashMap
- Java.util 包其他內容
- Java.util - 介面
- Java.util - 異常
- Java.util - 列舉
- Java.util 有用資源
- Java.util - 有用資源
- Java.util - 討論
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.