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 4 of 4

Thread: Scanner Issues: Read for more detail, thanks.

  1. #1
    Member Staticity's Avatar
    Join Date
    Jul 2011
    Location
    Texas
    Posts
    105
    My Mood
    Inspired
    Thanks
    3
    Thanked 5 Times in 5 Posts

    Default Scanner Issues: Read for more detail, thanks.

    while(a < amount){
     
    int num1 = 2 + (int)(Math.random()*23);
    int num2 = 2 + (int)(Math.random()*23);
    int num3 = 2 + (int)(Math.random()*23);
    int num4 = 2 + (int)(Math.random()*23);
     
    String answer = null;
     
    if(num1*num4 > num2*num3){
    	answer = ""+num1+"/"+num2+"";
    }
     
    else if(num1*num4 < num2*num3){
    	answer = ""+num3+"/"+num4+"";
    }
     
    System.out.println("The larger of " + num1 + "/" + num2 + " or " + num3 + "/" + num4 + " is");
     
    	String reply = scan.next();
     
    	if(reply == answer){
    	System.out.println("Correct!");
    	correct++;
    	score += 5;
    }
    	else{
    	System.out.println("Wrong!");
    	wrong++;
    	score -= 9;
    }
    	amount--;
    }

    This piece of my code determines whether a fraction is greater than another using cross multiplying. The user must decide which fraction is the larger value. In order to place "/" into the answer, I converted the "reply" and "answer" variables into Strings. However, the output comes out like so:
    ---------------------------------
    The larger of 4/3 and 10/3 is
    10/3
    Wrong!
    ---------------------------------
    I even tried adding - "System.out.println(answer); - and placed that exact answer into the line. But my answer was still wrong. Can anyone help me understand some more about Scanners, and how they work? I'm not quite sure why the program keeps returning "Wrong!" to me when I am clearly putting in the correct answer. Thanks in advance!
    Last edited by Staticity; September 7th, 2011 at 08:41 PM.
    Simplicity calls for Complexity. Think about it.


  2. #2
    Member Staticity's Avatar
    Join Date
    Jul 2011
    Location
    Texas
    Posts
    105
    My Mood
    Inspired
    Thanks
    3
    Thanked 5 Times in 5 Posts

    Default Re: Scanner Issues: Read for more detail, thanks.

    I solved the issue by changing the "==" to .equals().
    Simplicity calls for Complexity. Think about it.

  3. #3
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Scanner Issues: Read for more detail, thanks.

    Concatenating the empty Strings is an abuse of your powers.
    Improving the world one idiot at a time!

  4. #4
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Scanner Issues: Read for more detail, thanks.

    Quote Originally Posted by Staticity View Post
    I solved the issue by changing the "==" to .equals().
    This is a common error
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

Similar Threads

  1. Reading user input from the console with the Scanner class
    By JavaPF in forum Java SE API Tutorials
    Replies: 3
    Last Post: September 7th, 2011, 03:09 AM
  2. Can scanner read the specified line?
    By ice in forum File I/O & Other I/O Streams
    Replies: 7
    Last Post: January 28th, 2011, 09:35 AM
  3. Reading a file line by line using the Scanner class
    By JavaPF in forum File Input/Output Tutorials
    Replies: 0
    Last Post: April 17th, 2009, 07:34 AM
  4. Reading a file line by line using the Scanner class
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 0
    Last Post: April 17th, 2009, 07:34 AM
  5. Reading user input from the console with the Scanner class
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 0
    Last Post: November 11th, 2008, 02:47 PM