0

I have a txt file with 5 integers, then from standard input if I entered 1 it will output 10 if I entered 3 it will output 30. I put mine in an array, I just want to know if its possible to not put it in an array and still output the same way.

number.txt
10 20 30 40 50

public static void main(String[] args) throws IOException {

        int num = 5;
        int[] numArray = new int [num];

        //Reads in 5 integers
        File tempo = new File ("number.txt");
        Scanner file = new Scanner(tempo);

        for( int i = 0; i < numArray.length; i++){
            numArray[i] = file.nextInt();
        }
2
Contributors
2
Replies
10
Views
2 Years
Discussion Span
Last Post by redtribal23
1

If you don't put them in an array (or some other container eg ArrayList) then you would have to re-read the file for each user input, which would be a messy and inefficient solution.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.