Java: Scanner umie ciąć tekst, nawet na inty itp

Niesamowite funkcje Scannera! Oprócz wczytywania tekstu wpisanego przez użytkownika i czytania plików, potrafi on znacznie więcej.

Oto cytat ze strony:

Parameter

Data TypeParameterDescriptionRequired/Optional
StringpatternIt is a string specifying the pattern to scan.Required
PatternpatternIt is the pattern to scan for specified string.Required

Returns

MethodReturns
hasNext()This method returns true if and only if this scanner has another token.
hasNext(String pattern)This method returns true if and only if this scanner has another token matching the specified pattern.
hasNext(Pattern pattern)This method returns true if and only if this scanner has another token matching the specified pattern.

Exceptions

IllegalStateException- This method throws exception if the innvocation is done after the scanner has been closed.

Compatibility Version

Java 1.5 and above

Example 1

  1. import java.util.*;  
  2. public class ScannerHasNextExample1 {    
  3.     public static void main(String args[]){       
  4.           //Create Scanner object  
  5.         Scanner scan = new Scanner("Hello World!");  
  6.         //Printing the delimiter used  
  7.         System.out.println("Delimiter:" + scan.delimiter());  
  8.         //Print the Strings  
  9.         while (scan.hasNext()) {  
  10.             System.out.println(scan.next());  
  11.         }  
  12.         //Close the scanner  
  13.         scan.close();  
  14.         }    
  15. }  
Output:
Delimiter:\p{javaWhitespace}+
Hello
World!

Example 2

  1. import java.util.*;  
  2. public class ScannerHasNextExample2 {    
  3.     public static void main(String args[]){       
  4.            String s = "Hello, This is JavaTpoint.";  
  5.          //Create scanner Object and pass string in it  
  6.          Scanner scan = new Scanner(s);  
  7.          //Check if the scanner has a token  
  8.          System.out.println("Result: " + scan.hasNext());  
  9.          //Print the string  
  10.          System.out.println("String: " +scan.nextLine());  
  11.          //Check if the scanner has a token after printing the line  
  12.          System.out.println("Final Result: " + scan.hasNext());  
  13.          //Close the scanner  
  14.          scan.close();  
  15.        }    
  16.     }  
Output:
Result: true
String: Hello, This is JavaTpoint.
Final Result: false

Example 3

  1. import java.util.*;  
  2. public class ScannerHasNextExample3 {    
  3.     public static void main(String args[]){       
  4.              //Create Scanner object  
  5.              Scanner scan = new Scanner("Program:Java;Python;Android");  
  6.              //Initialize the String pattern  
  7.              String pattern = "Program:.*";  
  8.              //Check if pattern satisfies the String content  
  9.              if(scan.hasNext(pattern)){  
  10.                System.out.println("Pattern found");  
  11.              }  
  12.              else{  
  13.                System.out.println("Pattern not found");  
  14.              }  
  15.              scan.close();  
  16.          }    
  17. }  
Output:
Pattern found

Example 4

  1. import java.util.*;  
  2. public class ScannerHasNextExample4 {    
  3.     public static void main(String args[]){       
  4.               String str = "JavaTpoint.com 15 + 15 = 18.0";  
  5.             Scanner scanner = new Scanner(str);  
  6.             //Checking scanner's next token matches "c"  
  7.             System.out.println("Result: "+scanner.hasNext("JavaTpoint.com"));  
  8.             //Checking scanner's next token matches "="  
  9.             System.out.println("Result: "+scanner.hasNext("="));  
  10.             //Print the rest of the string  
  11.             System.out.println("Rest of String: "+scanner.nextLine());  
  12.             scanner.close();  
  13.         }    
  14. }  
Output:
Result: true
Result: false
Rest of String: JavaTpoint.com 15 + 15 = 18.0

Example 5

  1. import java.util.*;  
  2. import java.util.regex.Pattern;  
  3. public class ScannerHasNextExample5 {    
  4.     public static void main(String args[]){       
  5.               //Create Scanner object  
  6.             Scanner scan = new Scanner("Names:Raju1;Pawan;Suresh");  
  7.             //Declare the delimiter  
  8.             scan.useDelimiter(";");  
  9.             /*Initialize the String pattern which signifies that the String 
  10.             token contains characters of the alphabet only*/  
  11.             Pattern pattern = Pattern.compile("[A-Za-z]*");  
  12.             while(scan.hasNext()){  
  13.                 //Check if the token consists of declared pattern  
  14.                 if(scan.hasNext(pattern)){  
  15.                     System.out.println(scan.next());  
  16.                 }  
  17.                 else  
  18.                     scan.next();  
  19.                 }  
  20.                 scan.close();  
  21.         }    
  22. }  
Output:
Pawan
Suresh

Komentarze

Popularne posty z tego bloga

IntelliJ: zmiana rozmiaru czcionki scrollem

ThunderBird: jak zrobić professional stopkę