943,800 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 431
  • Java RSS
Feb 25th, 2009
0

small looping problem(I think)

Expand Post »
I am trying to write an applet that receives 2 inputs from a user then opens a window and displays the 2 numbers that were input, along with a message of either "the numbers are equal", or "this number is the largest".

My problem--my program never returns "the numbers are equal". If I put in equal numbers like 76 and 76 it returns "76 is the largest number". Why?

here is my code
Java Syntax (Toggle Plain Text)
  1. import java.awt.Graphics;//program uses class Graphics
  2. import javax.swing.JApplet;//program uses class JApplet
  3. import javax.swing.JOptionPane;//program uses class JOptionPane
  4. import java.math.*;//program uses math.max
  5.  
  6. public class LargerApplet extends JApplet
  7. {
  8. //variables to be used
  9.  
  10. String firstNumber;
  11. String secondNumber;
  12. String result;
  13.  
  14. private double answer;// finds max entered by user
  15.  
  16. //initialize applet by obtaining values from user
  17.  
  18. public void init()
  19. {
  20.  
  21. double number1;//first number to evaluate
  22. double number2;//second number to evaluate
  23.  
  24. //obtain first number from user
  25. firstNumber = JOptionPane.showInputDialog( "Enter first floating-point value" );
  26.  
  27. //obtain second number from user
  28. secondNumber = JOptionPane.showInputDialog( "Enter second floating-point value" );
  29.  
  30. //convert numbers from type String to type double
  31. number1 = Double.parseDouble( firstNumber );
  32. number2 = Double.parseDouble( secondNumber );
  33.  
  34.  
  35.  
  36. answer = Math.max( number1, number2);
  37.  
  38. }//end method init
  39.  
  40. //draw results in a rectangle on applet's background
  41. public void paint( Graphics g )
  42. {
  43. super.paint( g );//call superclass version of method paint
  44.  
  45. //draw rectangle starting from (15, 10) that is 270
  46. //pixels wide and 20 pixels tall
  47. g.drawRect( 15, 10, 270, 50 );
  48.  
  49. //draw results as a String at ( 25, 25 )
  50. g.drawString( "Your first number was-- " + firstNumber, 25,25 );
  51. g.drawString( "Your second number was-- " + secondNumber ,25,35 );
  52.  
  53. if ( firstNumber == secondNumber )
  54. {
  55. g.drawString ( "--Your numbers are equal--", 25,45 );
  56.  
  57. }//end if
  58.  
  59.  
  60. else
  61. {
  62.  
  63. g.drawString ( answer + "--Is the larger number--", 25, 55 );
  64.  
  65. }//end else
  66.  
  67.  
  68.  
  69.  
  70. }//end method paint
  71.  
  72. }//end class IsLargerApplet
Last edited by tmoney7566; Feb 25th, 2009 at 7:05 pm.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
tmoney7566 is offline Offline
17 posts
since Jan 2009
Feb 25th, 2009
0

Re: small looping problem(I think)

firstNumber and secondNumber are Strings (I am pretty sure you know this). since they are strings, == will only evaluate true if they are the same object. you want to use .equals to compare Strings in the way you intend.
Reputation Points: 85
Solved Threads: 64
Practically a Master Poster
sillyboy is offline Offline
686 posts
since Mar 2007
Feb 25th, 2009
0

Re: small looping problem(I think)

That was it.
if (firstNumber.equals(secondNumber)) worked great. Thank you
Reputation Points: 10
Solved Threads: 0
Newbie Poster
tmoney7566 is offline Offline
17 posts
since Jan 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: XML in Java
Next Thread in Java Forum Timeline: beginner Java - starter programs





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC