Question

I am told to create a fillArray method that will read integers from the file "data.txt"...

I am told to create a fillArray method that will read integers from the file "data.txt" and store them in the array. I'm told to assume that the file has no more than 100 items in it.

This is what I have so far:

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

while (input.hasNextInt())

{

curVal = input.nextInt();

// add code to store curVal into the array and update other information as needed

}

input.close();

}

catch (FileNotFoundException e)

{

System.out.println("Could not find data.txt file");

System.exit(1);

}

}

I am unsure what I am missing, if anyone can help, I would be very grateful. Thank you.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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

    }
}

======================================================================

Add a comment
Know the answer?
Add Answer to:
I am told to create a fillArray method that will read integers from the file "data.txt"...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Problem: Use the code I have provided to start writing a program that accepts a large...

    Problem: Use the code I have provided to start writing a program that accepts a large file as input (given to you) and takes all of these numbers and enters them into an array. Once all of the numbers are in your array your job is to sort them. You must use either the insertion or selection sort to accomplish this. Input: Each line of input will be one item to be added to your array. Output: Your output will...

  • I am trying to read from a file with with text as follows and I am...

    I am trying to read from a file with with text as follows and I am not sure why my code is not working. DHH-2180 110.25 TOB-6851 -258.45 JNO-1438 375.95 CS 145 Computer Science II IPO-5410 834.15 PWV-5792 793.00 Here is a description of what the class must do: AccountFileIO 1. Create a class called AccountFileIO in the uwstout.cs145.labs.lab02 package. (This is a capital i then a capital o for input/output.) a. This class will read data from a file...

  • I am getting an error from eclipse stating that: Exception in thread "main" java.lang.Error: Unresolved compilation...

    I am getting an error from eclipse stating that: Exception in thread "main" java.lang.Error: Unresolved compilation problem: at EvenOdd.main(EvenOdd.java:10) I am unable to find what I can do to fix this issue. Can you please assist? My code is below: import java.util.InputMismatchException; import java.util.Scanner; public class EvenOdd {    public static void main(String[] args) {        Scanner input = new Scanner(System.in);        System.out.println("Welcome to this Odd program!");        int userInput1 = input.nextInt();    }       public...

  • Write a Java method that will take an array of integers of size n and shift...

    Write a Java method that will take an array of integers of size n and shift right by m places, where n > m. You should read your inputs from a file and write your outputs to another file. Create at least 3 test cases and report their output. I've created a program to read and write to separate files but i don't know how to store the numbers from the input file into an array and then store the...

  • Can anyone debug this for me please? Java code to copy package debugmeone; import java.io.File; import...

    Can anyone debug this for me please? Java code to copy package debugmeone; import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class ReadTextFile { private Scanner input; // Ignore the hint given by NetBeans public void openFile() { try { input = new Scanner( new File("accountrecords.txt")); } catch(Exception e) { System.out.println("Something bad just happened here."); System.exit(707); } catch( FileNotFoundException fnfe) { System.out.println("Error - File Not Found: accountrecords.txt"); System.exit(100); } } } ITCS-2590 Debugging Execse Chapter 11 - NetBeans IDE 8.2 File...

  • Finish the given ProcessFile.java program that prompts the user for a filename and reprompts if file...

    Finish the given ProcessFile.java program that prompts the user for a filename and reprompts if file doesn’t exist. You will process through the file skipping any text or real (double) numbers. You will print the max, min, sum, count, and average of the integers in the file. You will want to create test files that contain integers, doubles, and Strings. HINT: Use hasNextInt() method and while loop. You may also want to use Integer.MAX_VALUE and Integer.MIN_VALUE for the initialization of...

  • Need Help ASAP!! Below is my code and i am getting error in (public interface stack)...

    Need Help ASAP!! Below is my code and i am getting error in (public interface stack) and in StackImplementation class. Please help me fix it. Please provide a solution so i can fix the error. thank you.... package mazeGame; import java.io.*; import java.util.*; public class mazeGame {    static String[][]maze;    public static void main(String[] args)    {    maze=new String[30][30];    maze=fillArray("mazefile.txt");    }    public static String[][]fillArray(String file)    {    maze = new String[30][30];       try{...

  • I have a program that reads a file and then creates objects from the contents of...

    I have a program that reads a file and then creates objects from the contents of the file. How can I create a linked list of objects and use it with the package class instead of creating and using an array of objects? I am not allowed to use any arrays of objects or any java.util. lists in this program. Runner class: import java.util.Scanner; import java.io.*; class Runner { public static Package[] readFile() { try { File f = new...

  • Java. Java is a new programming language I am learning, and so far I am a...

    Java. Java is a new programming language I am learning, and so far I am a bit troubled about it. Hopefully, I can explain it right. For my assignment, we have to create a class called Student with three private attributes of Name (String), Grade (int), and CName(String). In the driver class, I am suppose to have a total of 3 objects of type Student. Also, the user have to input the data. My problem is that I can get...

  • I need help debugging this Java program. I am getting this error message: Exception in thread...

    I need help debugging this Java program. I am getting this error message: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2 at population.Population.main(Population.java:85) I am not able to run this program. ------------------------------------------------------------------- import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.util.Scanner; /* Linked list node*/ class node { long data; long year; String country; node next; node(String c,long y,long d) { country=c; year=y; data = d; next = null; } } public class Population { private static node head; public static void push(String...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT