1

So for this program I need to have three arrays accept data and display the gross pay, It seems to work at first but after enter the first person's data, my code begins to stack on top of itself in the window, please help me fix this to work properly.

I am required to use arrays. Objects are not allowed.

import java.util.Scanner;

public class ThreeArrays {

    public static void main(String args[]) {
        float[] payRate = new float[5];
        float[] hours = new float[5];
        String[] name = new String[5];

        getPayData(name, hours, payRate);

        displayGrossPay(name, hours, payRate);
    }

    public static void getPayData(String[] name, float[] hours, float[] payRate) {
        Scanner kb = new Scanner(System.in);

        for (int i = 0; i < hours.length; i++) {

            System.out.print("Enter the employee's name: ");
            name[i] = kb.nextLine();

            System.out.print("Enter the employee's hours: ");
            hours[i] = kb.nextFloat();

            System.out.print("Enter the employee's hourly rate: ");
            payRate[i] = kb.nextFloat();
        }

    }

    public static void displayGrossPay(String[] name, float[] hours, float[] payRate) {
        for (int i = 0; i < hours.length; i++) {
            System.out.println("Employee name: " + name[i] + " Gross Pay: " + hours[i] *
                payRate[i]);
        }
    }
}
Zabuzard
  • 20,717
  • 7
  • 45
  • 67
  • Clear the buffer. – Compass May 02 '18 at 17:46
  • "*PLEASE DONT TELL ME TO USE OBJECTS MY TEACH REQUIRES THAT I USE ARRAYS*" - You do not need to yell at us. --- Please see: [Why is “Can someone help me?” not an actual question?](http://meta.stackoverflow.com/q/284236) and [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). – Turing85 May 02 '18 at 17:47
  • What does _my code begins to stack on top of itself_ mean? – Andrew S May 02 '18 at 17:48
  • 1
    Just curious, in what type of environment (high school, university, community college, etc.) are you taking this course? – Jim Garrison May 02 '18 at 17:49
  • So when i use the program on the 4 line after i execute it says "Enter employee's name: Enter employee's hours:" on the same line – Jeff Furman May 02 '18 at 17:50
  • I am in college – Jeff Furman May 02 '18 at 17:51
  • 2
    Change `System.out.print()` to `System.out.println()` **EDIT:** I understand your problem now. Just print a blank line between your inputs. – Jimenemex May 02 '18 at 17:53
  • I just did it still does it just on two lines, Can anyone please execute this code so they can see what I see, I appreciate any help. Go through entering the first employees info and when the second comes up you will see what I see – Jeff Furman May 02 '18 at 17:56
  • @JeffFurman If this assignment is not being given as an example of "how NOT to write code", I would seriously question the instructor's competence. There is almost never a good reason to write parallel array code, which is so 1960's Dartmouth Basic, in any modern OO language. – Jim Garrison May 02 '18 at 17:56
  • The program prompts correctly for the first employee but the second comes through incorrectly – Jeff Furman May 02 '18 at 17:57
  • This seems to be a dupe of: https://stackoverflow.com/questions/13102045 – Jorn Vernee May 02 '18 at 17:57
  • @JimGarrison I know it is so stupid but he says "Arrays is your classes weak point so I am giving you this assignment about them" – Jeff Furman May 02 '18 at 17:59
  • My second employee name input line is being skipped – Jeff Furman May 02 '18 at 18:02
  • 1
    Don't clear your question. Instead up-vote helpful answers and mark the one that solved the issue as **accepted**. – Zabuzard May 02 '18 at 18:27

3 Answers3

0

Put System.out.println(); between each statement where you ask for data.

EDIT: It could also be that the \n character isn't being read with .nextInt(). To fix it, put kb.nextLine(); after kb.nextFloat();

    for (int i = 0; i < hours.length; i++) {

        System.out.print("Enter the employee's name: ");
        name[i] = kb.nextLine();

        System.out.println("");

        System.out.print("Enter the employee's hours: ");
        hours[i] = kb.nextFloat();
        kb.nextLine();
        System.out.println("");

        System.out.print("Enter the employee's hourly rate: ");
        payRate[i] = kb.nextFloat();
        kb.nextLine();
        System.out.println("");

    }

    // Enter the employee's name: Foo
    // (Blank Line)
    // Enter the employee's hours: 1
    // (Blank Line)
    // Enter the employee's hourly rate: -20.00 // They owe me now. :D
    // (Blank Line)
Jimenemex
  • 2,836
  • 2
  • 13
  • 39
0

The problem is related to stdin flushing. Because last entered float value for line payRate[i] = kb.nextFloat(); don't removes \n character. This \n character is being fetched by line name[i] = kb.nextLine(); in next iteration. Which further leaves a String value for line hours[i] = kb.nextFloat();. Since it is not a valid float hence you are getting exception: java.util.InputMismatchException.

To solve this use line if(i < hours.length-1) kb.next(); after line payRate[i] = kb.nextFloat();.

Following is corrected code. See complete working code here:

public class ThreeArrays {

    public static void main(String args[]) {
        float[] payRate = new float[5];
        float[] hours = new float[5];
        String[] name = new String[5];

        getPayData(name, hours, payRate);

        displayGrossPay(name, hours, payRate);
    }

    public static void getPayData(String[] name, float[] hours, float[] payRate) {
        Scanner kb = new Scanner(System.in);

        for (int i = 0; i < hours.length; i++) {

            System.out.print("Enter the employee's name: ");
            name[i] = kb.nextLine();

            System.out.print("Enter the employee's hours: ");
            hours[i] = kb.nextFloat();

            System.out.print("Enter the employee's hourly rate: ");
            payRate[i] = kb.nextFloat();

            if(i < hours.length-1) kb.next();
        }

    }

    public static void displayGrossPay(String[] name, float[] hours, float[] payRate) {
        for (int i = 0; i < hours.length; i++) {
            System.out.println("Employee name: " + name[i] + " Gross Pay: " + (hours[i] *
                payRate[i]));
        }
    }
}
cse
  • 3,622
  • 2
  • 17
  • 36
-1

I have figured it out everyone

The problem lies with using

 System.out.print("Enter the employee's name: ");
 name[i] = kb.nextLine();

I had to change it to

System.out.print("Enter the employee's name: ");
name[i] = kb.next();