0

I have this java method which returns an ArrayList, but I want to return an Array of Strings. The method reads the file words.txt (contains all words with a word on each line), and I want to store those words into an Array of Strings.

Heres the code I already have:

public static ArrayList<String> readFile(){
 File myFile=new File("./src/folder/words.txt");
 Scanner s1=null;
 //Creates ArrayList to store each String aux
 ArrayList<String> myWords = new ArrayList<String>();
 try {
 s1 = new Scanner(myFile);
 }catch (FileNotFoundException e) {
 System.out.println("File not found");
 e.printStackTrace();
 }
 while(s1.hasNext()){
 String aux=s1.next();
 System.out.println(aux);
 }
 s1.close();
 return myWords;
}

Can I change this code to return a String []?

asked Mar 15, 2018 at 3:01
4
  • docs.oracle.com/javase/8/docs/api/java/util/… Commented Mar 15, 2018 at 3:03
  • 2
    "Can I change this code to return a String []?" - Yes, but. You will need to know the number lines you are going to before you can declare the array. Alternately, you could read the content into the ArrayList and then use this to create a new array of Strings (as you will know how many elements you need). On a side note. You should not be referencing src at all, it won't exist once the program is compiled and exported. You should also not be reading from s1 if there was an error, this will create a NullPointerException Commented Mar 15, 2018 at 3:04
  • see this question , that's the same Commented Mar 15, 2018 at 3:05
  • I know the number of lines, so I think I can do it! Should I do create the object String array? With the size and that stuff? Or how are you suggesting? Can you do code please? It might help a lot. Commented Mar 15, 2018 at 3:10

3 Answers 3

2

You can call List.toArray(String[]) to convert the List<String> to a String[]. I would also prefer a try-with-resources over explicitly closing the Scanner and a List<String> interface. Something like,

public static String[] readFile() { // <-- You could pass File myFile here
 File myFile = new File("./src/folder/words.txt");
 // Creates ArrayList to store each String aux
 List<String> myWords = new ArrayList<>();
 try (Scanner s1 = new Scanner(myFile)) {
 while (s1.hasNext()) {
 String aux = s1.next();
 System.out.println(aux);
 }
 } catch (FileNotFoundException e) {
 System.out.println("File not found");
 e.printStackTrace();
 }
 return myWords.toArray(new String[0]);
}
answered Mar 15, 2018 at 3:05
Sign up to request clarification or add additional context in comments.

2 Comments

I'm importing this: import java.awt.List; but there's an error in the line 5 "List"
@Pedro import java.util.List; - Please click on the JavaDoc link in my answer.
2

try using a built in function of collections class .

 ArrayList<String> stringList = new ArrayList<String>();
 stringList.add("x");
 stringList.add("y");
 stringList.add("z");
 stringList.add("a");
 /*ArrayList to Array Conversion */
 /*You can use the toArray method of the collections class and pass the new String array object in the constructor while making a new String array*/
 String stringArray[]=stringList.toArray(new String[stringList.size()]);
 
 for(String k: stringArray)
 {
 System.out.println(k);
 }
answered Oct 1, 2020 at 15:38

Comments

1

Add this at last:

String [] arr = myWords.toArray(new String[myWords.size()]);
return arr;

Or simply,

return myWords.toArray(new String[myWords.size()]);
answered Mar 15, 2018 at 3:06

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.