Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/334.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.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_String_For Loop_Char - Fatal编程技术网

Java 字符串操作替换和反转(用于循环)

Java 字符串操作替换和反转(用于循环),java,string,for-loop,char,Java,String,For Loop,Char,我在以不同代码行显示输出时遇到问题,我如何才能做到这一点 package replaceandreverse; import java.util.Scanner; public class ReplaceAndReverse { public static void main(String args[]) { //******************************************************************************

我在以不同代码行显示输出时遇到问题,我如何才能做到这一点

package replaceandreverse;

import java.util.Scanner;

public class ReplaceAndReverse {


 public static void main(String args[])
    { 
    //****************************************************************************************************************************************    

         String newsen="";//parameter 1 will contain the new sentence
         String temp="";//parameter 2 will be use a temporary memory to save the replace string process

        Scanner src = new Scanner(System.in);      //instantiate scanner
        System.out.println("Enter the sentence:"); //display Enter sentence
        String s = src.nextLine();                 //take user input also string s is the 3rd paramter which saves the string inputed

    //****************************************************************************************************************************************

        System.out.println("Enter the word to be replaced:"); //display Enter the word to be replaced
        String replace = src.next();                          // take user input
        System.out.println("Enter the word with which it is to be replaced:"); //Display Enter the word with which it is to be replaced
        String replacewith = src.next();        //take user input for the letter to with which to replace the previously inputed word.
        s=s+" ";//add to s

    //****************************************************************************************************************************************  
        //loop to perform the replace process
        // is on for loop which contains nested if-else loop
        for(int i=0;i<s.length();i++)
        {
            char x=s.charAt(i);// turning string to char
            if(x!=' ')
            {
                temp=temp+x;
            }//end of if loop 1
            else
            {
                if(replace.equals(temp))
                {
                    newsen=newsen+replacewith;
                }//end of if loop 2
                else
                {
                    newsen=newsen+temp;
                }//end of else loop 1

                newsen=newsen+" ";
                temp="";

            }//end of else loop 2

        }//end of for loop for the replace process

    //****************************************************************************************************************************************    

        System.out.println("The new sentence is :"+" "+newsen); //display the new sentence with the new sentence

        System.out.print("The reverse sentence is :"); //display the reverse sentence is

    //****************************************************************************************************************************************

        //loop to print the words in reverse
        for(int i=newsen.length()-1; i>=0; i-- )
        {

         System.out.print(newsen.charAt(i)); // display reverse sentence 

        }//end of loop to reverse string

    //****************************************************************************************************************************************

    }//end of main
}//end of class

使用nextLine函数而不是next函数。这将获取用户输入的所有内容。因此,如果您执行以下操作

System.out.println("Enter the word to be replaced:"); //display Enter the word to be replaced
String replace = src.nextLine();  // take the entire user input
System.out.println("Enter the word with which it is to be replaced:"); //Display Enter the word with which it is to be replaced
String replacewith = src.nextLine(); // take the entire user input
您得到了正确的输出

Enter the sentence:
my name is ryan
Enter the word to be replaced:
name
Enter the word with which it is to be replaced:
pizzapie
The new sentence is : my pizzapie is ryan 
The reverse sentence is : nayr si eipazzip ym

你能解释一下你想要完成什么吗?当你从用户那里获取输入时,使用src.nextLine而不是src.next。如果你使用Java API中已有的方法,你的代码会简单得多:String类有replacechar,char方法,StringBuilder有方法来反转它。你有什么问题?