Advertisement
Guest User

q1

a guest
Jan 15th, 2018
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 0.88 KB | None | 0 0
  1. package harr4603_lab02;
  2.  
  3. import java.io.File;
  4. import java.io.FileNotFoundException;
  5. import java.util.Scanner;
  6.  
  7. /**
  8.  * @author Jordan Harris
  9.  * @version 1.0
  10.  *
  11.  */
  12. public class ScannerTest
  13. {
  14.  
  15.     public static void main(String[] args)
  16.     {
  17.          System.out.println("Enter a series of integers. Press 'q' to quit.");
  18.         // Read from the keyboard.
  19.         Scanner s = new Scanner(System.in);
  20.         String result = "";
  21.         int total = 0;
  22.         result = s.next();
  23.         while (!result.equals("q"))
  24.         {
  25.             result = s.next();
  26.             try
  27.             {
  28.                 total += Integer.parseInt(result);
  29.             }
  30.             catch(NumberFormatException nfe)
  31.             {
  32.                 System.out.println("Invalid entry!");
  33.             }
  34.            
  35.         }
  36.  
  37.         s.close();
  38.         System.out.println("The total is " + total);
  39.  
  40.     }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement