简体   繁体   English

将逗号和空格分隔的输入分成两个词

[英]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. 该字符串的格式为“ word1,word2”,我正在尝试创建两个单独的字符串,分别为word1和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? 有人可以告诉我我要去哪里错吗?

Why don't you try: 您为什么不尝试:

input.split(",");

This will give you an String array. 这将为您提供一个String数组。 From JavaDocs. 从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. 更新:因为,您正在使用sc.next() ,它将使用一个单词,除非它看到一个空格来终止输入。 You should instead use sc.nextLine() to keep the complete input as user inputs. 您应该改用sc.nextLine()将完整的输入保留为用户输入。

next()

public java.lang.String next() Finds and returns the next complete token from this scanner. public java.lang.String next()查找并返回此扫描器的下一个完整令牌。 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. 即使先前调用hasNext返回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. 由于此方法继续在输入中搜索以寻找行分隔符,因此,如果不存在行分隔符,它可以缓冲所有要搜索的输入以跳过该行。

The problem is that you are using sc.next() instead of sc.nextLine() . 问题是,你正在使用sc.next()代替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" 在这种情况下, sc.next()将仅返回“ leads”,而不返回“ leads,gold”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM