1

I am trying to do a lab where I am supposed to find the mode from a numbers.txt file. But I'm having problems storing the numbers in an array. I understand how to read the file but for some reason when I am trying to store the numbers I get a java.util.NoSuchElementException. I think that means the scanner I am using doesn't have anything else to read off of but I put the method in a try-catch already so I don't understand what's going on.

Here's the main part of the problem:

int [] modeArray = new int [total];
try {
    in = new Scanner(new File("numbers.txt"));
    for (int x = 0; x <= total; x++) {
        modeArray[x] = in.nextInt();
    }
}
catch (IOException i) {
    System.out.println("Error: " + i.getMessage());
}

And here's the rest of the code if you want to see the rest of the lab:

import java.util.Scanner;
import java.io.File; 
import java.io.IOException;
/**
 * lab 16.1
 * 
 * @author (Raymond Ma) 
 * @version (1/3/15)
 */
public class firstLab {
    public static void statistics() { 
        Scanner in;
        Scanner inTwo;
        Scanner inThree;
        int total = 0;
        double numbers = 0;
        double num = 0;
        double standardDeviation;

        //Average part
        try {
            in = new Scanner(new File("numbers.txt"));
            while (in.hasNextInt()) {
                total += in.nextDouble(); 
                numbers++;
            }
        }
        catch (IOException i) {
            System.out.println("Error: " + i.getMessage());
        }  
        System.out.println("The Average of this huge list of integers is " + total/numbers);

        //Standard deviation part
        try {
            inTwo = new Scanner(new File("numbers.txt"));
            while (inTwo.hasNextInt()) {
                num = Math.pow((inTwo.nextDouble() - (total/numbers)),2);
            }
        }
        catch (IOException i) {
            System.out.println("Error: " + i.getMessage());
        }
        standardDeviation = Math.sqrt(num/(numbers-1));
        System.out.println("The Standard Deviation of this huge list of integers is " + standardDeviation);

        //This is the most annoying part (the mode)
        int [] modeArray = new int [total];
        try {
            inThree = new Scanner(new File("numbers.txt"));
            for (int x = 0; x <= total; x++) {
                modeArray[x] = inThree.nextInt();
            }
        }
        catch(IOException i) {
            System.out.println("Error: " + i.getMessage());
        }
        System.out.println("The Mode of this huge list of integers is ");// + )
    }
}
7
  • IMHO it is better practice when reading to use while (in.hasNext()) {...} instead of a for loop, even if you know how many numbers you're reading. What line causes the problem? Commented Jan 3, 2015 at 23:08
  • It doesn't look like they are really answering the question down there. Even if you caught it, you would possibly be misrepresenting the data because of the error. Commented Jan 3, 2015 at 23:12
  • it highlights the "modeArray[x] = in.nextInt();" line Commented Jan 3, 2015 at 23:13
  • I may have found it. modeArray[x] when x == total could be out of bounds. But wouldn't that throw an indexOutOfBoundsException? What is x when the error triggers? Commented Jan 3, 2015 at 23:16
  • @BenKnoble The problem has nothing to do with the array size. He gets that Exception, because he tries to read more data, than the file really has. Commented Jan 3, 2015 at 23:17

3 Answers 3

3

NoSuchElementException is a RuntimeException, not an IOException. Therefore it wouldn't be caught. Here are the hierarchies.

     java.lang.Object
            java.lang.Throwable
                   java.lang.Exception
                           java.lang.RuntimeException
                                    java.util.NoSuchElementException

VS.

     java.lang.Object
             java.lang.Throwable
                     java.lang.Exception
                              java.io.IOException

You can correct this by

  1. importing the exception by java.util.NoSuchElementException (right now you've only imported IOException )

  2. Catching it. (For example: catch(NoSuchElementException i) )

Also, in your for loop:

for (int x = 0; x <= total; x++) {
            modeArray[x] = inThree.nextInt();
        }

Change x<= total to x< total because the array is indexed from 0 to total-1. (total is the array size)

Sign up to request clarification or add additional context in comments.

Comments

1

You are getting the exception since the file doesn't contain enough integers, as you suspect.

Also, NoSuchElementException is not an IOException, so it will not be caught by the try-catch. Just add it to the catch statement:

int[] modeArray = new int[total];
try (Scanner in = new Scanner(new File("numbers.txt"))) {
    for (int x = 0; x <= total; x++) {
        modeArray[x] = in.nextInt();
    }
} catch (IOException | NoSuchElementException i) {
    System.out.println("Error: " + i.getMessage());
}

7 Comments

When I add the NoSuchElementException to the catch it says the class doesn't exist
@Zak That's because you only imported java.io.IOException. See my answer.
You need to add it to your imports, in the same way you imported IOException.
I tried to import it also but it still said that the class did not exist
The line should be import java.util.NoSuchElementException;.
|
1

NoSuchElementException is not an IOException. (See the object hierarchy tree in the Javadocs.) You have to catch(NoSuchElementException i).

But there are two issues going on here:

  1. In your code, total is the sum of the numbers, but you really want to use numbers when creating your array. (E.g. for numbers 3, 5, 7, there are 3 numbers--that's what you want when creating your array size--but total would be 15.)
  2. In your for loop, you want i < total, not i <= total. Again, if you have three numbers, since the index count starts at 0, the maximum index in the array is not the size (3), but the size - 1 (2).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.