-3

I'm 100% new to Java and am currently doing a course in it. I'm doing a mock up program that tests a users input against a pre-defined code I.E. a password (The numbers from the program Lost), however I am running into difficulties (see below) any help would be greatly appreciated. The main error I'm getting is with the if else statement...

import java.util.Scanner;

public class Lost 
{
   public static void main( String[] args ) 
   {
       String  myNumber;
       myNumber = "4815162342" ; 

      // create Scanner to obtain input from command window
      Scanner input = new Scanner( System.in );


      // prompt user for input and obtain value from user
      System.out.print( "Enter numbers..." );


      // if...else is nested

      if ( result == myNumber );
           // if result numbers,
          System.out.print( "...You've just saved the world for now!!" );
      else
          System.out.print( "BOOM!!!!" );


  } // end main
} // end class Lost

 // put notes here:
Manos Nikolaidis
  • 21,608
  • 12
  • 74
  • 82
c1jericho
  • 13
  • 2
  • 1
    Your `if`-statement is terminated, your input is never read and your `result` never initialized.. have you thought about trying out an IDE with compiler warnings yet? – kasoban Oct 20 '15 at 11:22
  • 1
    Possible duplicate of [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – weston Oct 20 '15 at 11:28
  • So you just banged all this out before running it. Build it up bit by bit. – weston Oct 20 '15 at 11:29
  • @weston: Probably not, this seems more like a syntax problem. The `String` comparison is also a problem of course. – Keppil Oct 20 '15 at 11:29
  • 1
    @Keppil OK, well If I could change reason I'd change to close as simple typographical error. – weston Oct 20 '15 at 11:31
  • @c1jericho Take a look at my solution below. You can accept one solution which helps you most by clicking on the hollow tick beside the answer. You get 2 rep in return. – user3437460 Oct 20 '15 at 11:57

4 Answers4

2

Your code has the following issues :

  1. if ( result == myNumber ); the ; at the end results in an empty body for the if statement. That's why you get an error for else without if. Delete the ;
  2. result is not defined in your code
  3. You are comparing String variables with ==. That will compare if the references are the same. If you want to compare the contents of the String use

    if (result.equals(myNumber))
    
  4. You seem to be missing the part that reads user input. It will look something like

    String result = "";
    if (input.hasNextLine())
        result = input.nextLine();
    
Manos Nikolaidis
  • 21,608
  • 12
  • 74
  • 82
1

Make changes in your code as

import java.util.Scanner;

public class Lost {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

         String  myNumber;
           myNumber = "4815162342" ; 

          // create Scanner to obtain input from command window

          System.out.print( "Enter numbers..." );

          Scanner input = new Scanner( System.in );

          String result = input.next();


          // prompt user for input and obtain value from user



          // if...else is nested

          if ( result.equals(myNumber) )
               // if result numbers,
              System.out.print( "...You've just saved the world for now!!" );
          else
              System.out.print( "BOOM!!!!" );

    }

}
PRINCE GUPTA
  • 43
  • 1
  • 7
1

This is what you can do:

import java.util.Scanner;
public class Lost 
{
    public static void main( String[] args ) 
    {
        String myNumber = "4815162342"; 
        Scanner scn= new Scanner(System.in);

        System.out.print("Enter numbers...");
        String result = scn.nextLine();    //receive input with string (Line 10)

        if (result.equals(myNumber))       //compare strings (input vs myNumber)
            System.out.println("...You've just saved the world for now!!");
        else
          System.out.println("BOOM!!!!");
  }
}

If you choose to receive the input numbers with integer, you will be using scn.nextInt() or Integer.parseInt(scn.nextLine()) at line 10, and comparison of result against muNumber will be (result == myNumber) instead of result.equals(myNumber).

As of now, for your basic understanding, when comparing primitives such as int, you use ==. When comparing objects and Strings, use .equals().

user3437460
  • 17,253
  • 15
  • 58
  • 106
  • Is this code block supposed to be a valid solution for OP's problem? If yes: have you tested it? – Tom Oct 20 '15 at 13:17
0

Get rid of the semi-colon behind the following line:

if ( result == myNumber );

You might want to get used to enclosing single-statement ifs in curly braces as well. I find that it's less error-prone when you need to add multiple statements to such a branch when the braces are already there.

user849924
  • 318
  • 3
  • 9