-2

I am currently working on an assignment and I am stumped on what to do next. I am not asking for you to do it for me I just need help on what to do next. I get an error on the line " number = inFile.nextInt();" and it says java.util.InputMismatchException: null (in java.util.Scanner)

Description: You are to read an external file of random integer values until the end of file is found. As you read the file you should determine how many numbers are less than the value 500 and how many numbers are greater than or equal to 500.

The output I need is: The number of numbers less than 500 is 192 The number of numbers greater than or equal to 500 is 208 The total number of numbers is 400

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


public class Prog209a
{
public static void main (String args[])
{  

Scanner inFile = new Scanner( "C:\\Users\\Air\\Documents\\java\\p209a.dat");


    int number; //number
    int Lesser = 0; //count of numbers less than 500
    int Greater = 0;//count of numbers greater than 500 or equal to 500
    int Count = 0;


    while(inFile.hasNext()== true)
    {
     //input
        number = inFile.nextInt();
     //decision making
        if (number < 500)
            Lesser++;
        else
            Greater++;     
        Count ++;    
    }

} }

Redheat2
  • 11
  • 3

2 Answers2

0

Your program counted perfectly; you just never presented the user with the desired output.

Add the following to the end of the code.

System.out.println("The number of numbers less than 500 is " + Lesser + ". The number of numbers greater than or equal to 500 is " + Greater + ". The total number of numbers is 400)

Assuming the program read 192 numbers less than 500 and 208 numbers greater than 500, the desired output will be reached.

  • I realize that this may be a duplicate but I don't see a desired answer for my use. Whenever I compile then try to execute the program I receive "java.util.InputMismatchException: null (in java.util.Scanner)" for the line "number = inFile.nextInt();" – Redheat2 Nov 13 '14 at 01:21
  • Java is basically telling you that it's reading a data type other than an `int` Are you sure that all of the numbers in your files are `int`s? If there are any letters or decimal places that could screw it up. If there are any doubles, replace `inFile.nextInt();` with `inFile.nextDouble();` – ThatDandyMan Nov 13 '14 at 01:23
  • They are in fact all int's. – Redheat2 Nov 13 '14 at 01:28
0

Do you mean print the result? System.out.println("The count of the number which is less than 500: " + Lesser);

cck3rry
  • 191
  • 2