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))

huangapple go评论72阅读模式
英文:

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 &lt; 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 &gt; 12) {
		throw new IllegalArgumentException(&quot;the format should be XXX-XXX-XXXX&quot;);
    }
    if(phoneNumber.charAt(0) == &quot;x&quot;) {
    	phoneNumber.charAt(0) = &quot;3&quot;;
    }
    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 &lt; ch.length; i++) {
   if(ch[i] == &#39;x&#39; { // single quotes.
        ch[i] = &#39;3&#39;;
   }
}
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(&quot;x&quot;,&quot;3&quot;);

Doesn't work because Strings are immutable. You need to reassign it.

phonenumber = phonenumber.replace(&quot;x&quot;,&quot;3&quot;);

答案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) == &#39;x&#39;

It would help you to go through the Java Documentation

答案3

得分: 0

尝试使用"phoneNumber.replace('x', '3');"。它应该将 'X' 替换为 '3'。

英文:

Try using "phoneNumber.replace(&quot;x&quot;,&quot;3&quot;);" It should replace the 'X's with '3's

huangapple
  • 本文由 发表于 2020年10月4日 04:52:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/64188879.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定