3
public static void main(String[] args) 
{
    Scanner input = new Scanner(System.in);
    String[]repName = new String[5];
    double[]salesAmount = new double[5];
    System.out.println("Please Enter Sales Reps Name Followed By Monthly Sales: \n");
    for (int i = 0  ; i < repName.length; i++ ) 
    {
        System.out.print("Sales Rep (Full Name):  "  );
        repName[i] = input.nextLine();

        System.out.print("Monthly Sales:  € "  );

        salesAmount[i] = input.nextDouble();
        System.out.println();
    }     

The code will only allow me to input a full name once, ie John Doe. It will not let me enter it with other doubles in the array. Why is this?

Ian Murray
  • 37
  • 4

3 Answers3

1

I got your error. When you enter double, you press enter. That is a new line which was taken instead of another name. Then it is expecting double but you are entering another name. So you get an input Mismatch.

After input.nextDouble() write input.nextLine();

The code should look like this.

 public static void main(String[] args)
{
    Scanner input = new Scanner(System.in);
    String[]repName = new String[5];
    double[]salesAmount = new double[5];
    System.out.println("Please Enter Sales Reps Name Followed By Monthly Sales: \n");
    for (int i = 0  ; i < repName.length; i++ )
    {
        System.out.print("Sales Rep (Full Name):  "  );
        repName[i] = input.nextLine();

        System.out.print("Monthly Sales:  € "  );

        salesAmount[i] = input.nextDouble();
        input.nextLine();
        System.out.println();
    }
}
Anil
  • 518
  • 4
  • 12
0

I have run your code, It's running well I think while entering salary you have not hit Enter key of your keybord.

I found that you are using loop over the length of person name. I think it is something wrong.

atish shimpi
  • 4,461
  • 1
  • 28
  • 46
0

An easy way to solve this is to just use the next method instead of nextLine.

repName[i] = input.next();

Shadowvail
  • 368
  • 3
  • 7