【问题标题】:String.replace() method not printing full string in javaString.replace() 方法不在java中打印完整的字符串
【发布时间】:2014-12-14 04:12:13
【问题描述】:

我正在尝试为我的计算机科学课做一些作业,但我似乎无法弄清楚这一点。问题是:

编写一个程序,读取一行文本,然后显示该行,但将第一次出现的 hate 更改为 love

这听起来像是一个基本问题,所以我继续写了这个:

import java.util.Scanner;

public class question {

    public static void main(String[] args) 
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Enter a line of text:");

        String text = keyboard.next();

        System.out.println("I have rephrased that line to read:");
        System.out.println(text.replaceFirst("hate", "love"));
    }
}

我希望“我恨你”的字符串输入读取“我爱你”,但它输出的只是“我”。当它检测到我要替换的单词第一次出现时,它会删除字符串的其余部分,除非它是字符串的第一个单词。例如,如果我只输入“hate”,它会将其更改为“love”。我查看了许多站点和文档,我相信我正在遵循正确的步骤。如果有人可以解释我在这里做错了什么,以便它确实显示带有替换单词的完整字符串,那就太好了。

谢谢!

【问题讨论】:

    标签: java eclipse string replace


    【解决方案1】:

    您的错误在于keyboard.next() 通话。这将读取第一个(空格分隔的)单词。您想改用keyboard.nextLine(),因为它读取整行(在这种情况下,这就是您的输入)。

    修改后,您的代码如下所示:

    import java.util.Scanner;
    
    public class question {
    
        public static void main(String[] args) 
        {
            Scanner keyboard = new Scanner(System.in);
            System.out.println("Enter a line of text:");
    
            String text = keyboard.nextLine();
    
            System.out.println("I have rephrased that line to read:");
            System.out.println(text.replaceFirst("hate", "love"));
        }
    }
    

    【讨论】:

      【解决方案2】:

      尝试像这样获取整行,而不仅仅是第一个标记:

      String text = keyboard.nextLine();
      

      【讨论】:

        【解决方案3】:

        keyboard.next() 只读取下一个令牌。 使用keyboard.nextLine() 阅读整行。

        在您当前的代码中,如果您在 replace 之前打印 text 的内容,您将看到只有 I 被用作输入。

        【讨论】:

          【解决方案4】:

          作为替代答案,构建一个 while 循环并查找有问题的单词:

          import java.util.Scanner;
          
          public class question {
          
          public static void main(String[] args)
          {
                // Start with the word we want to replace
                  String findStr = "hate";
                // and the word we will replace it with
                  String replaceStr = "love";
                // Need a place to put the response
                  StringBuilder response = new StringBuilder();
                  Scanner keyboard = new Scanner(System.in);
                  System.out.println("Enter a line of text:");
                  System.out.println("<Remember to end the stream with Ctrl-Z>");
          
                  String text = null;
                  while(keyboard.hasNext())
                  {
                    // Make sure we have a space between characters
                    if(text != null)
                    {
                      response.append(' ');
                    }
                    text = keyboard.next();
                    if(findStr.compareToIgnoreCase(text)==0)
                    {
                      // Found the word so replace it
                      response.append(replaceStr);
                    }
                    else
                    {
                      // Otherwise just return what was entered.
                      response.append(text);
                    }
                  }
          
                  System.out.println("I have rephrased that line to read:");
                  System.out.println(response.toString());
              }
          }
          

          利用扫描器一次返回一个单词。但是,如果单词后面跟着标点符号,则匹配将失败。无论如何,这是我阅读问题时突然出现的答案。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2012-11-28
            • 2010-09-18
            • 2021-10-12
            • 1970-01-01
            • 2019-05-08
            • 2017-07-20
            • 1970-01-01
            • 2011-11-08
            相关资源
            最近更新 更多