List of usage examples for java.util Scanner nextDouble
public double nextDouble()
From source file:MainClass.java
public static void main(String args[]) { String instr = "10 99.88 scanning is easy."; Scanner conin = new Scanner(instr); while (conin.hasNext()) { if (conin.hasNextDouble()) { System.out.println(conin.nextDouble()); }/*from w ww . j a v a 2s .com*/ } }
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();/* ww w . j av a 2 s . 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:Retirement.java
public static void main(String[] args) { // read inputs Scanner in = new Scanner(System.in); System.out.print("How much money do you need to retire? "); double goal = in.nextDouble(); System.out.print("How much money will you contribute every year? "); double payment = in.nextDouble(); System.out.print("Interest rate in %: "); double interestRate = in.nextDouble(); double balance = 0; int years = 0; // update account balance while goal isn't reached while (balance < goal) { // add this year's payment and interest balance += payment;/*from ww w . j av a2s . c om*/ double interest = balance * interestRate / 100; balance += interest; years++; } System.out.println("You can retire in " + years + " years."); }
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 av a 2s.co m*/ 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:Calculator.java
public static void main(String[] args) { System.out.println("type something like: 1+3"); Scanner scanner = new Scanner(System.in); double n1 = Double.NaN; double n2 = Double.NaN; String operation = null;/*from w ww .j a v a 2s .c o m*/ try { n1 = scanner.nextDouble(); operation = scanner.next(); n2 = scanner.nextDouble(); double result = calculate(n1, n2, operation); System.out.printf("%s %s %s = %.2f%n", n1, operation, n2, result); } catch (Exception e) { System.out.println("An invalid expression."); } }
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 w w .ja v a 2 s . 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 { 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 .ja v a2 s .c om*/ 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[]) { Scanner conin = new Scanner(System.in); int count = 0; System.out.println("Enter numbers to average."); while (conin.hasNext()) { if (conin.hasNextDouble()) { System.out.println(conin.nextDouble()); count++;//from w w w .j a va2 s .c o m } if (count == 3) { System.exit(0); } } }
From source file:MainClass.java
public static void main(String args[]) { Scanner conin = new Scanner(System.in); int count = 0; double sum = 0.0; System.out.println("Enter numbers to average."); while (conin.hasNext()) { if (conin.hasNextDouble()) { sum += conin.nextDouble(); count++;//from w ww . jav a 2s . co m } else { String str = conin.next(); if (str.equals("done")) break; else { System.out.println("Data format error."); return; } } } System.out.println("Average is " + sum / count); }
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();// w w w. j a va2s .c o m 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(); }