1

I got an Java ArrayIndexOutOfBoundsException when getting String input in Java. Please help me. This is my code: I edited my code to split using : it says

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1 at solution2.Solution.main(Solution.java:27)

public class Solution {

 static String search;

 public static void main(String[] args){

   String[] fn,ln,tp;
  boolean[] isSet;
 Scanner sc=new Scanner(System.in);      
 int no=sc.nextInt();

 String[][] temp=new String[no][3];
 fn=new String[no];
  ln=new String[no];
   tp=new String[no];
   isSet=new boolean[no];
   boolean flag=false;

     for(int i=0;i<no;i++){
   temp[i]=sc.nextLine().split(":");
   fn[i]=temp[i][0];
   ln[i]=temp[i][1];
   tp[i]=temp[i][2];
   isSet[i]=true;

     }

       System.out.println(temp.length);

      search=sc.nextLine();
2
  • Which one is line 27? What is the input for which you're getting the error? Commented Oct 20, 2012 at 15:26
  • temp[i]=sc.nextLine().split(":"); fn[i]=temp[i][0]; ln[i]=temp[i][1]; tp[i]=temp[i][2]; line we get error Commented Oct 20, 2012 at 15:48

4 Answers 4

1

The exception is occurring on this line:

ln[i] = temp[i][1];

so it appears

temp[i] = sc.nextLine().split(":");

is not receiving enough tokens in the :-delimited string to have create String array of size 3.

You will need to ensure temp[i].length == 3 to ensure that you can assign these tokens.

An example of valid input (note: no newline) is:

1 test:foo:bar
Sign up to request clarification or add additional context in comments.

Comments

0

The ArrayIndexOutOfBoundsException occurs when you are accessing non-existent indexes on the array created from split(":").

In this piece of code, temp[i] is not ensured to have values at index 0, 1, or 2, because if the nextLine() is something such as "dog", there are no colon characters to split around.

temp[i]=sc.nextLine().split(":");
fn[i]=temp[i][0];
ln[i]=temp[i][1];
tp[i]=temp[i][2];

To fix the issue, you should verify that the array indeed has the index before trying to access it.

temp[i]=sc.nextLine().split(":");
if (temp[i].length >= 3) {
    fn[i]=temp[i][0];
    ln[i]=temp[i][1];
    tp[i]=temp[i][2];
}

Comments

0

I insert sc.nextLine(). below int no = sc.nextInt(); line.

import java.util.Scanner;

public class Solution {

static String search;

public static void main(String[] args) {

    String[] fn, ln, tp;
    boolean[] isSet;
    Scanner sc = new Scanner(System.in);
    int no = sc.nextInt();
    sc.nextLine(); // **********

    String[][] temp = new String[no][3];
    fn = new String[no];
    ln = new String[no];
    tp = new String[no];
    isSet = new boolean[no];
    boolean flag = false;

    for (int i = 0; i < no; i++) {
        temp[i] = sc.nextLine().split(":");
        fn[i] = temp[i][0];
        ln[i] = temp[i][1];
        tp[i] = temp[i][2];
        isSet[i] = true;

    }

    System.out.println(temp.length);

    search = sc.nextLine();
}

}

Comments

0

You problem is, that sc.nextLine() return new line String (e.g. "\n") after you press [Enter]. Second time it's wait for you input. SeeJava String Scanner input does not wait for info, moves directly to next statement. How to wait for info?

In your case try to invoke sc.nextLine() before you processing:

sc.nextLine()
temp[i]=sc.nextLine().split(":");

EDIT: you are right, you must insert it after nextInt(), because nextLine() consume complete line.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.