abruzzi26
abruzzi26

Reputation: 169

Splitting a comma and space separated input into two words

I am having difficulty splitting a string of user input into two words. The string is in the format "word1, word2", and I am trying to create two separate strings of word1 and word2. Here is my attempt:

System.out.println("Enter the two words separated by a comma, or 'quit':");

Scanner sc = new Scanner(System.in);

String input = sc.next();

while(!input.equals("quit")){
    input.replaceAll("\\s+","");

    System.out.println(input);  //testing

    int index1 = input.indexOf(",");

    String wordOne = input.substring(0, index1);

    String wordTwo = input.substring(index1+1, input.length() );

    if(wordOne.length()!=wordTwo.length()){
           System.out.println("Sorry, word lengths must match.");
       }

    System.out.println("Enter the two words separated by a comma, or 'quit':"); 

    input = sc.next();
}

This is the output:

Enter the two words separated by a comma, or 'quit':  
leads, golds  
leads,  
Sorry, word lengths must match.  
Enter the two words separated by a comma, or 'quit':  
golds  
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -1  
    at java.lang.String.substring(String.java:1911)  
    at Solver.main(Solver.java:22) //this points to the line "String wordOne = input.substring(0, index1);"  

Could someone please tell me where I am going wrong?

Upvotes: 0

Views: 2113

Answers (2)

user6232354
user6232354

Reputation:

Why don't you try:

input.split(",");

This will give you an String array. From JavaDocs.

public String[] split(String regex)

Splits this string around matches of the given regular expression. This method works as if by invoking the two-argument split method with the given expression and a limit argument of zero. Trailing empty strings are therefore not included in the resulting array.

Update: Since, you are using sc.next() which will take a single word unless it sees a space at which it will terminate the input. You should instead use sc.nextLine() to keep the complete input as user inputs.

next()

public java.lang.String next() Finds and returns the next complete token from this scanner. A complete token is preceded and followed by input that matches the delimiter pattern. This method may block while waiting for input to scan, even if a previous invocation of hasNext returned true.

nextLine()

public java.lang.String nextLine()

Advances this scanner past the current line and returns the input that was skipped. This method returns the rest of the current line, excluding any line separator at the end. The position is set to the beginning of the next line. Since this method continues to search through the input looking for a line separator, it may buffer all of the input searching for the line to skip if no line separators are present.

Upvotes: 2

MS Srikkanth
MS Srikkanth

Reputation: 4241

The problem is that you are using sc.next() instead of sc.nextLine(). I can see that in your input you are entering "leads, gold" where leads, is followed by a space. In this case sc.next() will return just "leads," and not "leads, gold"

Upvotes: 0

Related Questions