作者热门文章
- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
您好,我是这个论坛的新手。我是 Java 的新手。我正在尝试将美国单词转换为英国单词,这样当我输入包含任何美国单词的句子时,输出将是该句子但替换为英国单词。这是我的尝试:
import javax.swing.JOptionPane;
public class PArraystest
{
public static void main(String [] arg)
{
String[] wordUSA = {"Cell", "Elevator", "Fries", "Gasoline", "Faucet"};
String[] wordUK = {"Mobile", "Lift", "Chips", "Petrol", "Tap"};
String changeUK = "";
String sent;
sent = JOptionPane.showInputDialog("What name do you want to search for?");
for (int i = 0; i < wordUSA.length; i++)
{
if (sent.contains(wordUSA[i]))
{
sent.replace((wordUK)[i],(wordUSA)[i]);
//break;
}
}
//if (changeUK.equals(""))
//System.out.println(" was not found.");
//else
System.out.println(sent);
}
}
最佳答案
两件事:
您需要将replace
返回的字符串再次赋值给sent
,否则sent
将保持不变`。
replace
方法是public String replace(char oldChar, char newChar)
,所以oldChar
US word应该放在第一位,后跟英国单词。
这是正确的行:sent = sent.replace(wordUSA[i],wordUK[i]);
关于java - 如何用并行数组替换单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18420034/
我是一名优秀的程序员,十分优秀!