List of usage examples for java.util Scanner Scanner
public Scanner(ReadableByteChannel source)
From source file:Test.java
public static void main(String[] args) { try {/*www . j av a 2s.c om*/ System.out.print("Enter a number: "); int number = new Scanner(System.in).nextInt(); if (number < 0) { throw new InvalidParameter(); } System.out.println("The number is: " + number); } catch (InputMismatchException | InvalidParameter e) { System.out.println("Invalid input, try again"); } }
From source file:Main.java
public static void main(String[] args) { String s = "java2s.com 1 + 1 = 2.0 true "; Long l = 123456789098765432L; s = s + l;/*from w w w .j a v a 2 s . c o m*/ Scanner scanner = new Scanner(s); while (scanner.hasNext()) { System.out.println("Not Found :" + scanner.next()); if (scanner.hasNextLong()) { System.out.println("Found :" + scanner.nextLong()); } } scanner.close(); }
From source file:Main.java
public static void main(String[] args) { String s = "java2s.com "; Long l = 1234567890987654L;/*from w w w . j av a 2 s . com*/ s = s + l; Scanner scanner = new Scanner(s); // use US locale in order to be able to identify long in a string scanner.useLocale(Locale.US); while (scanner.hasNext()) { // check if the scanner's next token is along System.out.println(scanner.hasNextLong()); System.out.println(scanner.next()); } scanner.close(); }
From source file:Main.java
public static void main(String[] args) { String s = "java2s.com "; Long l = 1234567890987654L;// w w w. jav a 2s . c om s = s + l; Scanner scanner = new Scanner(s); // use US locale in order to be able to identify long in a string scanner.useLocale(Locale.US); while (scanner.hasNext()) { // check if the scanner's next token is a long with radix 4 System.out.println(scanner.hasNextLong(4)); System.out.println(scanner.next()); } scanner.close(); }
From source file:Main.java
public static void main(String[] args) throws Exception { URLConnection connection = new URL("http://java.net").openConnection(); String text = new Scanner(connection.getInputStream()).useDelimiter("\\Z").next(); }
From source file:Main.java
public static void main(String[] args) { String s = "java2s.com 1 + 1 = 2.0 true "; Float f = 1.2385f;/*from w ww . j av a 2s .co m*/ s = s + f; Scanner scanner = new Scanner(s); scanner.useLocale(Locale.US); while (scanner.hasNext()) { scanner.next(); if (scanner.hasNextFloat()) { System.out.println("Found :" + scanner.nextFloat()); } } scanner.close(); }
From source file:Main.java
public static void main(String[] args) throws Exception { Console cnsl = System.console(); if (cnsl != null) { System.out.print("Enter name : "); Scanner scan = new Scanner(cnsl.reader()); while (scan.hasNext()) { String str = scan.next(); System.out.println(str); }// w w w .j a v a 2 s . c o m } }
From source file:Main.java
public static void main(String[] args) { String str = "java2s.com"; ByteArrayInputStream bais = new ByteArrayInputStream(str.getBytes()); System.setIn(bais);//w ww. j a va2 s.com Scanner scanner = new Scanner(System.in); String input = scanner.next(); System.out.println(input); }
From source file:Main.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 a v a 2s.c o m FileReader fin = new FileReader("Test.txt"); Scanner src = new Scanner(fin); src.useDelimiter(", *"); 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:MainClass.java
public static void main(String args[]) throws IOException { 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 2 s.c o m*/ FileReader fin = new FileReader("Test.txt"); Scanner src = new Scanner(fin); // Set delimiters to space and comma. // ", *" tells Scanner to match a comma and zero or more spaces as // delimiters. src.useDelimiter(", *"); // Read and sum numbers. while (src.hasNext()) { if (src.hasNextDouble()) { System.out.println(src.nextDouble()); } else { break; } } fin.close(); }