0

i am writing caeser cipher game. So When i start decrypt the cipher text, characters are written with uppercase. I need to make equal the uppercase string with the original one with the lowercase so that game can be finish. With this code it always continue even i finish decrypt the same string(a quote). Anyone knows how to do it ? str1, shr1 = cipher text. User = 1 or 2 is the mods of the game so dont mind it.

       String chose=str1;
        while (user==1) {   
            do {
                System.out.println("Please enter a letter what you want to replace: ");
                String choose1 = in.next();
                System.out.println("Please enter a letter what you want to replace with it: ");
                String choose2 = in.next();

                chose = chose.replace(choose1,choose2.toUpperCase());
                System.out.println(chose);       
            } while (chose!=str1);
        }

        String chose1 = shr1;
        while (user==2) {           
            do {
                System.out.println("Please enter a letter what you want to replace: ");
                String choose1 = in.next();
                System.out.println("Please enter a letter what you want to replace with it: ");
                String choose2 = in.next();

                chose1 = chose1.replace(choose1,choose2.toUpperCase());
                System.out.println(chose1);          
            } while (chose1!=shr1);
        }
asap
  • 9
  • 4
  • 1
    Don't compare Strings using `==`or `!=`. Use the `equals(...)` or the `equalsIgnoreCase(...)` method instead. Understand that `==` checks if the two object references are the same which is not what you're interested in. The methods on the other hand check if the two Strings have the same characters in the same order, and that's what matters here – Hovercraft Full Of Eels Feb 20 '20 at 22:22
  • Replace `chose!=str1` with `!chose.equals(str1)`. `==` or `!=` compare the references, not the content of the strings. – Arvind Kumar Avinash Feb 20 '20 at 22:23

0 Answers0