1

I have this program reads a file made up with words line by line into my Arraylist, then I want to print this ArrayList on to my console also line by line, however, I can only manage to print 1 word from that list (you can understand why from my code below). I would appreciate all kinds of input.

*Note1: I have tested both my reader method and my ArrayList they seem to work well, I can see all the elements that are added into my ArrayList.

*Note2: I could just print out with print-stream however I can't change the method signature, thus I have to return String with this method and I could not find any way to print the whole list on to my console.

*Edit: To clarify I do not want to print the list, I want this method to pass the String (as a list with all the elements) to another class's method that puts this String I returned from this method on to text box I created with GUI. Therefore I need this method to "return String(arStr)" somehow but I could not figure out yet.

public static String toStringFromArrayList(ArrayList<String> arStr) {
    String result = null;
    for (String s : arStr) {
        result = s;

    }
    return result;
}

** This is what I meant to do if anyone needs a similar solution can check it below **

public static String toStringFromArrayList (ArrayList<String> arStr) {

    String result ="";



     for (String s: arStr) {



      result += s+"\n";



     }

return result;
4
  • Read this article for some tips on debugging your code. This will help you understand what your code is doing. Commented Dec 2, 2019 at 23:35
  • 3
    You do not need toString() for a String. Commented Dec 2, 2019 at 23:36
  • To just print the whole list, you can do System.out.println(arStr);. This will print the entire list on one line, though. Commented Dec 2, 2019 at 23:39
  • You have to define the logic, how the list of multiple strings shall become a single string. Is return arStr.toString(); the right thing? Or rather return String.join("\n", arStr);? We can't know what you want. Commented Dec 3, 2019 at 15:33

2 Answers 2

2

You are only returning the last String and aren't printing anything. This prints every element of the the ArrayList n ints own line

public static void toStringFromArrayList (ArrayList arStr) {
    for (String s: arStr) {
        System.out.println(s);
    }
}

This should solve your problem, let me know if you need further assistance.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the answer, yes I could use that, however, my method requires me to have a return String statement, otherwise, I get an error saying "This method must return a result of type String"
@Gwaithim No problem. That is because your method is of type string. If you look at my code it is of type void so I don't need to return anything. Try replacing your existing method with mine and try again, let me know if it doesn't work or if you need further assistance :) - Ankit
Hey Ankit thanks for follow up. That method meant to return String so I couldnt change it but I found another way to return String line by line :)
2

You are assigning each element to result in te loop. Then you return the last assignment, hence you only print out one word

For example: Say you have the words "this, that, so, and she" in arStr then she will be the last word assigned to the 'result' which you then return.

Also, if you want to assign null to result then you shoud omit the "" around null, because now you are assigning the String value "null" to the variable results instead of a null value

Edit: One last tip, if you are using Java 8 or higher you can also use the forEach() method of the arraylist to print each string value. This is how you would do it.

arStr.forEach(element -> System.out.println(element));

If you use this you don't need the method you've made anymore.

4 Comments

Thanks for the input, I 've found that method however as my method signature requires me to return String, I cant use the method you suggested as that method returns void. When I try to implement that I get an error stating "This method must return a result of type String" Maybe just me being a rookie.
@Gwaithim you don't need your method anymore, you can use forEach in its place, all you have to do is print the words in the list no? So use forEach at the place where you are no calling your own method and delete your own method
also it is good practice to use the superclass List<String> as the argument datatype, instead of ArrayList
I have another class expecting String from that method, I am working on GUI , that method returning string to my another class that builds an interface with a text-box then I sort them etc. Thus I need to keep the method itself with the method signature.

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.