Results 1 to 14 of 14
- 05-02-2012, 03:47 PM #1
Member
- Join Date
- May 2012
- Posts
- 65
- Rep Power
- 0
How to take User Input and Perform Calculations
I am trying to write a Java program that will display a pop up box asking for user input (which I have learned will be a String type). Then convert the string to int and perform calculations. I am getting stuck...can someone provide some help on the calculations part (I keep getting an error that says can not find symbols?)?
Java Code:int num1 int num1Doubled int num1Tripled JFrame frame = new JFrame("A Dialog to Get Integer Input from the User"); String Name = JOptionPane.showInputDialog(frame, "Enter A Random Number"); num1 = Integer.parseInt(Name); num1Doubled = 2 * num1; num1Tripled = 3 * num1;
Last edited by jo15765; 05-02-2012 at 03:52 PM.
- 05-02-2012, 04:07 PM #2
Re: How to take User Input and Perform Calculations
Is any of that inside a method? Please post an SSCCE that demonstrates exactly what you're doing.
How to Ask Questions the Smart Way
Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
Static Void Games forum - Come say hello!
- 05-02-2012, 04:18 PM #3
Member
- Join Date
- May 2012
- Posts
- 65
- Rep Power
- 0
Re: How to take User Input and Perform Calculations
This is the code (still a work in progress) that I am working with:
Java Code:import javax.swing.JOptionPane; import java.awt.*; import javax.swing.*; public class ArithmaticOperations { public static void main (String args[]) { int num1 int num1Doubled int num1Tripled JFrame frame = new JFrame("A Dialog to Get Integer Input from the User"); String Name = JOptionPane.showInputDialog(frame, "Enter A Random Number"); num1 = Integer.parseInt(Name); num1Doubled = 2 * num1; num1Tripled = 3 * num1; //Somehow use JOption.DisplayMessageBox to show the results in a pop-up } }
C:\A_Programs>javac tes.java
tes.java:9: error: ';' expected
int num1
^
tes.java:10: error: ';' expected
int num1Doubled
^
tes.java:11: error: ';' expected
int num1Tripled
^
3 errors
- 05-02-2012, 04:22 PM #4
Senior Member
- Join Date
- Aug 2011
- Posts
- 116
- Rep Power
- 0
- 05-02-2012, 04:29 PM #5
Member
- Join Date
- May 2012
- Posts
- 65
- Rep Power
- 0
Re: How to take User Input and Perform Calculations
Oh good grief! Of course it is something extremely easy that I just carelessly overlooked!
Any tips on how to use JOption.DisplayMessageBox to show the results in a pop-up? Like one pop up window but each on a seperate line show:
the userinput
numberDoubled
numberTripled
- 05-02-2012, 04:32 PM #6
Re: How to take User Input and Perform Calculations
Can you use html? Does \n work?
Recommended reading: How to Make Dialogs (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)How to Ask Questions the Smart Way
Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
Static Void Games forum - Come say hello!
- 05-02-2012, 04:37 PM #7
Member
- Join Date
- May 2012
- Posts
- 65
- Rep Power
- 0
Re: How to take User Input and Perform Calculations
So try it like this:
Java Code:JOptionPane.showMessageDialog(frame, The User Inputted: " + num1 \n); JOptionPane.showMessageDialog(frame, The number Doubled: " + num1Doubled \n); JOptionPane.showMessageDialog(frame, The number Tripled: " + num1Tripled \n);
- 05-02-2012, 04:40 PM #8
Re: How to take User Input and Perform Calculations
How to Ask Questions the Smart Way
Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
Static Void Games forum - Come say hello!
- 05-02-2012, 04:40 PM #9
Member
- Join Date
- May 2012
- Posts
- 65
- Rep Power
- 0
Re: How to take User Input and Perform Calculations
How would I code it so that each one displays in the same dialog box then?
- 05-02-2012, 04:48 PM #10
Re: How to take User Input and Perform Calculations
You're almost there, so try experimenting with different Strings. What happens when you have the \n in the middle of a String displayed on a JOptionPane (I honestly don't know for sure it will work, but it's what I would try since it takes 3 seconds to test)? What happens if you use some html tags like br?
How to Ask Questions the Smart Way
Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
Static Void Games forum - Come say hello!
- 05-02-2012, 05:01 PM #11
Senior Member
- Join Date
- Aug 2011
- Posts
- 116
- Rep Power
- 0
Re: How to take User Input and Perform Calculations
- 05-02-2012, 05:03 PM #12
Member
- Join Date
- May 2012
- Posts
- 65
- Rep Power
- 0
Re: How to take User Input and Perform Calculations
Thanks for the help! It took me a quick minute but I just had to encompass the /n with quotes so my code reads like:
Java Code:JOptionPane.showMessageDialog(frame, "The User Inputted: " + num1 + "\n" + "The number doubled is: " +num1Doubled + "\n" + "The number tripled is: " + num1Tripled );
- 05-02-2012, 05:14 PM #13
Member
- Join Date
- May 2012
- Posts
- 65
- Rep Power
- 0
Re: How to take User Input and Perform Calculations
Actually one more question...
I tried to add one more calculation to the above code (which is working perfectly) so here is my modified code:
Java Code:import javax.swing.JOptionPane; import java.awt.*; import javax.swing.*; import java.lang.Math; public class ArithmaticOperations { public static void main (String args[]) { int num1; int num1Doubled; int num1Tripled; int numtimesP; JFrame frame = new JFrame("A Dialog to Get Integer Input from the User"); String Name = JOptionPane.showInputDialog(frame, "Enter A Random Number"); num1 = Integer.parseInt(Name); num1Doubled = 2 * num1; num1Tripled = 3 * num1; numtimesP = Math.PI * num1; //Somehow use JOption.DisplayMessageBox to show the results in a pop-up JOptionPane.showMessageDialog(frame, "The User Inputted: " + num1 + "\n" + "The number doubled is: " +num1Doubled + "\n" + "The number tripled is: " + num1Tripled ); } }
on line 22 error: possible loss of precision
numtimesP = Math.PI * num1
required: int
found: double
What did I do wrong?
- 05-02-2012, 05:17 PM #14
Re: How to take User Input and Perform Calculations
Your numtimesP variable is an int, which can only be whole numbers (0, 1, -5, etc). Math.PI is a double, which is a decimal. So when you store a decimal-type number in an int, you lose any information after the decimal point.
A quick google of "java possible loss of precision" gives several more detailed explanations.How to Ask Questions the Smart Way
Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
Static Void Games forum - Come say hello!
Similar Threads
-
User Input???
By jonytek in forum New To JavaReplies: 8Last Post: 01-13-2013, 03:52 PM -
User input
By the ole buc in forum New To JavaReplies: 16Last Post: 12-11-2011, 08:08 PM -
Help with user input
By sconniegorilla in forum New To JavaReplies: 2Last Post: 02-16-2011, 03:00 PM -
Need help getting input(first/last name) from user
By nightrise420 in forum New To JavaReplies: 11Last Post: 09-11-2010, 04:09 AM -
User input- Pop Up Box
By dedachi in forum AWT / SwingReplies: 3Last Post: 03-23-2009, 05:47 AM
Bookmarks