2

I am getting these exception while running the code

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 Ideone.main(Main.java:22)

I am new at java and unable to resolve this error. Please help ! Here is my code ->

import java.util.*;
import java.lang.*;
import java.io.*;


class etest {
    public static void main (String[] args) throws java.lang.Exception{
        int n,k;
        Scanner in = new Scanner(System.in);
        n = in.nextInt();
        k = in.nextInt();
        BufferedReader input = new BufferedReader( new InputStreamReader(System.in));
        //StringTokenizer token = new StringTokenizer(input.readLine());
        int total=0;
        int values[] = new int[n];
        for(int i =0; i<n; i++) {
            values[i] = Integer.parseInt(input.readLine());
            if ((values[i]%k)==0) {
                total++ ;
            }
            input.close();
        }
        System.out.println(total);
    }
}

I am using the following input sample to run the program. Thank you so much for any help!

7 3
1
51
966369
7
9
999996
11
deme72
  • 1,113
  • 1
  • 10
  • 13
Akash Kumar
  • 165
  • 1
  • 12
  • If your stream is exhausted, `input.readLine()` returns `null` – QBrute May 06 '15 at 12:56
  • So how to overcome this? – Akash Kumar May 06 '15 at 12:58
  • Wouldnt this be much easier with just the `Scanner` and having something like `while (scanner.hasNextInt()) { values[i] = Integer.parseInt(scanner.nextInt()); }` – Jyr May 06 '15 at 12:58
  • @Jyr there is no need to do `Integer.parseInt` for `scanner.nextInt()` because already that is `int` – Prashant May 06 '15 at 13:01
  • You are trying too many things at a time ...use either `Scanner` or `BuffredReader` keep it simple ,close the IO stream immediately after use (if it has no further use) – Abhishek May 06 '15 at 13:03
  • @Prashant True. It was a fast comment and I wasn't paying enough attention obviously ;). – Jyr May 06 '15 at 13:05
  • What exactly is this code supposed to do? – deme72 May 06 '15 at 13:08
  • @deme72 The input begins with two positive integers n k (n, k<=10^7). The next n lines of input contain one positive integer ti, not greater than 10^9, each and output denoting how many integers ti are divisible by k. – Akash Kumar May 06 '15 at 13:17

1 Answers1

0

your program has too many errors :

You can change your code as below

public static void main(String[] args) throws java.lang.Exception {
    int n, k;
    Scanner in = new Scanner(System.in);
    n = in.nextInt();
    k = in.nextInt();
    int total = 0;
    int values[] = new int[n];
    for (int i = 0; i < n; i++) {
        values[i] = in.nextInt();
        if ((values[i] % k) == 0) {
            total++;
        }
    }
    System.out.println(total);
}

1) you should not close BufferedReader it will automatically close input stream also.

2) you don't need Scanner and BufferedReader at the same time. your solution can use any one of them.

3) better to use try-catch while using Integer.parseInt(String str);

if you want to go with BufferedReader then you need to change your code as

public static void main(String[] args) throws java.lang.Exception {
    int n, k;


    BufferedReader input = new BufferedReader( new InputStreamReader(System.in));
    String str[]= input.readLine().split(" ");
    n = Integer.parseInt(str[0]);
    k = Integer.parseInt(str[1]);
    int total = 0;
    int values[] = new int[n];
    for (int i = 0; i < n; i++) {
        values[i]=Integer.parseInt(input.readLine());
        if ((values[i] % k) == 0) {
            total++;
        }
    }
    System.out.println(total);
}
Prashant
  • 2,556
  • 2
  • 20
  • 26