Evan Tempel
Evan Tempel

Reputation: 1

Getting array values from user input

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.

Upvotes: 0

Views: 66

Answers (1)

Jean-François Savard
Jean-François Savard

Reputation: 21004

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();

Upvotes: 1

Related Questions