java的问题...如何解决.

来源:百度知道 编辑:UC知道 时间:2024/05/26 01:08:56
如何从键盘中输入的文本中的字符串”word”替换为字符串"world",并删掉所有字符"this"
现在就25,好的回头补分。. 写了个..出错了..JAVA高手帮忙解决下.给出详细代码更好‘‘‘

public static void main(String[] args)
{
String x =new String("word");
String text=" ";
int g=0;
String str =" ";
System.out.println("请输入文本.输人\"end\"结束");
BufferedReader in =new BufferedReader(new InputStreamReader(System.in));
while(!(str=in.readLine().equalsIgnoreCase("end")));
{
text+=str;
}
int i=0;
while(i<=text.length()-4)
{
if(text.charAt(i)=='w')
{
String a=text.substring(i,i+4);
if(a.equalsIgnoreCase(word))
{
g++;
public String replace("word","world");
i=i+4;
}
else i++;
}

用replaceAll就可以批量替换了。有什么问题继续补充,我再修改
代码如下:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Beetle {
public static void main(String args[]) throws Exception {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String str = in.readLine();//取得输入的字符串
str = str.replaceAll("word", "world");//将字符串中全部的word替换成world
str = str.replaceAll("this", "");");//将字符串中全部的this替换成空格

System.out.println(str);
}
}

String x =new String("word"); 显得没有用喽

public static void main(String[] args)
{
String text="";
int g=0;
String str ="";
System.out.println("请输入文本.输人\"end\"结束");
BufferedReader in =new BufferedReader(new InputStreamReader(System.in));
try{
while(!((str=in.readLi