Question

Write a code to pull the numbers from a text file called "Games.txt" and put them...

Write a code to pull the numbers from a text file called "Games.txt" and put them into an array. also make getters and setters for the array

JAVAJAVAJAVA

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

import java.io.File;
import java.util.Scanner;

public class GamesFile {
   static int arr[];

   public static void main(String[] args) throws Exception {
       Scanner sc = new Scanner(new File("Games.txt"));
       int count = 0;
       // finding the size of array
       while (sc.hasNextLine()) {
           sc.nextLine();
           count++;
       }
       // creating array with size
       arr = new int[count];
       sc = new Scanner(new File("Games.txt"));
       int i = 0;
       // reading numbers into file
       while (sc.hasNext()) {
           arr[i++] = sc.nextInt();
       }
       i=0;
       while(i<count){
           System.out.print(arr[i]+" ");
           i++;
       }
   }

   public static int[] getArr() {
       return arr;
   }

   public static void setArr(int[] aArr) {
       arr = aArr;
   }

}

Add a comment
Know the answer?
Add Answer to:
Write a code to pull the numbers from a text file called "Games.txt" and put them...
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
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