The exercise is to enter one number (n1) and after the user has to enter n1 numbers.Then it has to be printed the number of negative numbers out of the n1 numbers added. So if the user decide that n1=5 and then add 5 numbers : 3,4,5,-1,2 the program should say that there is one negative number. The program i made is as follows:
import java.io.*;
class MyProgram
{
public static void main(String args[])throws IOException
{
InputStreamReader isr=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(isr);
System.out.println( "insert a number");
int n1=Integer.parseInt(br.readLine());
int i;
int count=0;
for(i=0; i<=n1; i++){
System.out.println( "insert a number");
int n=Integer.parseInt(br.readLine());
if (n<0){
count+=1;
}
System.out.println("there are" + count + "negative numbers");
}
}
}
The error that appear is :
Exception in thread "main" java.lang.NumberFormatException: null
at java.lang.Integer.parseInt(Integer.java:542)
at java.lang.Integer.parseInt(Integer.java:615)
at MyProgram.main(Main.java:14)
i cant understand how i can fix this and make the program run correctly, can someone help me on this? thanks in advance!