Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Adding two user inputs into one variable. Code included  RSS feed

 
Johnny Wayne
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to add two variables that are initialized from user inputs. I have two projects going; one using the console for inputs and outputs, and the one you see below for JOptionPane input and outputs. The console project works fine, so I am certain it has something to do with the capture of the user inputs. I highlighted the line of code that is giving the incorrect sum. It should return 30, but it continuously comes up with 20, which messes the other metrics up.
Also if someone wouldn't mind just showing an example of the HTML formatting to combine those bottom three JOptionPanes into one.
Thanks in advance.
 
Norm Radder
Ranch Foreman
Posts: 2240
28
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the capture of the user inputs

You need to read the API doc for the JOptionPane class's showInputDialog() method to see how to use it.  The showInputDialog() method returns a value that needs to be saved in a variable:

showing an example of the HTML formatting to combine those bottom three JOptionPanes into one

If you want several Strings to be displayed on separate lines by the JOptionPane.showMessageDialog() method, add a \n at end of each String.
See your previous thread for examples.
Otherwise please explain why you want to use HTML.
 
Johnny Wayne
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Norm Radder wrote:
the capture of the user inputs

You need to read the API doc for the JOptionPane class's showInputDialog() method to see how to use it.  The showInputDialog() method returns a value that needs to be saved in a variable:

showing an example of the HTML formatting to combine those bottom three JOptionPanes into one

If you want more than one String to be displayed on separate lines by the JOptionPane.showMessageDialog() method, add a \n at end of each String.
See your previous thread for examples.


Alright, I'll read the API doc. Thanks.

I assure you the \n does not work.
 
Norm Radder
Ranch Foreman
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the \n does not work

Please post an example with the \n that does not work.
It works for me.
 
Johnny Wayne
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mr. Norm, so I have figured out that the program is only using the users first input (number of days required) to determine the discount. The program isn't doing what I have declared "discount" for, which is add referrals and rentals.
 
Norm Radder
Ranch Foreman
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have declared "discount" for, which is add referrals and rentals.

Ok, time for some debugging.  Add some print statements that print out the value in referrals and the value in rentals to make sure they are what you think they should be.
 
Johnny Wayne
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Norm Radder wrote:
I have declared "discount" for, which is add referrals and rentals.

Ok, time for some debugging.  Add some print statements that print out the value in referrals and the value in rentals to make sure they are what you think they should be.


Morning, the print statements show that the program counts what I type in "number of days" rather than anything that is typed in the "rentals" and "referrals" inputs. I'm assuming the "int days = new Integer(input).intValue();  is incorrect.


 
Knute Snortum
Sheriff
Posts: 4073
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Norm Radder wrote:
the capture of the user inputs

You need to read the API doc for the JOptionPane class's showInputDialog() method to see how to use it.  The showInputDialog() method returns a value that needs to be saved in a variable:

I think you need to read this comment again.  You're not capturing the the input -- the return value of the JOptionPane.
 
Norm Radder
Ranch Foreman
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you add the print statements that I suggested for those two variables?  What was printed?
 
Johnny Wayne
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Norm Radder wrote:Did you add the print statements that I suggested for those two variables?  What was printed?


Yes sir, it is only capturing the first input. I am working on making it return the value of each input, but I keep getting the syntax wrong. I will just keep reading and trying until I get it.
 
Campbell Ritchie
Marshal
Posts: 55717
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this Java™ Tutorials page.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!