Sarah Kubitsky
Sarah Kubitsky

Reputation: 51

My loop in java is not working. Can anyone see why?

I have a lab in programming due soon. My program stops after asking for inputs after it asks for one. I cannot see what I did wrong. Can anyone see the problem?

public static double dReadInputs(int numberOfInputs)        //this accepts and fills an array with grade inputs
{
    int t = 0;
    double[] inputs = new double[numberOfInputs];
    while (t < numberOfInputs)
    {
        JOptionPane.showInputDialog("Please enter 10 grade values one at a time: ");
        inputs[10] = Integer.parseInt(in.nextLine());
        t++;
    }
    return inputs[10];

Upvotes: 2

Views: 63

Answers (3)

jiaweizhang
jiaweizhang

Reputation: 819

public static double[] dReadInputs(int numberOfInputs)        //this accepts     and fills an array with grade inputs
{
    int t = 0;
    double[] inputs = new double[numberOfInputs];
    while (t < numberOfInputs)
    {
        JOptionPane.showInputDialog("Please enter "+numberOfInputs+"grade values one at a time: ");
        inputs[t] = Integer.parseInt(in.nextLine());
        t++;
    }
    return inputs;
}

Basically, you want to get all the inputs so you want to return a double array instead of just a single double. Also, make sure you use a counter (or even better yet create a for loop) so you update the respective double in the double array.

What's happening is that you're always writing to the 11th item in the array when you only have an array of size 10, so it's giving an ArrayOutOfBoundsException.

Upvotes: 1

Raf
Raf

Reputation: 7649

Based on my understanding from your question description. You want the JOptionPane to show 10 times and each time take input and assign it to the array. See below:

public static double[] dReadInputs(int numberOfInputs)       
    {
        int t = 0;
        double[] inputs = new double[numberOfInputs];
        while (t < numberOfInputs)
        {
            String in = JOptionPane.showInputDialog("Please enter value for grade " + (t + 1) + " : ");
            inputs[t] = Double.parseDouble(in);
            t++;
        }
        return inputs;
    }

You can also protect your code against possible NullPointerException which can happen when user do not provide input and close the input dialog using Cancel or Close buttons. Solution:

if (in != null) {
  //inform user to enter valid +ve integer/double
  //you might wanna continue back to loop without incrementing t 
} 

You can protect the code against NumberFormatException by making sure that the user always inputs valid number and nothing else. Even empty input dialogue would cause exception: Solution:

try {
    inputs[t] = Double.parseDouble(in);
} catch(NumberFormatException nfe) {
  //inform user to input valid +ve integer/double
  //you might wanna continue back to loop without incrementing t
}

Upvotes: 1

v.coder
v.coder

Reputation: 1932

The index 10 in inputs[10] throws an exception because it might be greater than the size of the inputs variable.

This might work in your case:

inputs[t] = Integer.parseInt(in.nextLine()); 

Upvotes: 1

Related Questions