String.replace result is ignored?

Viewed 17560

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));
    }
}
5 Answers

I had the same problem, but i did it like this:

String newWord = oldWord.replace(oldChar,newChar); 

use that statement string = string.replace(string.charAt(i));

Why? String is an immutable object. Look at this thread to get a complete explanation. This a fundemental part of Java, so make sure you learn it well.

Related