0
import java.util.Scanner;
public class Sales   
{
public static void main (String[] args)
{

 Scanner scan = new Scanner(System.in);

 String[] name = new String[5]; 
 double[] sales = new double[5]; 

     for (int i=0; i<5; i++) {  
     System.out.println ("What is the name of the person?");
     name[i] = scan.nextLine();
     System.out.println ("How much did he/she sell?");
     sales[i] = scan.nextDouble();
     }
     for (int j=0; j<5; j++) {  
     System.out.println (name[j] + " sold " + sales[j]);
     }


     int sum = 0; 
     for (double k : sales){
     sum+=k;
     }

     double avg = sum / 5; 
     System.out.println ("The average sales is: " + avg);

     for (int i = 0; i < 5; i++) {
     if (sales[i] >= avg){ 
     System.out.println (name[i] + " sold more than the average sales.");
     }
     }
 }

}

(This code is only a portion of the program.. sorry if it's kinda messy[im a beginner]) This is the issue... It outputs like this:
What is the name of the person?
Patrick
How much did he/she sell?
8000
What is the name of the person?
How much did he/she sell?

The lines in bold are the issue. I want to enter a different name and amount, but it asks me twice at the same time. Any help will be greatly appreciated. Thank you.

pmcg521
  • 199
  • 2
  • 13
  • 1
    Oh yes take a look to [this](http://stackoverflow.com/questions/17538182/getting-keyboard-input/17538216#17538216) and special to this [answer](http://stackoverflow.com/a/13102066/2415194) – nachokk Jan 11 '14 at 02:12

1 Answers1

4

After nextDouble you have to call scan.NextLine() to get rid of a end of line character that was left in the output.

The scan.nextDouble method reads just the number, nothing else. And because the user "confirms" his input by hitting enter, he will also send a end-of-line character there. The for loop would then take this end-of-line character as input for the first question. Hence - after the double is read, just call an empty scan.NextLine() and you will be fine :-) .

Martin Zikmund
  • 34,962
  • 7
  • 63
  • 83