chamara
chamara

Reputation: 11

Java Array index out of bounds Exception when getting string input in java

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();

Upvotes: 1

Views: 4072

Answers (4)

Reimeus
Reimeus

Reputation: 159754

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

Upvotes: 1

Anton Bessonov
Anton Bessonov

Reputation: 9803

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.

Upvotes: 0

chrome
chrome

Reputation: 659

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();
}

}

Upvotes: 0

FThompson
FThompson

Reputation: 28687

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];
}

Upvotes: 0

Related Questions