0

I have this code, that lets the user enter a string with 2 parts separated by the character (-) and the inputted string, would be split into two different parts, and then output those two parts on the screen separated by a comma.

public class Test
{
public static void main(String[] args)
{
    java.util.Scanner scanner = new java.util.Scanner(System.in);

    String part = "";

    System.out.println("Please enter the 2-part string to be split: ");
    part = scanner.next();

    String[] parts = part.split("-");
    String part1 = parts[0];
    String part2 = parts[1];

    System.out.print("," + part1 + "," + part2);

    scanner.close();
}

}

Now lets say the user enterd

aaa-222

the outcome would be

aaa,222

What if i want to enter a string that has spaces, like this

i love you-i hate you

how do i split (i love you) and (i hate you) ? I get this error with the code above.

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
    at tutorial.Test.main(test.java:17)

PS. the split parts should include the whitespace.

  • 1
    *I get an error when the code above*: reading the error helps finding its cause. Using a debugger also helps. Also read the javadoc of Scanner, and especially its next() method. – JB Nizet Sep 26 '16 at 19:00
  • What error do you get? – Vivek Kumar Sep 26 '16 at 19:01
  • I edited the post. This error " Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1 at tutorial.Test.main(test.java:17) " – Rafat Rifaie Sep 26 '16 at 19:02
  • 1
    nextLine() takes white space – scarecrow- Sep 26 '16 at 19:03
  • 1
    If you're asking about not splitting up "sentences", you can't split on spaces. You would have to define a simple syntax for users to enter such data. For example, you might reuqire them to enter using quotes, eg `"i love you" "i hate you"`, so you still split on spaces, but not spaces within quotes. I leave that for you to define and implement. – Bohemian Sep 26 '16 at 19:03
  • 1
    See also http://stackoverflow.com/questions/5554734/what-causes-a-java-lang-arrayindexoutofboundsexception-and-how-do-i-prevent-it – Tunaki Sep 26 '16 at 19:07
  • @scarecrow- nextLine() worked perfect. – Rafat Rifaie Sep 26 '16 at 19:08

1 Answers1

7

Instead of using

part = scanner.next();

https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#next()

use

part = scanner.nextLine();

https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#nextLine()

next, only reads in to the end of the token. where as nextLine reads until the user goes to the next line (presses enter)

jkozlowski
  • 168
  • 9
  • Thank you this is exactly what i was looking for. I will accept the answer once i can. – Rafat Rifaie Sep 26 '16 at 19:07
  • May i ask what you mean by "to the end of token"? – Rafat Rifaie Sep 26 '16 at 19:11
  • 1
    as per the link I provided "A complete token is preceded and followed by input that matches the delimiter pattern" the default delimiter pattern includes spaces. so any word(s) seperated by one the delimiter pattern. – jkozlowski Sep 26 '16 at 19:21