简体   繁体   中英

Less input values than array length

For an assignment I have been asked to create a Java program that reads integer values from a file and stores them into an array with a length of 40, and prints the values in reverse order. My problem is that the assignment also requires it to work when there are less than 40 values in the file. This is what I have tried:

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

public class Set8_Prog3
{
    public static void main (String[] args) throws IOException
    {
        FileReader i = new FileReader("Set8_Prog3 numbers.txt");
        Scanner j = new Scanner(i);

        int[] values = new int[40];

        int k = 0;
        int last = -1;
        while (k < 40)
        {
            values[k] = j.nextInt();
            last = values[k];
            k++;
        }


        System.out.println("The values from the file in reverse order is: ");
        while (last >= 0)
        {
            System.out.println(values[last]);
            last--;
        }
    }
}

It compiles successfully, but when I run it I get an error saying java.util.NoSuchElementException. Also, I am not supposed to use an ArrayList.

只需将您的 while 条件更改为while(k < 40 && j.hasNextInt()) ,它应该可以正常工作。

values[k] = j.nextInt();

What happens if there is no nextInt() ? You have to manage it with j.hasNextInt()

while (k < 40)
{
    values[k] = j.nextInt();
    last = values[k];
    k++;
}

You are always trying to read 40 ints from the file, even if there is less. From the Javadoc of Scanner.nextInt() :

Throws:
  InputMismatchException - if the next token does not match the Integer regular expression, or is out of range
  NoSuchElementException - if input is exhausted
  IllegalStateException - if this scanner is closed

You are still reading even though the input has been exhausted. You should make use of the Scanner.hasNextInt() method to check if there is more input, and than your code can act accordingly.

First, your variable names are just unreadable. i and j should always be int or Integer values and not FileReader or whatever else.

Then, you don't need variable last and it is incorrect to use it the way you do it in your last loop, just use k instead.

Finally, and that is your real problem, you should just handle this exception (or better, use Scanner.hasNextInt() in your loop condition) in case the file does not contain 40 integers. In that case, just exit the loop.

While getting your input use hasNextInt to find is there any int values

While(k<40 && scanner.hasNextInt()){


}

K is already increased by 1. so first reduce it by 1. Then loop it until 0

k--;
    while (k>= 0)
    {
        System.out.println(values[k]);
        k--;
    }


    while (k < 40 && scanner.hasNextInt())
    {
        values[k] = j.nextInt();
        k++;
    }

    k--;
    System.out.println("The values from the file in reverse order is: ");
    while (k>= 0)
    {
        System.out.println(values[k]);
        k--;
    }

UPDATED CODE

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

public class Set8_Prog3
{
    public static void main (String[] args) throws IOException
    {
        FileReader i = new FileReader("Set8_Prog3 numbers.txt");
        Scanner j = new Scanner(i);

        int[] values = new int[40];

        int k = 0;

        while (k < 40 && j.hasNextInt())
        {
            values[k] = j.nextInt();
            k++;
        }

        k--;
        System.out.println("The values from the file in reverse order is: ");
        while (k>= 0)
        {
            System.out.println(values[last]);
            k--;
        }
    }
}

Sorry for the minor rewrite of some of your code. Just made it more clear as to what variables mean.

I also modified your use of magic numbers in the loop, and changed it to a for loop to get rid of the variable declaration above.

I also added scanner.hasNextInt() into the conditional of the for loop along with the i < values.length().

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

public class Set8_Prog3 {
    public static void main(String[] args) throws IOException {
        FileReader fileReader = new FileReader("Set8_Prog3 numbers.txt");
        Scanner scanner = new Scanner(fileReader);

        int[] values = new int[40];

        int count = -1;
        for (int i = 0; i < values.length && scanner.hasNextInt(); i++) {
            values[i] = scanner.nextInt();
            count = i;
        }

        System.out.println("The values from the file in reverse order is: ");
        for (int i = count; i >= 0; i--) {
            System.out.println(values[i]);
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM