Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

java.lang.NumberFormatException 1 (Averaging Program)

By: JustAHorriblePlayer on Apr 25th, 2012  |  syntax: Java  |  size: 1.56 KB  |  views: 25  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. import java.util.ArrayList;
  2. import java.util.Iterator;
  3. import java.util.Scanner;
  4.  
  5.  
  6. class Averaging {
  7.        
  8.         private static Scanner scanner = new Scanner(System.in);
  9.         private static int pin = 0000;
  10.         private static ArrayList<Integer> integer = new ArrayList<Integer>();
  11.        
  12.        
  13.         private static int totalSum = 0;
  14.         private static int entries = 0;
  15.        
  16.         public static void main(String[] args) {
  17.                 System.out.print("Welcome to the Averaging Program.");
  18.                 System.out.print("To verify you made this program, please enter a 4-digit pin: ");
  19.                 int passcode = scanner.nextInt();
  20.                 verify(passcode);
  21.         }
  22.        
  23.         private static void verify(int pass) {
  24.                 if(pass == pin) {
  25.                         System.out.printf("Password accepted.\n\nI will be asking you to input values\n");
  26.                         loopInsert();
  27.                 }
  28.         }
  29.        
  30.         private static void loopInsert() {
  31.                 for(int i = 0; i <= 25 ; i++) {
  32.                         System.out.print("Please insert a number, or to stop, type !stop.");
  33.                         String next = scanner.next();
  34.                         if(next == "!stop") {
  35.                                 System.out.print("Good. I am currently calculating right now..");
  36.                                 calculate();
  37.                         } else {
  38.                                 integer.add(Integer.parseInt(next));
  39.                         }
  40.                 }
  41.         }
  42.        
  43.         private static void calculate() {
  44.                 Iterator<Integer> iter = integer.iterator();
  45.                 while(iter.hasNext()) {
  46.                         entries++;
  47.                         totalSum += iter.next();
  48.                 }
  49.                 if(!iter.hasNext()) {
  50.                         int average = (totalSum/entries);
  51.                         System.out.printf("Your average is: " + average + ". I will show you what you have input.\n");
  52.                         System.out.print(integer.toString());
  53.                         System.out.print("To start a new session, please reboot me.");
  54.                 }
  55.         }
  56.  
  57. }