Zak
Zak

Reputation: 43

Java "no such element exception"

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 ");// + )
    }
}

Upvotes: 1

Views: 4321

Answers (3)

idontevenseethecode
idontevenseethecode

Reputation: 962

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).

Upvotes: 1

Jobs
Jobs

Reputation: 3377

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)

Upvotes: 3

Keppil
Keppil

Reputation: 46239

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());
}

Upvotes: 1

Related Questions