Jodie
Jodie

Reputation: 11

How do you store data values into an array from user input in a dialog box?

import javax.swing.JOptionPane;
import java.text.DecimalFormat;

public class quiz
  public static void main(String[] args)
  {
    String name, allname, loopname;
    int names, allnames, op, loopsname;
    int arValues[];

    name = JOptionPane.showInputDialog(null, "Enter how many friends you have:");
    names = Integer.parseInt(name);
    arValues = new int[name-1];

**for(names = 0; names == names; names++)
{
  loopname = JOptionPane.showInputDialog(null, "enter your friend's names);
  loopsname = Integer.parseInt(loopsname);
  arValues[names] = loopsname;
}**

allname = JOptionPane.showInputDialog(null, "Enter the number of which friends name in which you want to see");
allnames = Integer.parseInt(allname);
op = arValues[allnames];

}

 public static void finish()
{
  System.exit(0);
}
}

The code compiles, but when it is ran it has multiple run-time errors, including being out of bounds. need help figuring it out

Error Message:

java.lang.ArrayIndexOutOfBoundsException: 2 at quiz.main(quiz.java:28) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:27‌​2)

Upvotes: 1

Views: 1564

Answers (1)

Majora320
Majora320

Reputation: 1351

In the for loop, the condition names == names is always true. What you should do is use a temporary variable, let's say i, like so:

for (int i = 0; i < names; i++) {
    loopname = JOptionPane.showInputDialog(null, "Enter value for array:");
    loopsnames = Integer.parseInt(loopsnames);
    arValues[i] = loopsnames;
}

Each iteration of the loop now checks i to see if it is less than names, and if that is true, increments it and runs the code inside the loop. This ensures that the loop will run exactly values times. The other place that was causing an error was the line:

arValues = new int[names-1];

You should have done:

arValues = new int[names];

Since you want to keep exactly names elements.
Here is a complete and working version of your code:

import javax.swing.JOptionPane;
import java.text.DecimalFormat;

public class  quiz {
    public static void main(String[] args) {
        String name, allname, loopname;
        int names, allnames, op, loopsname;
        int arValues[];

        name = JOptionPane.showInputDialog(null, "Enter how many friends names you would like ot enter");
        names = Integer.parseInt(name);
        arValues = new int[value];

        for (int i = 0; i < names; i++) {
            loopname = JOptionPane.showInputDialog(null, "Enter your friend's name");
            loopnames = Integer.parseInt(loopname);
            arValues[i] = loopnames;
        }

        allname = JOptionPane.showInputDialog(null, "Enter the number of which friend you want to see");
        allnames = Integer.parseInt(allname);
        op = arValues[allnames];
    }

    public static void finish() {
        System.exit(0);
    }
}

Upvotes: 1

Related Questions