在Java实践中修改字符串

在Java实践中修改字符串,java,string,methods,Java,String,Methods,嗨,我在上面的代码上遇到了问题,它;这是一个实践问题,但“将直线从空间变为结尾的子字符串”确实让我困惑。我知道如何找到空间的索引,并从开始到索引打印字符串 预期产出: public static void displayWords(String line) { // while line has a length greater than 0 // find the index of the space. // if there is one, // print t

嗨,我在上面的代码上遇到了问题,它;这是一个实践问题,但“将直线从空间变为结尾的子字符串”确实让我困惑。我知道如何找到空间的索引,并从开始到索引打印字符串

预期产出:

public static void displayWords(String line)
{
  // while line has a length greater than 0
  //   find the index of the space.
  //   if there is one,
  //    print the substring from 0 to the space
  //    change line to be the substring from the space to the end
  //   else
  //    print line and then set it equal to the empty string

}
  line = "I love you";
  System.out.printf("\n\n\"%s\" contains the words:\n", line );
  displayWords(line);

另一种方法

I
love
you
<将其用作参考。

您需要使用
substring()
方法。检查:

公共字符串子字符串(int-beginIndex,int-endIndex)
返回作为此字符串的子字符串的新字符串。子字符串从指定的
beginIndex
开始,并延伸到索引
endIndex-1
处的字符。因此,子字符串的长度是
endIndex beginIndex
示例:

  • “汉堡包”。子字符串(4,8)
    返回
    “敦促”
  • “微笑”。子字符串(1,5)
    返回
    “英里”
<>所以,考虑一下:

public static void displayWords(String line)
{
  Scanner s = new Scanner(line)
  while (s.hasNext())
  {
    System.out.println(s.next())
  }
}

你有足够的时间。。。今晚我感到很慷慨。因此,解决方案如下:

public static void displayWords(String line) {
    while(line.length() > 0) { // This is the first part (the obvious one)
        /*
           You should do something here that tracks the index of the first space 
           character in the line. I suggest you check the `charAt()` method, and use 
           a for() loop.
           Let's say you find a space at index i
         */
         System.out.println(line.substring(0,i)); // This prints the substring of line
                                                  // that goes from the beginning
                                                  // (index 0) to the position right
                                                  // before the space
         /*
           Finally, here you should assign a new value to line, that value must start
           after the recently found space and end at the end of the line.
           Hint: use substring() again, and remember that line.lengh() will give you
                 the position of the last character of line plus one.
          */
    }
}
publicstaticvoiddisplaywords(字符串行){
int i;//您需要这个
while(line.length()>0){//这是第一部分(显而易见的一部分)
/*
在这里,检查每个字符,如果找到空格,则中断循环
*/
对于(i=0;i

在再次阅读您的问题后,我意识到它应该是一个递归解决方案。。。下面是递归方法:

public static void displayWords(String line) {
    int i; // You'll need this
    while(line.length() > 0) { // This is the first part (the obvious one)
        /*
           Here, check each character and if you find a space, break the loop
         */
        for(i = 0; i < line.length(); i++) {
            if(line.charAt(i) == ' ') // If the character at index i is a space ...
                break;                // ... break the for loop
        }
        System.out.println(line.substring(0,i)); // This prints the substring of line
                                                 // that goes from the beginning
                                                 // (index 0) to the position right
                                                 // before the space
        /*
          Here, the new value of line is the substring from the position of the space
          plus one to the end of the line. If i is greater than the length of the line
          then you're done.
         */
        if(i < line.length())
            line = line.substring(i + 1, line.length());
        else
            line = "";
    }
}
publicstaticvoiddisplaywords(字符串行){
int i;
对于(i=0;i0)
显示字(行);
}

您是否查看了
substring()
方法?我无法添加其他方法,这是一个实践问题,您必须使用提供给您的代码段。他的意思是我必须能够使用任何其他字符串,如“John God the store”,预期输出为“John God the store”。我可以使用substring方法打印单词,但它不适用于其他字符串。您可以通过扫描仪运行字符串。我认为OP是一个初学者,虽然这是一种非常简单的方法来完成所需的操作,但并不完全符合OP的需要。有时,在学习时,这样做是一件好事正确欣赏“简单、高效、干净、优雅的方式”的“艰难、不太有效的方式”
public static void displayWords(String line) {
    int i;
    for(i = 0; i < line.lengh(); i++) {
        if(line.charAt(i) = ' ')
            break;
    }
    System.out.println(line.substring(0, i);
    if(i < line.lengh())
        line = line.substring(i + 1, line.lengh();
    else
        line = "";
    if(line.lengh() > 0)
        displayWords(line);
}