Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String.replace result is ignored?

So i'm using IntelliJ and the replace buzzword is highlighted. Most of the tips are over my head so i ignore them, but what i got for this one was that the result of string.replace is ignored. Why?

would i need something like ( string = string.replace(string.charAt(i));)?

import java.util.Scanner;

public class PhoneNumberDecipher {
    public static String phoneNumber;
    public static String Decipher(String string) {
        string = phoneNumber;
        for(int i =0; i<=phoneNumber.length(); i++) {
            if(string.equalsIgnoreCase("A")
               ||string.equalsIgnoreCase("B")
               ||string.equalsIgnoreCase("C")) {
                string.replace(string.charAt(i),'2')
            }
            else if(string.equalsIgnoreCase("D")
                    ||string.equalsIgnoreCase("E")
                    ||string.equalsIgnoreCase("F")) {
                string.replace(string.charAt(i),'3');
            }
            else if(string.equalsIgnoreCase("G")
                    ||string.equalsIgnoreCase("H")
                    ||string.equalsIgnoreCase("I")) {
                string.replace(string.charAt(i),'4');
            }
            else if(string.equalsIgnoreCase("J")
                    ||string.equalsIgnoreCase("K")
                    ||string.equalsIgnoreCase("L")) {
                string.replace(string.charAt(i),'5');
            }
            else if(string.equalsIgnoreCase("M")
                    ||string.equalsIgnoreCase("N")
                    ||string.equalsIgnoreCase("O")) {
                string.replace(string.charAt(i),'6');
            }
            else if(string.equalsIgnoreCase("P")
                    ||string.equalsIgnoreCase("Q")
                    ||string.equalsIgnoreCase("R")
                    || string.equalsIgnoreCase("S")) {
                string.replace(string.charAt(i),'7');
            }
            else if(string.equalsIgnoreCase("T")
                    ||string.equalsIgnoreCase("U")
                    ||string.equalsIgnoreCase("V")) {
                string.replace(string.charAt(i),'8');
            }
            else if(string.equalsIgnoreCase("W")
                    ||string.equalsIgnoreCase("X")
                    ||string.equalsIgnoreCase("Y")
                    ||string.equalsIgnoreCase("Z")) {
                string.replace(string.charAt(i),'9');
            }
        }

        return string;
    }

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        System.out.println("Please Enter a Phone Number you Wish to Decipher...");
        phoneNumber = input.nextLine();

        System.out.print(Decipher(phoneNumber));
    }
}
like image 452
user6069027 Avatar asked Dec 07 '22 23:12

user6069027


2 Answers

String objects are immutable.

From the docs:

public String replace(char oldChar,char newChar)

Returns a string resulting from replacing all occurrences of oldChar in this string with newChar.

Hope this helps.

like image 105
Imposter Avatar answered Dec 28 '22 11:12

Imposter


IntelliJ is complaining that you're calling a method whose only effect is to return a value (String.replace) but you're ignoring that value. The program isn't doing anything at the moment because you're throwing away all the work it does.

You need to use the return value.

There are other bugs in there too. You might be able to progress a little further if you use some of this code:

StringBuilder convertedPhoneNumber = new StringBuilder();
// Your loop begins here
char curCharacter = phoneNumber.charAt(i);
if (curCharacter == 'a') {
  convertedPhoneNumber.append("2");
}
// More conditional logic and rest of loop goes here.

return convertedPhoneNumber.toString();
like image 44
Paul Hicks Avatar answered Dec 28 '22 10:12

Paul Hicks