0

I am trying to get an integer and a double value from the user using a scanner. Im having trouble setting the values i get to the array itself. Here is my code:

import java.util.Scanner;

public class MainClass {

    public static void main(String[] args) {
        final int SIZE = 7;
        int[] arr = new int[SIZE];
        int[] arr2 = new int[SIZE];
        double[] dub = new double[SIZE];

        Scanner sc = new Scanner(System.in);

        PayRoll p = new PayRoll();

        for(String i: p.AccEmp()){
            System.out.println("How many hours were worked by the this employee number: " + i);
             arr = sc.nextInt();
            p.SetHRS(arr);

            System.out.println("What is the pay for this employee number: " + i);
            dub = sc.nextDouble();
            p.setPR(dub);


        }

    }

}

P is an instance, accEmp is an accessor from another class. Also, i cant use Array List.

1
  • Please, use more descriptive variable names. It'll make your code easier to read for everyone, including yourself. For example, sc can be input. Commented Apr 18, 2015 at 0:22

1 Answer 1

1

Your arr variable is an array of int. Scanner.nextInt will read and int, but not an array.

The line arr = sc.nextInt() won't compile. Either change arr to be an int or add the value to the array.

I believe, since you seem to be looping over employees, that you should keep a reference to the looping index and add to the array at that index :

for(int i = 0; i < p.accEmp().length/* or .size() if it's a list */; i++)
{
    arr[i] = sc.nextInt();
}

and also incorporate this within the loop, the part where you get the double values:
dub[i]=sc.nextDouble();

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.