Answers
thanks for the question,
You need to create an array of size 100. and then keep adding the numbers you read from the file in the array at the location given by the variable index. What you have so far, is to read all the numbers in the file but the code to store the numbers in the array is missing.
Since you have not shared the complete problem statement, I am assuming there is an int[] array by the name numbers and we want to populate the data from the file into this int[] array.
I have updated your class to do that. Rest everything is all good and sweet : )
If you have any problem, please do comment, no need to post another question for this. Will help you
thanks .
================================================================================
import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class SomeClassName { // assuming you have an array numbers[] of size 100 declared private int[] numbers = new int[100]; public void fillArray() { int curVal; Scanner input = null; try { input = new Scanner(new File("data.txt")); // set the current number of items in the array to zero int index = 0; // the position in the array where we are going to store the current number read while (input.hasNextInt()) { curVal = input.nextInt(); // add code to store curVal into the array and update other information as needed numbers[index] = curVal; index += 1; // increment the index by 1 so that we can add the next number in the next index } input.close(); } catch (FileNotFoundException e) { System.out.println("Could not find data.txt file"); System.exit(1); } } public static void main(String[] args) { } }
======================================================================
.