Results 1 to 4 of 4
Like Tree1Likes
  • 1 Post By mitchj

Thread: Problem using Scanner and JOptionPane simultaneously

  1. #1
    mitchj is offline Member
    Join Date
    Apr 2012
    Posts
    7
    Rep Power
    0

    Default Problem using Scanner and JOptionPane simultaneously

    Hi,

    I am trying to use JOptionPane for some input. It works fine until I add a Scanner before the JOptionPane for some other input. It seems my Scanner is messing up the JOptionPane but I can't figure out why. (The scanner has to be before the JOptionPane)

    Can someone please help?

    Here is my code:


    import java.util.Scanner;

    import javax.swing.JOptionPane;

    public class JOptionPaneTester {

    /**
    * @param args
    */
    public static void main(String[] args) {
    // TODO Auto-generated method stub

    Scanner in = new Scanner(System.in);
    System.out.println("Enter something");
    if (in.hasNext()) {
    String inStr = in.next();
    }

    System.out.println("before JOptionPane");
    String input = JOptionPane.showInputDialog(null, "Enter the deposit amount");
    System.out.println("after JOptionPane");

    }

    }


    Thanks in advance!!

    Mitch

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Problem using Scanner and JOptionPane simultaneously

    Use in.nextLine() not in.next() in order to handle the end of line token.

  3. #3
    mitchj is offline Member
    Join Date
    Apr 2012
    Posts
    7
    Rep Power
    0

    Default Re: Problem using Scanner and JOptionPane simultaneously

    Quote Originally Posted by Fubarable View Post
    Use in.nextLine() not in.next() in order to handle the end of line token.
    Thanks for the prompt reply but the JOptionPane still does not appear (behavior of the program remains the same). My code gets to the part where it prints out "before JOptionPane" but the cursor sits in the console like it still expects me to input something and even if I input something the cursor stays in the console forever.

    Thanks!

    Mitch

  4. #4
    mitchj is offline Member
    Join Date
    Apr 2012
    Posts
    7
    Rep Power
    0

    Default Re: Problem using Scanner and JOptionPane simultaneously

    Thanks again for your reply but the JOptionPane was there the whole time. It was behind my eclipse IDE. Duh!
    awinston likes this.

Similar Threads

  1. JOptionPane problem
    By rokit boy in forum New To Java
    Replies: 3
    Last Post: 02-05-2012, 09:32 PM
  2. Problem with Array and Joptionpane
    By tkas in forum New To Java
    Replies: 3
    Last Post: 11-28-2011, 02:12 AM
  3. Problem with JOptionPane
    By chrisLU5 in forum New To Java
    Replies: 8
    Last Post: 05-05-2011, 11:04 PM
  4. Converting Scanner input to JOptionPane
    By Mideoan in forum New To Java
    Replies: 1
    Last Post: 03-10-2011, 12:26 AM
  5. JOptionPane problem
    By MomenT in forum New To Java
    Replies: 22
    Last Post: 10-02-2008, 06:56 PM

Posting Permissions

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