简体   繁体   English

替换字符串中的char值(Java)

[英]Replacing a char value in a string (Java)

In my assignment I need to get this output: 在我的作业中,我需要获得以下输出:

Enter a word: house 输入一个词:房子
What letter do you want to replace?: e 您要替换什么字母?:e
With what letter do you wish to replace it? 您想用什么字母代替? w w
The new word is housw. 新词是housw。

_____________________________________________. _____________________________________________。

I got the program to work with this code, but now I need to set while loops. 我让程序可以使用此代码,但现在需要设置while循环。 Here is my current code. 这是我当前的代码。

String word = ""; 字串=“”; Scanner keyboard = new Scanner(System.in); 扫描仪键盘=新的Scanner(System.in);

    System.out.print("Enter a word: " + word);
    word = keyboard.nextLine();

    String readChar = null;
    System.out.print("What letter do you want to replace?: ");
    readChar = keyboard.next();

    String changeChar;
    System.out.print("With what letter do you wish to replace it? ");
    changeChar = keyboard.next();

    keyboard.close();
    System.out.println(word.replaceAll(readChar, changeChar));

    System.out.println();

I need to now make my program output this: 我现在需要使程序输出如下:

Enter a word: house 输入一个词:房子
What letter do you want to replace?: a 您要替换什么字母?:
There is no a in house. 没有房子。
What letter do you want to replace?: b 您要替换什么字母?:b
There is no a in house. 没有房子。
What letter do you want to replace?: e 您要替换什么字母?:e
With what letter do you wish to replace it? 您想用什么字母代替? w w
The new word is housw. 新词是housw。


How would my while loop look to portray this output? 我的while循环如何描绘此输出?

After you read the word and the character you want to replace (plus the character you want to replace it with) you can use replace method from the String class. 读取单词和要替换的字符(以及要替换为的字符)后,可以使用String类中的replace方法。

Here is an example usage (adapt the variable names to your code) 这是用法示例(使变量名适应您的代码)

word = word.replace(letterToReplace, replacementLetter);

So for example 所以举个例子

String word = "aba";
word = word.replace('a', 'c');
System.out.println(word); // Prints out 'cbc'

Also here is an obligatory link to the JavaDoc for the replace method: 另外,这里是到JavaDoc的强制链接,用于replace方法:

http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#replace%28char,%20char%29 http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#replace%28char,%20char%29

I hope you don't mind hard interpretation,Below is the example you can follow the same. 希望您不要介意进行解释,下面是可以遵循的示例。

 String a = "HelloBrother How are you!";
 String r = a.replace("HelloBrother","Brother");

 print.i(r);

If you want to replace all of the letters, you can do it like this (working code): 如果要替换所有字母,可以这样操作(工作代码):

public static void main(String[] args) {

    String word = "";
    Scanner keyboard = new Scanner(System.in);

    System.out.print("Enter a word: " + word);
    word = keyboard.nextLine();

    String readChar = null;
    System.out.print("What letter do you want to replace?: ");
    readChar = keyboard.next();

    String changeChar;
    System.out.print("With what letter do you wish to replace it? ");
    changeChar = keyboard.next();

    keyboard.close();
    System.out.println(word.replace(readChar, changeChar));
}

Okay this is one possible way to implement the edited second part of the question: 好的,这是实现问题的第二部分的一种可能方法:

public static void main(String[] args) {

    String word = "";
    Scanner keyboard = new Scanner(System.in);

    System.out.print("Enter a word: " + word);
    word = keyboard.nextLine();

    boolean done = false;
    do{
        String readChar = null;
        System.out.print("What letter do you want to replace?: ");
        readChar = keyboard.next();

        if(word.contains(readChar)){

            String changeChar;
            System.out.print("With what letter do you wish to replace it? ");
            changeChar = keyboard.next();

            done = true;
            keyboard.close();
            System.out.println(word.replace(readChar, changeChar));
        }
    }
    while(!done);
}

This illustrates what you need to do. 这说明了您需要执行的操作。

import java.util.Scanner;
public class WordScrambler{

    public static void main(String[] args) {

    Scanner keyboard = new Scanner(System.in);

    System.out.print("Enter a word: ");
    String word = keyboard.nextLine();

    System.out.print("What letter do you want to replace?: ");
    char letter = keyboard.next().charAt(0);

    StringBuffer out = new StringBuffer(word);
    System.out.print("With what letter do you wish to replace it? ");
    char changeChar = keyboard.next().charAt(0);

    for (int i=0; i<word.length(); ++i) 
        if(word.charAt(i) == changeChar)
            out.setCharAt(i, changeChar);

    System.out.println("The new word is "+out);
    }
}

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

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