small looping problem(I think)

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Jan 2009
Posts: 17
Reputation: tmoney7566 is an unknown quantity at this point 
Solved Threads: 0
tmoney7566 tmoney7566 is offline Offline
Newbie Poster

small looping problem(I think)

 
0
  #1
Feb 25th, 2009
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
  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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 686
Reputation: sillyboy is on a distinguished road 
Solved Threads: 61
sillyboy's Avatar
sillyboy sillyboy is offline Offline
Practically a Master Poster

Re: small looping problem(I think)

 
1
  #2
Feb 25th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 17
Reputation: tmoney7566 is an unknown quantity at this point 
Solved Threads: 0
tmoney7566 tmoney7566 is offline Offline
Newbie Poster

Re: small looping problem(I think)

 
0
  #3
Feb 25th, 2009
That was it.
if (firstNumber.equals(secondNumber)) worked great. Thank you
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC