-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

6
  • What is the problem you are facing? Commented Apr 7, 2017 at 22:49
  • Why does your method take an int[], since what you need to print is an ArrayList<Integer>? Wouldn't it be more logical if it took a List<Integer> as argument instead of an int[]? Commented Apr 7, 2017 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. Commented Apr 7, 2017 at 22:52
  • Of course it doesn't. Look at your main method: it doesn't contain any instruction. Commented Apr 7, 2017 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. Commented Apr 7, 2017 at 22:53

1 Answer 1

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;
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.