List of usage examples for java.util Scanner hasNext
public boolean hasNext()
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 byte with radix 7, print found and the byte if (scanner.hasNextByte()) { System.out.println("Found :" + scanner.nextByte(7)); }/*from w w w . ja va 2 s. c o m*/ // if a Byte is not found, print "Not Found" and the token 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 ww . j a v 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:Main.java
public static void main(String[] args) { String content = " <2008-10-07> hi <test>" + " <2008-11-26> user <test>" + " <2008-11-28><aaaa> "; Scanner sc = new Scanner(content).useDelimiter("\\s*[<>]\\s*"); while (sc.hasNext()) { System.out.printf("[%s|%s|%s]%n", sc.next(), sc.next(), sc.next()); if (sc.hasNext()) sc.next();/*from w ww .j a v a 2 s . c om*/ } }
From source file:ScanMixed.java
public static void main(String args[]) throws IOException { int i;/* w ww . j av a 2 s .c om*/ 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:SetTest.java
public static void main(String[] args) { Set<String> words = new HashSet<String>(); // HashSet implements Set long totalTime = 0; Scanner in = new Scanner(System.in); while (in.hasNext()) { String word = in.next();// w w w . j ava 2 s . com long callTime = System.currentTimeMillis(); words.add(word); callTime = System.currentTimeMillis() - callTime; totalTime += callTime; } Iterator<String> iter = words.iterator(); for (int i = 1; i <= 20 && iter.hasNext(); i++) System.out.println(iter.next()); System.out.println(". . ."); System.out.println(words.size() + " distinct words. " + totalTime + " milliseconds."); }
From source file:MainClass.java
public static void main(String args[]) throws IOException { int count = 0; double sum = 0.0; FileWriter fout = new FileWriter("test.txt"); fout.write("2 3.4 5 6 7.4 9.1 10.5 done"); fout.close();/*from w ww .j av a 2s . c o m*/ FileReader fin = new FileReader("Test.txt"); Scanner src = new Scanner(fin); while (src.hasNext()) { if (src.hasNextDouble()) { sum += src.nextDouble(); count++; } else { String str = src.next(); if (str.equals("done")) break; else { System.out.println("File format error."); return; } } } fin.close(); System.out.println("Average is " + sum / count); }
From source file:LogTest.java
public static void main(String[] args) throws IOException { String inputfile = args[0];//from w w w.j a v a 2 s . c o m String outputfile = args[1]; Map<String, Integer> map = new TreeMap<String, Integer>(); Scanner scanner = new Scanner(new File(inputfile)); while (scanner.hasNext()) { String word = scanner.next(); Integer count = map.get(word); count = (count == null ? 1 : count + 1); map.put(word, count); } scanner.close(); List<String> keys = new ArrayList<String>(map.keySet()); Collections.sort(keys); PrintWriter out = new PrintWriter(new FileWriter(outputfile)); for (String key : keys) out.println(key + " : " + map.get(key)); out.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); scanner.useLocale(Locale.US); while (scanner.hasNext()) { // check if the scanner's next token is a short with a radix 4 System.out.println(scanner.hasNextShort(4)); System.out.println(scanner.next()); }//w w w .ja va2s. c o m 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("2 3.4 5 6 7.4 9.1 10.5 done"); fout.close();/*from w w w . j ava2 s . c om*/ FileReader fin = new FileReader("Test.txt"); Scanner src = new Scanner(fin); // Read and sum numbers. while (src.hasNext()) { if (src.hasNextDouble()) { System.out.println(src.nextDouble()); } else { break; } } fin.close(); }
From source file:MainClass.java
public static void main(String args[]) throws IOException { int i;//from w w w .ja v a 2s.c om 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(); }