0

Here is my code:

for (int i = 0; i < 99; i++)
{

String inputString = keyboard.next();
String[] inputArray = inputString.split(":");

if (inputString.equals("quit"))
    System.out.println("You have quit");

FirstArray[i] = inputArray[0];
SecondArray[i] = Integer.parseInt(inputArray[1]);  // these throw errors
ThirdArray[i] = Integer.parseInt(inputArray[2]);    

System.out.println(FirstArray[i]);
System.out.println(SecondArray[i]);
System.out.println(ThirdArray[i]);

So here is my code, I'm trying to test out arrays and I need to get input from the user split using the delimiter ":"

I had to parseInt the last two arrays (as they are taking in integer values) to get the split input from the second and third index of the inputArray.

I have the last part of the code to test if it works, and it does but when I type in "quit" to end the loop it throws:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1

I have searched and understood the issue but don't know how to avoid it. Sorry if I'm not explaining my issue, would appreciate another working solution. Thanks in advance for help

icedwater
  • 4,701
  • 3
  • 35
  • 50

4 Answers4

1

The string "quit" does not contain any ":" characters, so the result of inputString.split(":") is an array with a single element. So as soon as you try to access inputArray[1], you will have the exception, because index 1 refers to the 2nd element in the array, although this array has only one element

if (inputString.equals("quit")) {
    System.out.println("You have quit");
    return;  // add this line
}

Add the return statement (shown above), and this will by pass the code problematic code. It seems like the right thing to do anyways, as the user is asking to quit the program.

EJK
  • 12,332
  • 3
  • 38
  • 55
1

Access inputArray only till its length i.e use inputArray.length() first to find array length then access array elements from 0 to length -1.

Most evident case from your code is when you enter quit but other inputs might cause it too since your are not checking length of array i.e. if length of splitted array is less that 3 for whatever input , you will receive this exception.

Sabir Khan
  • 9,826
  • 7
  • 45
  • 98
0

The issue you are running into is that the code accessing the inputArray variable is run regardless of whether or not the quit command is received. You have two options here.

1) Return on the quit command (recommended)

if (inputString.equals("quit")) {
    System.out.println("You have quit");
    return; // This will avoid running the code below
}

FirstArray[i] = inputArray[0];
SecondArray[i] = Integer.parseInt(inputArray[1]);  // these throw errors
ThirdArray[i] = Integer.parseInt(inputArray[2]);    

System.out.println(FirstArray[i]);
System.out.println(SecondArray[i]);
System.out.println(ThirdArray[i]);

2) Throw the remaining code in an else case

if (inputString.equals("quit")) {
    System.out.println("You have quit");
} else {
    FirstArray[i] = inputArray[0];
    SecondArray[i] = Integer.parseInt(inputArray[1]);  // these throw errors
    ThirdArray[i] = Integer.parseInt(inputArray[2]);    

    System.out.println(FirstArray[i]);
    System.out.println(SecondArray[i]);
    System.out.println(ThirdArray[i]);
}

I would also recommend adding an error case if the inputArray doesn't end up being the expected length.

if (inputArray.length != 3) {
    System.out.println("That's weird. I was expecting 3 parameters, but only found " + inputArray.length);
    return;
}
Jeremy Mangas
  • 361
  • 1
  • 8
  • Thanks, the return bit of code is really helpful for me and understanding what it does, my next challenge is validation and your last suggestion is great :) Getting the hang of methods within the string class now, thanks. – Shackmeister Jan 11 '16 at 04:34
0

you can use Scanner class to read the input.

Scanner scanner = new Scanner(System.in);
for(int i=0; i<Noofiterations; i++){ //iterations are the no.of times you need to read input.
  String[] inputArray = scanner.nextLine().split(":");
//rest of the code is same as yours.
}
Input should be in the form "abc:123:334:wet"

Hope this helps. Let me know if i didn't get your question.

user2503849
  • 110
  • 12