0

So I'm making this program for my younger brother and I ran into a problem. The program is suppose to ask for the user's input and then compare it to multiple string values through a series of "if" statements. What happens instead is the user provides their input and then the program instantly terminates. I've been at this for hours and am starting to get pretty ticked about it. Here's the code that I've typed so far:

package package1;
import java.util.Scanner;

public class Psychic_Calculator {
@SuppressWarnings("resource")
public static void main(String[] args) {

Scanner scan = new Scanner(System.in);  
System.out.println("Hello user, please type your name below:"); 
String a = scan.nextLine();
System.out.println("Welcome " + a + ", think of a number. Once you have your  number, type 'okay' below!");

String b = scan.nextLine();

if (b == "okay"){

System.out.println("Now, add '11' to your number and type 'okay' below.");
    }

else if (b == "Okay"){

System.out.println("Please don't capitalize 'okay', try typing it again!");

String c = scan.nextLine();

if (c == "okay"){

System.out.println("Now, add '11' to your number and type 'okay' below.");

String d = scan.nextLine();

if (d == "okay"){

System.out.println("Now, add '2' to your new number, then type 'okay' below.");

String e = scan.nextLine();

if (e == "okay"){

System.out.println("Now, subtract your original number from your new number, then type 'okay' below.");

String f = scan.nextLine();

if (f == "okay"){

System.out.println("Your new number is '13'. Don't even try denying it.");
                    }
                }
            }
        }

        if (c == "Okay"){

        System.out.println("I already told you not to capitalize 'okay', try typing it again, you idiot!");

        String g = scan.nextLine();

        if (g == "okay"){

        System.out.println("Now, add '11' to your number and type 'okay' below.");

        String h = scan.nextLine();

        if (h == "okay"){

        System.out.println("Now, add '2' to your new number, then type 'okay' below.");

        String i = scan.nextLine();

        if (i == "okay"){

        System.out.println("Now, subtract your original number from your new number, then type 'okay' below.");

        String j = scan.nextLine();

        if (j == "okay"){

        System.out.println("Your new number is '13'. Don't even try denying it.");  
                        }
                    }
                }
            }
        }

        if (c != "okay") {

        while (c != "okay") {

        System.out.println("Do you even know how to spell 'okay'?" + "'" + c + "' does not spell 'okay', you moron! Try typing 'okay' again.");

        String n = scan.nextLine();

        if (n == "okay"){

        System.out.println("Finally, you learned how to spell 'okay'. Your vocabulary is now one word larger, you're welcome. Now, please add '11' to your number and then type 'okay'(correctly this time).");

        String k = scan.nextLine();

        if (k == "okay"){

        System.out.println("Now, add '2' to your new number, then type 'okay' below.");

        String l = scan.nextLine();

        if (l == "okay"){

        System.out.println("Now, subtract your original number from your new number, then type 'okay' below.");

        String m = scan.nextLine();

        if (m == "okay"){

        System.out.println("Your new number is '13'. Don't even try denying it.");  
                }

                }
            }

        }

        else {
            System.out.println(a + ", " + "you have failed to type 'okay' too many times! You have no idea how to spell 'okay' you electricutin' motherboarder! Go shove your face in a pile of computer chips and grow a pair!");

                    System.out.println("(of RAM cartriges...I meant to say RAM cartriges).");
                }   
             }
          }
       }
    }
}
Ravi Dhoriya ツ
  • 4,435
  • 8
  • 37
  • 48
Pie King
  • 3
  • 1
  • 1
    Maybe try putting another input at the very end that says "Press Enter to exit the program" – Pradyumna Jan 17 '15 at 06:47
  • You should make the console wait. http://stackoverflow.com/questions/6032118/make-the-console-wait-for-an-user-input-to-close However, some research would be useful for you before posting. – Doro Jan 17 '15 at 06:48
  • Ah, the other thing.. in Java, you need to use String.equals for string comparisons.. not the equality operator – Pradyumna Jan 17 '15 at 06:49
  • Also, that "b" may contain a newline character at the end.. so you may want to clean it up with either Apache Commons StringUtils or with a regex – Pradyumna Jan 17 '15 at 06:51

2 Answers2

0

The problem is how you're comparing the strings. Change b == "okay" to b.equals("okay") Change all the == comparisons to .equals() or .equalsIgnoreCase(). For negations, use (!(c.equals("okay"))

In Java, == will compare primitive types by value but will compare objects by memory address. In other words, when you say b == "okay" its not doing a value comparison, its checking to see whether or not those two objects point to the same memory address, which of course is false.

Update: Just a few things about the way you're going about writing this program.

1) You're creating a lot of string objects needlessly. You're better off re-using one string object until you absolutely need another one. This applies to any object you're using. Try not allocate objects needlessly. 2) Instead of all the if-statements, you can define an array of instructions, like such and condense your code:

String [] instr = {"Add 2", "Add 11", "Subtract Original"};//array of directions
String [] invalidResp = {"Wrong", "You can't spell", "Try Again", "No"};//array of responses to print if user doesnt type 'okay' properly
int instrCounter = 0;//you don't really need this but it helps with readability
String userInput = "";//only string object you'll need =)

while (instrCounter < instr.length){//I couldve just as easily used a for loop instead.
   userInput = scan.nextLine();
   while (!userInput.equals("okay")){
      userInput = scan.nextLine();
      System.out.println(invalidResp[(int)   (Math.random()*invalidResp.length)]);
   //will print a random invalid response from the invalidResp array
      System.out.println(userInput + " is not how you spell okay");
   }//end while
   System.out.println("Great, now, " + instr[instrCounter]);
   instrCounter++;
}//end outer while

Remember, when you write code, you want it to be fairly generic and flexible. The way I wrote my code, I can add to the instr array or add more invalid responses and im not needlessly creating objects. Like I said in the inline comment, I could've just as easily used a for loop for the outer loop but for the sake of readability and making sure you understood what I was doing, I used a while loop as I believe they're more intuitive to read.

Chuck Onwuzuruike
  • 344
  • 1
  • 4
  • 12
  • Wow, this really helped me! I really appreciate the help! I do, however, have two more questions. Is there a way to end the program at a specific point, and also my line of code, System.out.println("Do you even know how to spell 'okay'?" + "'" + c + "' does not spell 'okay', you moron! Try typing 'okay' again."); , keeps giving 'c' the first value the user set it to. Is there a way to set the value to whatever the user provides for each time going through a loop? – Pie King Jan 17 '15 at 23:06
  • I made some edits. You can stop any java program using System.exit(0); but why do you need to do so? – Chuck Onwuzuruike Jan 18 '15 at 00:08
  • Thank you, again! The reason I needed to stop the program is because after the program ended, it would repeat the while loop. – Pie King Jan 18 '15 at 18:23
  • Wow, these tips really helped, I would be totally lost without your assistance! Your format that you posted in your answer really helped me! I tried to vote it up, but I don't have enough reputation. – Pie King Jan 18 '15 at 19:49
0

GAME: while(running) { System.out.println("---------------------------");

continue GAME;

//Name the while loop and then you can break; out.