List of usage examples for java.util Scanner hasNextInt
public boolean hasNextInt()
From source file:Main.java
public static void main(String[] args) { String s = "java2s.com 1 + 1 = 2.0 true "; Scanner scanner = new Scanner(s); while (scanner.hasNext()) { if (scanner.hasNextInt()) { System.out.println("Found :" + scanner.nextInt()); }/*w w w . j av a2 s.c o m*/ System.out.println("Not Found :" + scanner.next()); } scanner.close(); }
From source file:MainClass.java
public static void main(String args[]) throws IOException { // Write output to a file. FileWriter fout = new FileWriter("test.txt"); fout.write("int: 1 double 1.0 boolean true"); fout.close();//from w w w . j av a 2 s .com FileReader fin = new FileReader("Test.txt"); Scanner src = new Scanner(fin); while (src.hasNext()) { if (src.hasNextInt()) { System.out.println("int: " + src.nextInt()); } else if (src.hasNextDouble()) { System.out.println("double: " + src.nextDouble()); } else if (src.hasNextBoolean()) { System.out.println("boolean: " + src.nextBoolean()); } else { System.out.println(src.next()); } } fin.close(); }
From source file:ScanMixed.java
public static void main(String args[]) throws IOException { int i;//from w w w . j a va 2 s . com double d; boolean b; String str; FileWriter fout = new FileWriter("test.txt"); fout.write("Testing Scanner 10 12.2 one true two false"); fout.close(); FileReader fin = new FileReader("Test.txt"); Scanner src = new Scanner(fin); while (src.hasNext()) { if (src.hasNextInt()) { i = src.nextInt(); System.out.println("int: " + i); } else if (src.hasNextDouble()) { d = src.nextDouble(); System.out.println("double: " + d); } else if (src.hasNextBoolean()) { b = src.nextBoolean(); System.out.println("boolean: " + b); } else { str = src.next(); System.out.println("String: " + str); } } fin.close(); }
From source file:MainClass.java
public static void main(String args[]) throws IOException { int i;//from ww w .ja v a 2s . co m double d; boolean b; String str; FileWriter fout = new FileWriter("test.txt"); fout.write("string true false 1 2 3 4.12"); fout.close(); FileReader fin = new FileReader("Test.txt"); Scanner src = new Scanner(fin); while (src.hasNext()) { if (src.hasNextInt()) { i = src.nextInt(); System.out.println("int: " + i); } else if (src.hasNextDouble()) { d = src.nextDouble(); System.out.println("double: " + d); } else if (src.hasNextBoolean()) { b = src.nextBoolean(); System.out.println("boolean: " + b); } else { str = src.next(); System.out.println("String: " + str); } } fin.close(); }
From source file:MainClass.java
public static void main(String args[]) throws IOException { int i;// w ww. j a v a2s . c o m double d; boolean b; String str; FileWriter fout = new FileWriter("test.txt"); fout.write("Testing Scanner 10 12.2 one true two false"); fout.close(); FileReader fin = new FileReader("Test.txt"); Scanner src = new Scanner(fin); while (src.hasNext()) { if (src.hasNextInt()) { i = src.nextInt(); System.out.println("int: " + i); } else if (src.hasNextDouble()) { d = src.nextDouble(); System.out.println("double: " + d); } else if (src.hasNextBoolean()) { b = src.nextBoolean(); System.out.println("boolean: " + b); } else { str = src.next(); System.out.println("String: " + str); } } fin.close(); }
From source file:Main.java
public static void main(String[] args) { String s = "java2s.com 1 + 1 = 2.0 "; Scanner scanner = new Scanner(s); while (scanner.hasNext()) { // check if the scanner's next token is an int System.out.println(scanner.hasNextInt()); System.out.println(scanner.next()); }// w w w . j av a 2s . c om scanner.close(); }
From source file:Main.java
public static void main(String[] args) { String s = "java2s.com 1 + 1 = 2.0 true "; Scanner scanner = new Scanner(s); while (scanner.hasNext()) { // if the next is a int, print found and the int with radix 4 if (scanner.hasNextInt()) { System.out.println("Found :" + scanner.nextInt(4)); }//from w w w .j a v a 2s .c o m System.out.println("Not Found :" + scanner.next()); } scanner.close(); }
From source file:Main.java
public static int[] readNumsFromCommandLine() { Scanner s = new Scanner(System.in); int count = s.nextInt(); s.nextLine(); // throw away the newline. int[] numbers = new int[count]; Scanner numScanner = new Scanner(s.nextLine()); for (int i = 0; i < count; i++) { if (numScanner.hasNextInt()) { numbers[i] = numScanner.nextInt(); } else {//from w ww. j av a 2 s .c om System.out.println("You didn't provide enough numbers"); break; } } return numbers; }
From source file:Main.java
/** * Remove indexes from an xpath string.// w ww.j a v a2 s .co m * @param xpath xpath string * @return unindexed xpath string */ public static String removeIndexes(String xpath) { final String[] partialSteps = xpath.split("[/]"); if (partialSteps.length == 0) { return xpath; } int startIndex = 0; StringBuffer buf = new StringBuffer(); for (int i = startIndex; i < partialSteps.length; i++) { String step = partialSteps[i]; int start = step.indexOf('['); if (start > -1) { int end = step.indexOf(']'); Scanner scanner = new Scanner(step.substring(start + 1, end)); if (scanner.hasNextInt()) { // remove index and the brackets step = step.substring(0, start); } } buf.append(step); if (i < partialSteps.length - 1) { buf.append("/"); } } return buf.toString(); }
From source file:Main.java
private static int getChoice() { Scanner input = new Scanner(System.in); int choice = 1; //default value to force skip of first if statement check do {//from ww w . j a v a2 s. c o m if (choice < 1 | choice > 4) System.out.println("Invalid choice..."); System.out.print("Choice: "); /*This while loop checks the buffer stream for the next incoming input. *the purpose is to check if the input can be assigned an int value. *at this time choice has not be assigned*/ while (!input.hasNextInt()) { // ask for an input, if input NOT nextInt then: input.next(); // consumes the token and returns to top of loop for another token System.out.print("Try again: "); //don't want println as that would shift cursor to new line } choice = input.nextInt(); //once the input has been found to be an int, assign to choice /*now choice needs to be checked for the correct range of values. * if its not within 1-4 then repeat while loop above*/ } while (choice < 1 | choice > 4); //input.close(); return choice; }