user2774647
user2774647

Reputation: 75

How to use multiple Input Dialogs (New to Java)

I am trying to create a program that asks a user for a sentinel value (a value to enter when they want to end the list). It then asks the user to enter numbers until they re-enter the sentinel value. It then figures out the max number in the list. I'm very new to Java, and whenever I run the program is just asks for the sentinel value then does nothing else (never pops up the second input dialog). I'm sure it's something simple that I'm doing wrong, but I can't figure it out. Thanks for any help.

import java.util.*; 

import javax.swing.JOptionPane;
public class HW1 {

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

    Scanner input = new Scanner(System.in);
    int number;
    int max;
    int sentinel;
    int count=0;

    JOptionPane.showInputDialog("Please enter a sentinel value: ");
    sentinel=input.nextInt();

    JOptionPane.showInputDialog("Please enter numbers. Enter" + sentinel +" to end.");
    number = input.nextInt();
    max = number;
    while (number!=sentinel){
        count +=1;
        if (number>max)
            max=number;
        JOptionPane.showInputDialog("Please enter numbers. Enter" + sentinel +" to end.");
        number = input.nextInt();
    }
    if (count!=0){
        JOptionPane.showMessageDialog(null, "The max is:" + max);
    }
  }
 }

Upvotes: 2

Views: 798

Answers (2)

Jessai
Jessai

Reputation: 947

You are mixing the ways to input data to your program. Let's begin:

Scanner input = new Scanner(System.in);

The line above allows you to catch data in the command line from the keyboard.

JOptionPane.showInputDialog("Please enter a sentinel value: ");

This Option Pane is showing correctly, you put a value and then nothing happens. This is because your program is waiting to input something in the command line

sentinel=input.nextInt();

When your program arrives to the line above, the input.nextInt() stops the program until you put something in the command line.

The correct way should be something like this:

sentinel = Integer.parseInt(JOptionPane.showInputDialog("Please enter a sentinel value: "));
number = Integer.parseInt(JOptionPane.showInputDialog("Please enter numbers. Enter" + sentinel +" value to end."));

And remove:

number = input.nextInt();
sentinel=input.nextInt();

Upvotes: 2

Eran Medan
Eran Medan

Reputation: 45715

I think the confusion is this:

  • the JOptionPane opens with an input dialog

  • when the option pane closes, whatever you put there is ignored

  • then the code goes to this line sentinel=input.nextInt();

    which waits for input from the console (e.g. you need to go back to the console, type the number there and press enter, only then the program will advance, it will block untill you do)

I would change it to something like this:

  String sentinelInput = JOptionPane.showInputDialog("Please enter a sentinel value: ");
  sentinel= Integer.parseInt(sentinelInput);

(repeat for all places where you expect input)

An alternative solution is

Don't use the JOptionPane, and instead just System.out.println to print the user a request for input (instead of the popup dialog). Then you can and keep the existing input.nextInt() calls to collect it.

Just note that all interaction will be in the console, without any popup dialogs (which I actually prefer in terms of user experience, and also it will be working in non GUI machines such as a linux terminal...)

Upvotes: 2

Related Questions