2 Replies - 206 Views - Last Post: 16 January 2013 - 12:22 PM Rate Topic: -----

#1 SwoleWithBacon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 16-January 13

Error with input from user.

Posted 16 January 2013 - 12:06 PM

Whenever I ask the user to input some values, it seems to take in two values per request. One set is used for the calculations of total, average, posCount, etc.... while the second set of value seems to be used to signify the end of input with a negative number. I cannot find where in my code the program is requiring two sets of values.
 public class Test {
	public static void main(String [] args){
		Scanner input = new Scanner(System.in);
		double[] scoreArray = new double[10];
		double total = 0;
		int count = 0;
		double average = 0;
		
		for(int i = 0; i < scoreArray.length; i++){
			System.out.print("Enter a new score: ");
			scoreArray[i] = input.nextDouble();
			if(scoreArray[i] >= 0){
				count++;
				total += scoreArray[i];
			}
			if(input.nextDouble() < 0){
				break;
			}
		}
		
		average = total / count;
		
		int posCount = 0;
		int negCount = 0;
		for(int j = 0; j < count-1; j++){
			if(scoreArray[j] >= average){
				posCount++;
			}
			else{
				negCount++;
			}
		}
		
		System.out.println("The total is " + total);
		System.out.println("The total count is "  + count);
		System.out.println("The average is " + average);
		System.out.println("There are " + posCount + " number(s) equal to or above the average.");
		System.out.println("There are " + negCount + " number(s) less than the average.");
	}		
	}


Is This A Good Question/Topic? 0
  • +

Replies To: Error with input from user.

#2 FallenG  Icon User is offline

  • New D.I.C Head

Reputation: 22
  • View blog
  • Posts: 44
  • Joined: 12-January 13

Re: Error with input from user.

Posted 16 January 2013 - 12:11 PM

If have used input.nextDouble() to mean "wait for the user to input a Double" once, but you then use it again in the loop and don't expect the same behaviour?
Was This Post Helpful? 1
  • +
  • -

#3 SwoleWithBacon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 16-January 13

Re: Error with input from user.

Posted 16 January 2013 - 12:22 PM

View PostFallenG, on 16 January 2013 - 12:11 PM, said:

If have used input.nextDouble() to mean "wait for the user to input a Double" once, but you then use it again in the loop and don't expect the same behaviour?

Thank you! I would have never looked at that if you hadn't told me.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1