英文:
I want to change the string of letters xxx-xxx-xxxx into 333-333-3333 I tried this but getting an error in the if(phoneNumber.charAt(0))
问题
public class ABCTelephoneTranslator {
    public static String translate(String phoneNumber) {
        char[] ch = new char[phoneNumber.length()];
        for (int i = 0; i < phoneNumber.length(); i++) {
            ch[i] = phoneNumber.charAt(i);
        }
        for (char c : ch) {
            System.out.println(c);
        }
        System.out.println(ch.length);
        if (ch.length > 12) {
            throw new IllegalArgumentException("the format should be XXX-XXX-XXXX");
        }
        if (phoneNumber.charAt(0) == 'x') {
            ch[0] = '3';
        }
        return new String(ch);
    }
}
英文:
public class ABCTelephoneTranslator {
public static String translate(String phoneNumber) {
	 // Creating array of string length 
    char[] ch = new char[phoneNumber.length()]; 
    // Copy character by character into array 
    for (int i = 0; i < phoneNumber.length(); i++) { 
        ch[i] = phoneNumber.charAt(i); 
    } 
   
    // Printing content of array 
    for (char c : ch) { 
        System.out.println(c); 
    }
    System.out.println(ch.length);
    if(ch.length > 12) {
		throw new IllegalArgumentException("the format should be XXX-XXX-XXXX");
    }
    if(phoneNumber.charAt(0) == "x") {
    	phoneNumber.charAt(0) = "3";
    }
    return phoneNumber;
}
I need to change the string of letters xxx-xxx-xxxx into 333-333-3333 how do i do that I tried this but getting an error where it says if(phoneNumber.charat(0) == "x") and below that please help me resolve this thank you
答案1
得分: 6
以下是翻译好的部分:
有更简单的方法来完成这个任务,但为了回答你的具体问题,你需要执行以下步骤。在你的方法末尾:
for (int i = 0; i < ch.length; i++) {
    if (ch[i] == 'x') { // 单引号。
        ch[i] = '3';
    }
}
return new String(ch);
另外,要获取字符数组,你可以这样做。
char ch[] = phonenumber.toCharArray();
与其他人告诉你的相反,
phonenumber.replace("x", "3");
不起作用,因为 String 是不可变的。你需要重新赋值它。
phonenumber = phonenumber.replace("x", "3");
英文:
There are easier ways to do this but to answer your specific question you need to do the following. At the end of your method:
for (int i = 0; i < ch.length; i++) {
   if(ch[i] == 'x' { // single quotes.
        ch[i] = '3';
   }
}
return new String(ch);
Also, to get the array of characters you can do.
char ch[] = phonenumber.toCharArray();
And contrary to what others have told you,
phonenumber.replace("x","3");
Doesn't work because Strings are immutable.  You need to reassign it.
phonenumber = phonenumber.replace("x","3");
答案2
得分: 1
charAt函数返回一个字符。您不能将其用于与字符串进行比较。您需要将其与字符进行比较。因此,您的if条件应为phoneNumber.charAt(0) == 'x'。
阅读Java文档会对您有所帮助。
英文:
charAt functionr returns a character. You cannot use it to compare it to a string. You need to compare it to a character. So, your if-condition should be phoneNumber.charAt(0) == 'x'
It would help you to go through the Java Documentation
答案3
得分: 0
尝试使用"phoneNumber.replace('x', '3');"。它应该将 'X' 替换为 '3'。
英文:
Try using "phoneNumber.replace("x","3");" It should replace the 'X's with '3's
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论