0

and It would be awesome if someone can help me.

I'm trying to use the Split command of Java, to split a String using the space BUT the problem is, that maybe the string won't have a space, that means it will be just a simple order (instead of "enter 2" it will be "exit")

Scanner SC = new Scanner(System.in);
String comando = SC.nextLine();
String[] comando2 = comando.split("\\s+");
String first = comando2[0];
String second = comando2[1];

When i try this, it works if I write "enter 3" because "first = enter" and "second = 3", but if I write "exit" it throws an error, because second doesn't have a value. I would like to split the String, so when i try to this the following:

if ( comando.equalsIgnoreCase("exit"))
    // something here
else if ( first.equalsIgnoreCase("enter"))
    // and use String "second"

Can someone help? Thank you!

3
  • What is your problem? Your code should work except you are doing comments wrong (// instead of /). Commented Jan 15, 2015 at 0:54
  • No, in Java you can't work with values that haven't been initialitated. That is why my code doesn't work. Commented Jan 15, 2015 at 14:05
  • I meant the second example (with the if clauses), which does some implicit checking. Commented Jan 15, 2015 at 22:33

3 Answers 3

4

Don't try to access the second element in the array until you're sure it exists. Example:

if(comando2.length < 1) {
    // the user typed only spaces
} else {
    String first = comando2[0];
    if(first.equalsIgnoreCase("exit")) { // or comando.equalsIgnoreCase("exit"), depending on whether the user is allowed to type things after "exit"
        // something here

    } else if(first.equalsIgnoreCase("enter")) {
        if(comando2.length < 2) {
            // they typed "enter" by itself; what do you want to do?
            // (probably print an error message)
        } else {
            String second = comando2[1];
            // do something here
        }
    }
}

Notice how this code always checks comando2.length before trying to access the elements of comando2. You should do the same.

Sign up to request clarification or add additional context in comments.

Comments

1

How about this?

...
String[] comando2 = comando.split("\\s+");
String first = comando2.length > 0 ? comando2[0] : null;
String second = comando2.length > 1 ? comando2[1] : null;
...

Your problem is that you access an array element before you know if it exists or not. This way you get the value if the array is long enough or get null if it is not.

The expression a ? b : c evaluates to b if a is true or to c if a is false. This ? : operator is called ternary operator.

Comments

-1

Why not check if it has spaces, and handle it differently if it does:

if (comando.contains(" "))
{
    String[] comando2 = comando.split(" ");
    String first = comando2[0];
    String second = comando2[1];
}
else
{
    String first = comando;
}

1 Comment

This is bad design. You can never assume that a word will always come after the space. If the user type a string with a space after, this will fail.

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.