Hello. First time user here and having trouble with input/output with dialog boxes.
So I am suppose to use dialog boxes to read an unspecified number of integers until a zero is entered and output how many positive and negative values were entered, and to compute the average.
This is what my code looks like so far :
The program runs, but If I enter a integer, the box just vanishes and the program hangs. If I try entering more then one integer, the program just terminates. I am use to using a scanner for input and displaying the output on the console, so I am pretty bad at using dialog boxes. Am I missing something very basic here? Or is my code just completely wrong. Can you even enter multiple integers in the same dialog box? Any guidance in the right direction is appreciated and yes, I am pretty new to programming as you can see.
So I am suppose to use dialog boxes to read an unspecified number of integers until a zero is entered and output how many positive and negative values were entered, and to compute the average.
This is what my code looks like so far :
import javax.swing.JOptionPane; public class CountNumbers { // main method public static void main(String[] args) { int countPos = 0; int countNeg = 0; int count = 0; int total = 0; float average = 0f; // Enter integer(s) String numString = JOptionPane.showInputDialog(null, "Enter integer(s):", "Exit if 0 is entered", JOptionPane.INFORMATION_MESSAGE); int num = Integer.parseInt(numString); while (num != 0) { if (num > 0) countPos++; else if (num < 0) { countNeg++; } total += num; count++; average = total / count; num = Integer.parseInt(numString); } if (count == 0) { JOptionPane.showMessageDialog(null,"You didn't enter any numbers"); } else { JOptionPane.showMessageDialog(null, "You entered" + countPos + "positive number(s)\n" + "and" + countNeg + "negative number(s)\n" + "and the average value is" + average); } } }
The program runs, but If I enter a integer, the box just vanishes and the program hangs. If I try entering more then one integer, the program just terminates. I am use to using a scanner for input and displaying the output on the console, so I am pretty bad at using dialog boxes. Am I missing something very basic here? Or is my code just completely wrong. Can you even enter multiple integers in the same dialog box? Any guidance in the right direction is appreciated and yes, I am pretty new to programming as you can see.