Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Scanner cannot be used as variable in "if" Statements???

  1. #1
    Junior Member
    Join Date
    May 2014
    Location
    Java, TX
    Posts
    18
    My Mood
    Where
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Scanner cannot be used as variable in "if" Statements???

    I created a variable for the scanner called serena. Serena variable is equal to what the user inputs. My if statement says that if the answer the user enters is not equal to the actual answer then it is to display "wrong". It is a basic math game I am working on. NetBeans is telling me that I cannot use the scanner in an if statement? I am confused. Please take a look and help.

    package pkgnew;

    import java.util.Scanner;

    public class New{
    public static void main(String args[]){

    Scanner serena = new Scanner(System.in);
    double fnum, snum, answer;
    fnum = 6;
    snum = 6;
    answer = fnum + snum;
    System.out.println("6 + 6");
    serena.nextLine();
    if (serena = answer){
    System.out.println("Correct!");
    }else
    System.out.println("Wrong!The answer is:" answer);{

    }
    }

    --- Update ---

    Do I have to define Serena as whatever number the user inputs? If so, how?


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,043
    My Mood
    Mellow
    Thanks
    184
    Thanked 648 Times in 635 Posts

    Default Re: Scanner cannot be used as variable in "if" Statements???

    Please post your code correctly.

    '=' is an assignment operator. '==' is a comparison operator.

  3. #3
    Junior Member
    Join Date
    May 2014
    Location
    Java, TX
    Posts
    18
    My Mood
    Where
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Scanner cannot be used as variable in "if" Statements???

    Sorry for the mistake, greg. The problem is the scanner and double being incomparable...

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,043
    My Mood
    Mellow
    Thanks
    184
    Thanked 648 Times in 635 Posts

    Default Re: Scanner cannot be used as variable in "if" Statements???

    Assign what is collected by the Scanner object to a variable and then compare that. Something like:
    double userAnswer = serena.nextDouble();
    if ( userAnswer == answer ) . . . etc.

  5. The Following User Says Thank You to GregBrannon For This Useful Post:

    kunimaro15689 (May 18th, 2014)

  6. #5
    Junior Member
    Join Date
    May 2014
    Location
    Java, TX
    Posts
    18
    My Mood
    Where
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Scanner cannot be used as variable in "if" Statements???

    Thank you, Greg. It worked!

  7. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,043
    My Mood
    Mellow
    Thanks
    184
    Thanked 648 Times in 635 Posts

    Default Re: Scanner cannot be used as variable in "if" Statements???

    You're welcome.

Similar Threads

  1. Replies: 2
    Last Post: June 22nd, 2013, 10:30 AM
  2. Replies: 8
    Last Post: May 6th, 2013, 07:53 AM
  3. [SOLVED] "null" in my output :( Scanner problem?
    By crys in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 2nd, 2012, 08:05 PM
  4. error with "Scanner" class in basic calculator program.
    By wheezebeez in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 2nd, 2012, 09:49 AM
  5. Bizarre Issue with Scanner Failing to "notice" rest of file
    By nitrogenFingers in forum File I/O & Other I/O Streams
    Replies: 9
    Last Post: November 14th, 2011, 10:56 PM

Tags for this Thread