-2

I'm pretty new to Java and I'm having difficulty printing the array created from reading a .txt file. I cannot figure out what is i've done wrong. Also, how would I put the .txt file into the same directory as my class? these have to be seperate methods,

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

public class ArrayOperations {
  // The code for the method readFromFile is given below.

public static void main(String[] args){

}
public static ArrayList<Integer> readFromFile(String fileName)
                              throws FileNotFoundException
{ 
     File f = new File("data.txt");
     Scanner fileIn = new Scanner (f);
     ArrayList<Integer> list = new ArrayList<Integer>();
     while (fileIn.hasNextInt()){ // quit when you encounter ‘Q’
        int num = fileIn.nextInt();
        list.add(num);

     }// end while
   fileIn.close();
   return list;
} // end readFromFile

public static String printArray(int[] readFromFile){
    String str = "";
    for(int i = 0; i < 10; i++){
        str = str + Integer.toString(readFromFile[i]) + " ";
    }
    return str;
}

}

This is the .txt file named Data.txt

the output i'm trying to get is this:

The data in the file is:

45 32 97 87 64 37 65 72 84 22 58 65 72 89 93 95

Connor
  • 1
  • 3
  • What is the problem you are facing? – prasanth Apr 07 '17 at 22:49
  • Why does your method take an int[], since what you need to print is an ArrayList? Wouldn't it be more logical if it took a List as argument instead of an int[]? – JB Nizet Apr 07 '17 at 22:50
  • my program isn't giving me any output at all, I'm really new to Java and have little knowledge on Array's or reading a text file. – Connor Apr 07 '17 at 22:52
  • Of course it doesn't. Look at your main method: it doesn't contain any instruction. – JB Nizet Apr 07 '17 at 22:53
  • *Here's a description of what I want to do. Here's a code dump.* No problem description, no explanation of any issues with the code, no question asked. – Ken White Apr 07 '17 at 22:53
  • You need call your methods in your main function – Yu Jiaao Apr 07 '17 at 22:54

1 Answers1

0
import java.io.*;
import java.util.ArrayList;
import java.util.*;

public class ArrayOperations {
    public static void main(String[] args) throws Exception{
            List<Integer> readFromFile = readFromFile("data.txt") ;
            printArray(readFromFile) ;
    }
    public static List<Integer> readFromFile(String fileName)
                                  throws FileNotFoundException
    { 
         File f = new File(fileName);
         Scanner fileIn = new Scanner (f);
         List<Integer> list = new ArrayList<Integer>();
         while (fileIn.hasNextInt()){ // quit when you encounter ‘Q’
            int num = fileIn.nextInt();
            list.add(num);

         }// end while
       fileIn.close();
       return list;
    } // end readFromFile

    public static String printArray( List<Integer> readFromFile){
        String str = "";
        for(int i = 0; i < readFromFile.size(); i++){
            str = str + Integer.toString(readFromFile.get(i)) + " ";
        }
        System.out.println(str);
        return str;
    }
}
  • Thank you for your answer, this worked well. I changed List to ArrayList to match up with my previous code. Thank you for helping me learn what the code is actually supposed to look like. – Connor Apr 07 '17 at 23:37