Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
方法在java中不打印完整字符串_Java_Eclipse_String_Replace - Fatal编程技术网

方法在java中不打印完整字符串

方法在java中不打印完整字符串,java,eclipse,string,replace,Java,Eclipse,String,Replace,我正试图为我的计算机科学课做一些家庭作业,但我似乎不明白这一点。问题是: 编写一个程序,读取一行文本,然后显示该行,但第一次出现的恨变为爱 这听起来像是一个基本问题,所以我继续写了这个: import java.util.Scanner; public class question { public static void main(String[] args) { Scanner keyboard = new Scanner(System.in);

我正试图为我的计算机科学课做一些家庭作业,但我似乎不明白这一点。问题是:

编写一个程序,读取一行文本,然后显示该行,但第一次出现的恨变为爱

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

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"));
    }
}
我希望输入一个字符串“我恨你”读作“我爱你”,但它只输出“我”。当它检测到我试图替换的单词第一次出现时,它会删除字符串的其余部分,除非它是字符串的第一个单词。例如,如果我只输入“恨”,它会将其更改为“爱”。我看过很多网站和文档,我相信我遵循了正确的步骤。如果有人能解释我在这里做错了什么,这样它就可以用替换的单词显示完整的字符串,那太棒了


谢谢大家!

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

String text = 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"));
    }
}
keyboard.next()
只读取下一个令牌。 使用
keyboard.nextLine()
读取整行内容。


在当前代码中,如果在
替换
之前打印
文本
的内容,您将看到只有
I
被作为输入。

作为替代答案,构建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());
    }
}
import java.util.Scanner;
公开课问题{
公共静态void main(字符串[]args)
{
//从我们要替换的单词开始
字符串findStr=“hate”;
//我们将用这个词来代替它
String replaceStr=“爱”;
//需要一个地方来放置响应
StringBuilder响应=新建StringBuilder();
扫描仪键盘=新扫描仪(System.in);
System.out.println(“输入一行文本:”);
System.out.println(“”);
字符串文本=空;
while(keyboard.hasNext())
{
//确保字符之间有空格
如果(文本!=null)
{
响应。追加(“”);
}
text=键盘.next();
如果(findStr.compareToIgnoreCase(文本)==0)
{
//找到了这个词,所以替换它
响应。追加(replaceStr);
}
其他的
{
//否则只需返回输入的内容。
答复.追加(文本);
}
}
println(“我已经将这一行改写为:”);
System.out.println(response.toString());
}
}
利用扫描仪一次返回一个单词的优势。但是,如果单词后面有标点符号,则匹配将失败。不管怎样,当我读到这个问题时,这就是我脑海中闪现的答案