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!

Edited 1 Year Ago by Miran_Leli

You are trying to parse a variable that hasn't been initialized yet (so has the default value, null) to a number. There is no value, so also not a numerical value which can be parsed.
This causes the exception.

If you want the user to enter the data by input through the command prompt, I would recommend using an instance of Scanner.
A quick fix can also be - this, within your loop:

int n=Integer.parseInt(br.readLine());

replaced by:

String s = br.readLine();
if ( s != null ){
int n = Integer.parseInt(s);
// rest of process
}

but you should really use the Scanner class, instead of a BufferedReader. using that, you can read ints directly by using the nextInt() method.

thanks a lot!!!! can i ask you a last doubt about it? ..i've tried as you said by using an istance of Scanner but still would give me a similar error:'

import java.util.Scanner;
     class MyProgram
     {
     public static void main(String args[])
       {
        Scanner scan=new Scanner(System.in);
        System.out.println( "insert a number");
        int n1=scan.nextInt();
        int i;
        int count=0;
        for(i=0; i<=n1; i++){
            System.out.println( "insert a number");
            int n=scan.nextInt();
            if (n<0){
                count+=1;
            }
        System.out.println("there are" + count + "negative numbers");
        }
        }
        }

Error: Exception in thread "main" java.util.NoSuchElementException`

at java.util.Scanner.throwFor(Scanner.java:862)

at java.util.Scanner.next(Scanner.java:1485)

at java.util.Scanner.nextInt(Scanner.java:2117)

at java.util.Scanner.nextInt(Scanner.java:2076)

at java.util.Scanner.nextInt(Scanner.java:2076)

at MyProgram.main(Main.java:13)

This exception isn't even related to the previous one. Why do you say you get a similar exception?

I also have no idea what you are trying to do, but I just ran your code, and it works just the way it should. well .. the way you wrote the code, anyway.

Alright, i guess by using the online compiler that error comes, anyway thanks for your time and patience, you have helped me a lot!!!

It looks to me like the data you are entering at runtime isn't what the program expects. Your new exception looks like Scanner's version of the oriogional exception, so it's the same underlying problem - eg maybe you are pressing enter multiple times after entering n1.
If you just enter the numbers separated by spaces or carriage returns then that code should run OK.

This article has been dead for over six months. Start a new discussion instead.