0

hey i need help once i run it will proceed to next one it will show error message

package test1;
import javax.swing.JOptionPane;
public class Test1 {



public static void main(String[] args) {

int counter;
int counter1=0;
int counter2=0;
int counter3=0;
int counter4=0;

JOptionPane.showMessageDialog(null,"Welcome");
for(counter=0;counter<20;)
{ 

int num1;
String firstinput = null ;
JOptionPane.showInputDialog("Enter a positive integer(1-9999):");
num1= Integer.parseInt(firstinput);

while (num1<1 || num1>9999)
{
JOptionPane.showInputDialog("The input number is not positive. Please enter a positive integer (1-9999):");
num1= Integer.parseInt(firstinput);
}
if(num1>=1 && num1<=9)
{
counter1++;
}
if(num1>=10 && num1<=99)
{
counter2++;
}
if(num1>=100 && num1<=999)
{
counter3++;
}
if(num1>=1000 && num1<=9999)
{
counter4++;
}
counter++;

}

JOptionPane.showInputDialog("The number of 1-digit input is:"+counter1);
JOptionPane.showInputDialog("The number of 2-digit input is:"+counter2);
JOptionPane.showInputDialog("The number of 3-digit input is:"+counter3);
JOptionPane.showInputDialog("The number of 4-digit input is:"+counter4);


JOptionPane.showInputDialog("Thank You, BYE BYE");
        System.exit(0);
}
}

Edited by deceptikon: Moved to Java forum

3
Contributors
7
Replies
35
Views
3 Years
Discussion Span
Last Post by stultuske
0

The error message tells you exactly what the error was and the exact line where the error occurred. That would be very useful information to share.

0

That tells you there's a null value being passed to parseInt on line 24 (looks like line 22 in the version you posted). Look at the code:

String firstinput = null ;
JOptionPane.showInputDialog("Enter a positive integer(1-9999):");
num1= Integer.parseInt(firstinput)

Obviously firstInput will still be null when you pass it to parseInt.
I guess you forgot to get the value from the input dialog and assign that to firstInput?

0

I have correct it...it works now but when i enter a negetive number it will keep one looping non stop.

package test2;
import javax.swing.JOptionPane;
public class Test2 {


    public static void main(String[] args) {



int counter;
int counter1=0;
int counter2=0;
int counter3=0;
int counter4=0;
int num1;
String string_in1;
String string_in2;

JOptionPane.showMessageDialog(null,"Welcome");
for(counter=0;counter<5;)
{ 

  string_in1 = JOptionPane.showInputDialog("Enter a positive integer(1-9999):");
  num1=Integer.parseInt(string_in1);


while (num1<1 || num1>9999)
{
JOptionPane.showInputDialog("The input number is not positive. Please enter a positive integer (1-9999):");
num1=Integer.parseInt(string_in1);
}
if(num1>=1 && num1<=9)
{
counter1++;
}
if(num1>=10 && num1<=99)
{
counter2++;
}
if(num1>=100 && num1<=999)
{
counter3++;
}
if(num1>=1000 && num1<=9999)
{
counter4++;
}
counter++;

}

JOptionPane.showMessageDialog(null,"The number of 1-digit input is:"+counter1);
JOptionPane.showMessageDialog(null,"The number of 2-digit input is:"+counter2);
JOptionPane.showMessageDialog(null,"The number of 3-digit input is:"+counter3);
JOptionPane.showMessageDialog(null,"The number of 4-digit input is:"+counter4);


JOptionPane.showMessageDialog(null,"Thank You, BYE BYE");
        System.exit(0);
}
}

Bellow is output which keep on looping

cff8e8e24dd90caa572cd9e7d9b2e0d3

0

that's because you made a logical error:

here is the total reach of the while loop in which you show that:

while (num1<1 || num1>9999)
{
JOptionPane.showInputDialog("The input number is not positive. Please enter a positive integer (1-9999):");
num1=Integer.parseInt(string_in1);
}

it may look correct, but you have forgotten to assign the value read in by the inputDialog to string_in1, so it'll always take the previous (negative) value.

Edited by stultuske

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.