Results 1 to 14 of 14
  1. #1
    jo15765 is offline Member
    Join Date
    May 2012
    Posts
    42
    Rep Power
    0

    Default 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 02:52 PM.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,963
    Rep Power
    8

    Default 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 - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    jo15765 is offline Member
    Join Date
    May 2012
    Posts
    42
    Rep Power
    0

    Default 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
    	}
    }
    And now this is the error I get:
    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

  4. #4
    wdh321 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    116
    Rep Power
    0

    Default Re: How to take User Input and Perform Calculations

    Quote Originally Posted by jo15765 View Post
    This is the code (still a work in progress) that I am working with:

    And now this is the error I get:
    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
    Take a look at lines 9,10,11. Then look at where you would expect to see ";"

  5. #5
    jo15765 is offline Member
    Join Date
    May 2012
    Posts
    42
    Rep Power
    0

    Default 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

  6. #6
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,963
    Rep Power
    8

    Default Re: How to take User Input and Perform Calculations

    Quote Originally Posted by jo15765 View Post
    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
    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 - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    jo15765 is offline Member
    Join Date
    May 2012
    Posts
    42
    Rep Power
    0

    Default 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);

  8. #8
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,963
    Rep Power
    8

    Default Re: How to take User Input and Perform Calculations

    Quote Originally Posted by jo15765 View Post
    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);
    Huh? No, that's now what I meant at all. That will result in three dialogs, not a single dialog with three lines.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  9. #9
    jo15765 is offline Member
    Join Date
    May 2012
    Posts
    42
    Rep Power
    0

    Default 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?

  10. #10
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,963
    Rep Power
    8

    Default Re: How to take User Input and Perform Calculations

    Quote Originally Posted by jo15765 View Post
    How would I code it so that each one displays in the same dialog box then?
    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 - Play indie games, learn from game tutorials and source code, upload your own games!

  11. #11
    wdh321 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    116
    Rep Power
    0

    Default Re: How to take User Input and Perform Calculations

    Quote Originally Posted by jo15765 View Post
    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);
    You can add these into the same dialog box as KevinWorkman said (currently you are making 3) and use the \n for new lines
    BUT your code you have there wont work yet, you need to use "" around your strings currently you just have one " in the middle.

  12. #12
    jo15765 is offline Member
    Join Date
    May 2012
    Posts
    42
    Rep Power
    0

    Default 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 );

  13. #13
    jo15765 is offline Member
    Join Date
    May 2012
    Posts
    42
    Rep Power
    0

    Default 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 );
        }
    }
    And I am getting this error:
    on line 22 error: possible loss of precision
    numtimesP = Math.PI * num1

    required: int
    found: double

    What did I do wrong?

  14. #14
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,963
    Rep Power
    8

    Default 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 - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. User Input???
    By jonytek in forum New To Java
    Replies: 8
    Last Post: 01-13-2013, 02:52 PM
  2. User input
    By the ole buc in forum New To Java
    Replies: 16
    Last Post: 12-11-2011, 07:08 PM
  3. Help with user input
    By sconniegorilla in forum New To Java
    Replies: 2
    Last Post: 02-16-2011, 02:00 PM
  4. Need help getting input(first/last name) from user
    By nightrise420 in forum New To Java
    Replies: 11
    Last Post: 09-11-2010, 03:09 AM
  5. User input- Pop Up Box
    By dedachi in forum AWT / Swing
    Replies: 3
    Last Post: 03-23-2009, 04:47 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •