Results 1 to 4 of 4
  1. #1
    eviljeff is offline Member
    Join Date
    Jan 2012
    Location
    Chicago, Illinois, USA
    Posts
    19
    Rep Power
    0

    Default Question about JOptionPane

    I am a programming student, and I unfortunately missed class today. Today my professor probably discussed what JOptionPane is, which I missed.

    I'm currently working on a problem where I must declare an object keyboard of type Scanner, then declare and initialize a variable 'num', and declare and initialize a String variable to store user input. My next task is to prompt the user for input from the keyboard, and then store the user input into the String variable. So far, this is what I've got:

    Java Code:
    Scanner keyboard = new Scanner(System.in);
    int num = 0;
    String inputString;
    inputString = JOptionPane.showInputDialog("Enter an integer: ");
    Now, I'm not sure what JOptionPane actually does, because I've missed today's lecture. But the above code is from an example he left on the Blackboard site. So I now have two questions: (1) instead of the last line of the above code, could I just use the following? :

    Java Code:
    System.out.println("Enter an integer: ");
    inputString = keyboard.nextString();
    Question (2) Considering I use the method described in my question (1), does the following make sense to use to complete the program? :

    Java Code:
    //convert input to integer
    num = Integer.parseInt(inputString);
    
    //display the results
    JOptionPane.showMessageDialog(null, "The value of the integer is: " + num);
    
    //end program
    System.exit(0);
    }
    Any help would be greatly appreciated!

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Question about JOptionPane

    Yes and yes. I think - but you'll be sure when you try them.

    JOptionPane is discussed on the How to Make Dialogs page of Oracle's Tutorial.

  3. #3
    eviljeff is offline Member
    Join Date
    Jan 2012
    Location
    Chicago, Illinois, USA
    Posts
    19
    Rep Power
    0

    Default Re: Question about JOptionPane

    Thank you very much! :) I am having some trouble compiling the .java file, however. But I think it is unrelated to the code and more of an issue with the compiler. I'm not sure if I have everything I need to compile and run java code. I'm using Linux -- Xubuntu 11.10. Would you by any chance know what packages I would need to be able to work with java?

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Question about JOptionPane

    I don't know offhand. But ?Ubuntu is generally pretty good about installing packages.

    My guess would be

    Java Code:
    sudo apt-get install openjdk-7-jre
    If you have an existing Java runtime or compiler you may have to check after an install that "which java" and "which javac" report the correct (and same!) versions.

Similar Threads

  1. Simple question involving Strings and JOptionPane
    By Napkins in forum New To Java
    Replies: 6
    Last Post: 02-02-2011, 02:30 AM
  2. JOptionPane
    By izzy in forum New To Java
    Replies: 1
    Last Post: 03-17-2010, 04:55 PM
  3. Help with JOptionPane
    By sanctishit in forum New To Java
    Replies: 1
    Last Post: 03-10-2010, 10:20 AM
  4. JOptionpane
    By tommyyyy in forum New To Java
    Replies: 2
    Last Post: 03-20-2009, 08:33 AM
  5. JOptionPane String to int question
    By blackstormattack in forum New To Java
    Replies: 3
    Last Post: 03-08-2009, 12:14 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
  •