I was having difficculties when trying to read from a JOPtionPane input dialog using a Scanner object (text below)
int array[] = new int[6];
for (int i=0; i<6; i++)
{
Scanner sc = new Scanner(System.in);
JOptionPane.showInputDialog("Enter a number to fill the array: ");
array[i]=sc.nextInt(System.in);
}
Difficulties got solved when changed code like follows:
String st;
int array[] = new int[6];
for (int i=0; i<6; i++)
{
st= JOptionPane.showInputDialog("Enter a number to fill the array: ");
array[i] = Integer.parseInt(st);
}
I would like to know why I can’t fill the array using a Scanner class object :/
Scanners can be set to take input from multiple different places, but unfortunately not from JOptionPane. If you want to use Scanner, you’ll have to pick a different method of input. Either a file, existing string or Console input.
Java 6 API – class Scanner
JOptionPane collects its input from only one place, itself.
Java 6 API – class JOptionPane