Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Reading in floats into JPanel?

  1. #1
    Junior Member
    Join Date
    Jul 2013
    Posts
    13
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Reading in floats into JPanel?

    I'm trying to read in two floats but I'm not allowed to assign num1 or num2 to the input dialog box. Therefore, I'm having to assign the string variable msg to my input dialog box and then covert msg back over to a float and assign it to my float variables. whew! that was sentence.

    How can I reduce the complexity of this by just reading in num1 and num2 from the start?

    Please disregard commented out sections.

    import javax.swing.*;
    public class Week_4_test1
    {
        //public Week_4_test1(){}
            public static void main(String[] args)
            {
                float sum = 0.0f;
                float num1 = 0.0f;
                float num2 = 0.0f;
                //needs to be done because we are creating it dynamically 
                //String prompt = "Enter your name: ";
                //Scanner me = new Scanner(System.in);
                //System.out.printf("Enter your name: ");
                //String name = me.nextLine();
                String msg = "";
                //System.out.printf("Welcome To Java...");
                //System.out.print(name + "\n"+ msg + "\n");
                //JOptionPane.showMessageDialog(null, "Again, welcome to Java" + name);
                //JOptionPane.showInputDialog(null, prompt);
                //JOptionPane.showMessageDialog(null, "Another LIne " + name);
                msg = JOptionPane.showInputDialog(null, "Enter num1");
                num1 = Float.parseFloat(msg);
                msg = JOptionPane.showInputDialog(null, "Enter num1");
                num2 = Float.parseFloat(msg);
                sum = num1 + num2;
                JOptionPane.showMessageDialog(null, "The sume of " + num1 + " + " + num2 + " is " + sum);
            }
    }
    Attached Images Attached Images


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Reading in floats into JPanel?

    Think about what you're doing here:

    msg = JOptionPane.showInputDialog(null, "Enter num1");
    num1 = Float.parseFloat(msg);

    You're showing an input dialog, then assigning that String to a variable named message. You then pass that variable into Float.parseFloat(), which simply takes a String.

    Why not just pass the String returned from the input dialog directly into the parseFloat() method?

    num1 = Float.parseFloat(JOptionPane.showInputDialog(null, "Enter num1"));
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. The Following User Says Thank You to KevinWorkman For This Useful Post:

    syregnar86 (July 31st, 2013)

Similar Threads

  1. Reading text file and counting the number of words, integers, and floats.
    By Jsmooth in forum File I/O & Other I/O Streams
    Replies: 11
    Last Post: April 12th, 2012, 06:39 PM
  2. [SOLVED] Pesky <JPanel>.getWidth() and <JPanel>.getHeight() Methods...
    By snowguy13 in forum AWT / Java Swing
    Replies: 1
    Last Post: December 31st, 2011, 03:35 PM
  3. Reading file of floats into array list and sorting into subsets
    By Skave in forum Collections and Generics
    Replies: 2
    Last Post: November 9th, 2011, 07:03 PM
  4. How to copy image from one jpanel to another jpanel
    By ramanavarayuri1986 in forum AWT / Java Swing
    Replies: 0
    Last Post: February 15th, 2010, 02:36 AM
  5. Creating and displaying a JPanel inside another JPanel
    By JayDuck in forum AWT / Java Swing
    Replies: 1
    Last Post: April 7th, 2009, 08:02 AM