0
Scanner sc = new Scanner (System.in);
String enter = new String ("Enter your name brave soul: ");
System.out.println (enter);
String name = sc.next();
System.out.println ("Your name is: " + name + "? Y/N");
boolean y = true; 
boolean n = false;
String yesorno = sc.next();

String intro1 = new String ("Welcome to Urieka! The objective of this game is quite simple, just find the key to escape.");


if (true) {System.out.println (intro1);}   //here
else if (false) {System.out.println (enter);} //here

I'm having a problem, I want to print intro1 if the user inputs y and I want the prompt to enter the name if they input it incorrectly. Its currently ONLY printing intro1 regardless if I input y or no.

Furthermore, is there a way for me to run that scanner again because I assume that if I DO get this working and the user inputs n/false, then it would just print "Enter your name brave soul" and nothing else. Would I somehow have to add a scanner into the statement on the else if line?

METEORITES
  • 43
  • 1
  • 6

4 Answers4

3
if (true) {System.out.println (intro1);}   //here

this is always true and will always run. The else will likewise never run.

You want instead

if ("y".equalsIgnoreCase(yesorno)) {
   //...
}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • Oh..I was under the impression that true was only true if I said so. Thats why I set values for them, I get it thought, I think, ill attempt the edit now. – METEORITES Sep 27 '14 at 19:24
3

Well... true is always true so

if (true) { ... } 

will always be executed. You should do something like :

System.out.println("y".equalsIgnoreCase(yesorno) ? intro1 : enter);
Dici
  • 25,226
  • 7
  • 41
  • 82
  • There's a beginning to everything ! If I were a beginner I would be curious about this syntax that enables to do it in a single line and I would learn something. – Dici Sep 27 '14 at 19:25
  • I understand. So then this means that if I got rid of the scanner input on the yesorno variable the subsequent if statement should not run? This leads to my next question however, would I have to re-input the scanner function if the user inputs n? – METEORITES Sep 27 '14 at 19:30
  • @METEORITES : You should have a loop which will exit only when the user have inputted something valid. In each loop, use the `Scanner` to read user input. – Dici Sep 27 '14 at 19:33
  • I was thinking about using recursion instead, but a loop would be much more simplistic I'm sure. @Dici – METEORITES Sep 27 '14 at 19:34
  • @METEORITES : Recursivity has a cost, it generates more method calls and it's worse if the recursive function takes parameters, because each call forces to store a local context (unless the method is tail-recursive). Never consider recursivity as an option if there is no good algorithmic reason for it. – Dici Sep 27 '14 at 19:39
2

You never change these booleans:

boolean y = true;
boolean n = false;

Also try to avoid usage of if(true), as mentioned in previous post:

if (true) {System.out.println (intro1);}   //here

It is not mandatory to use a constructor when instantiating a String object:

String enter = new String("Enter your name brave soul: ");
// IS THE SAME AS <=>
String enter = "Enter your name brave soul: ";

Here is my solution of your problem:

Scanner scanner = new Scanner(System.in);
    boolean correctName = false;
    String name = "";

    while(!correctName){ //== Will run as long "correctName" is false. 
        System.out.println("Enter your name brave soul: ");
        name = scanner.next();
        System.out.println("Your name is: " + name + "? Y/N");
        String yesorno = scanner.next();
        correctName = "Y".equalsIgnoreCase(yesorno);  //== changes the boolean depending on the answer
    }
    System.out.println("Welcome to Urieka" + name + "! The objective of this game is quite simple, just find the key to escape.");
ChristopherMortensen
  • 1,093
  • 2
  • 8
  • 10
  • This is excellent it did everything I wished. It seems that me trying to keep the intro as string variables caused more problems than solutions – METEORITES Sep 27 '14 at 20:02
0

If (true) means that it will always enter the if condition and it will always print intro01 as it's doing. The else condition will never be reached.

your condition should be something like:

if("y".equalsIgnoreCase(yesorno))
    System.out.println (intro1);
else
    System.out.println (enter);
Alboz
  • 1,833
  • 20
  • 29