-4

I am trying to split a string in the following format: AND 1 0.

The logic method below takes in a String read using .nextLine().

While I am able to split the String into Array using the split method and \\s+ regex, I run into an ArrayOutOfBound error whenever I try to access the second value of the array.

The second and third index of the Array should contain the values '1' and '2' respectively - why does this yield an error?

Code:

import java.util.*;

public class HelloWorld {

  public static int logic(String operator){
    String[] splitted = operator.split("\\s+");
    int val = 0;

    System.out.println(splitted.length); //Returns 3
    System.out.println(operator); //Returns AND 1 0
    System.out.println(splitted[0]); //Returns AND
    System.out.println(splitted[2]);  //ERROR
    return val;
  }

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        int val = sc.nextInt(); //Error lies here, should be parsed as String.

        if(val == 1){
            int no_times = sc.nextInt();
            for(int i = 0 ; i < no_times; i++){
                System.out.println(logic(sc.nextLine()));
            }
            sc.close();
        }
    }
}

Input:

OR 1 1
AND 0 1
AND 1 0
OR 0 0
AND 0 0
OR 1 0
OR 0 1
OR 0 1
OR 1 1
OR 1 0
Murthi
  • 5,051
  • 1
  • 8
  • 15
Carrein
  • 2,631
  • 2
  • 19
  • 53

6 Answers6

1

I have try running your code, and it produce the correct output without errors.

However when I try to replace the space between "AND", "1", "0" with some strange char(maybe in Chinese call "SBC case" space, those chars cannot be recognized by \\s+), it reproduce your error. So I advice you check whether there is a english space char(which can be recognized by \\s+) between "AND", "1", "0".

firesurfing
  • 145
  • 3
0

Try this. you get that error because splitted's length is less than you want to print.

First Method

    public static int logic(String operator){
        String[] splitted = operator.split("\\s+");
        int val = 0;
        System.out.println(operator); //Prints "AND 1 0"
        System.out.println("splitted length :: "+splitted.length()); 
        for (String str:splitted) {
            System.out.println(str); //Prints "AND"
        }
        // System.out.println(splitted[1]); //ERROR
        return val;
    }

Second Method

    public static int logic(String operator){
        String[] splitted = operator.split("\\s+");
        int val = 0;
        System.out.println(operator); //Prints "AND 1 0"
        System.out.println("splitted length :: "+splitted.length()); 
        for (int i = 0; i<splitted.length();i++) {
            System.out.println(splitted[i]); //Prints "AND"
        }
        // System.out.println(splitted[1]); //ERROR
        return val;
    }
Upendra Shah
  • 1,681
  • 13
  • 21
0

I am not able to see the error,the code snippet provided by you should work. Though you can use below to avoid exception:

public static int logic(String operator){
        String[] splitted = operator.split("\\s+");
        int val = 0;
        System.out.println(operator); //Prints "AND 1 0"
        for(String split : splitted) {
            System.out.println(split);
        }
        return val;
      }
jeet427
  • 510
  • 3
  • 11
0

You really should make the intention of your code more clear, especially when asking for help. As for your problem, this is a duplicate of Scanner is skipping nextLine() after using next() or nextFoo()?

Scanner#nextInt doesn't consume the \n after the input, so the nextLine() is just receiving \n.

Fixed code:

public static void main(String[] args) {
    System.out.print("Type 1 to continue: ");
    Scanner sc = new Scanner(System.in);
    int val = sc.nextInt();

    if(val == 1){
        System.out.print("\nHow many tests do you want to run? ");
        int no_times = sc.nextInt();
        sc.nextLine();
        for(int i = 0 ; i < no_times; i++){
            System.out.print("\nLogic: ");
            System.out.println(logic(sc.nextLine()));
        }
        sc.close();
    }
}
Trophonix
  • 92
  • 1
  • 6
0

The nextLine method is reading the value from current cursor location to till the end of the line and it includes the delimiter as well. So when we read the first line of input it takes the space which is delimiter here. For example, if our first is like 1 1 OR 1 1

then first nextInt returns 1 then second nextInt return 1 then nextLine returns ' OR 1 1' and length of it is 4. I guess, your first input is wrong may be you are missing something there. one simple solution is just change the following line your code.

String[] splitted = operator.split("\\s+");

to

String[] splitted = operator.trim().split("\\s+");

Just trim before splitting. so that, trailing spaces are removed. It may solve your problem.

Murthi
  • 5,051
  • 1
  • 8
  • 15
-1

The full input takes in two integer first. This two integers were parsed by nextInt() rather than nextLine(), hence the given argument for the logic method was not taking in a complete String - hence the splitting fails.

Carrein
  • 2,631
  • 2
  • 19
  • 53