how to convert a result of a method to String in Java?

Viewed 80

Here is the code, that works:

public class WorkshopApplicationMyCollection {
static void listOfPeople(List<String> someList) {
    for (int i = 0; i<someList.size(); i++){
        if (i == someList.size()-1) {
            System.out.print(someList.get(i) + ". \n");
        } else {
            System.out.print(someList.get(i) + ", ");
        }
    }
}
public static void main(String[] args) {

    List<String> listOfPeople = new ArrayList<>();
    listOfAuthorities.add("Musk");
    listOfAuthorities.add("Davinchie");
    listOfAuthorities.add("Gates");



    System.out.print("Number of people " + listOfPeople.size() + ". Those people are ");

    listOfPeople(listOfAuthorities);

    
}

}

What I'm trying to do is to make a result of a method listOfPeople() to be a string by itself so that I could put it into the sentence. when I'm doing like this:

System.out.print("Number of people " + listOfPeople.size() + ". Those people are " + listOfPeople(listOfAuthorities));

the IDE says that the method has to not be void. How to make the method return string on it's own?

4 Answers

If you want to use the method in System.out.println it should return String instead of void. Think of void as a procedure that doesn't return anything. You want a method that returns String which can be used as a subject of printing out to a console.

(I prefer String.join(CharSequence delimiter, Iterable elements) to chain String elements:

static String listOfPeople(List<String> someList) {
    return String.join(", ", someList) + ". \n";
}

Then it shall be used as:

System.out.print("Number of people " + listOfPeople.size() + ". Those people are " + listOfPeople(listOfPeople));

Your method should return a String, not call System.out.println directly. A common way of doing this is streaming the list and then joining it:

static String listOfPeople(List<String> someList) {
    return someList.stream().collect(Collectors.joining(", ", "", ". \n"));
}

Well, a void function obviously returns nothing.

Here is a simple generic approach without using a Stream:

public static String flattenList(List<?> list)
    {
        StringBuilder sb = new StringBuilder();
        for(int i = 0; i<list.size(); i++)
        {
            sb.append(list.get(i));
            if(i < list.size()-1) sb.append(",");
            else sb.append("\n");
        }
        
        return sb.toString();
    }

If you just want to output the individual values, you can use something like that:

people.stream().forEach(System.out::println);

Use String.join("delimiter", list).

You coded wrong some name of variables, so I've corrected to you. Summarizing:

import java.util.ArrayList;
import java.util.List;

public class WorkshopApplicationMyCollection {

    public static void main(String[] args) {

        List<String> listOfAuthorities = new ArrayList<>();
        listOfAuthorities.add("Musk");
        listOfAuthorities.add("Davinchie");
        listOfAuthorities.add("Gates");

        System.out.print("Number of people " + listOfAuthorities.size() + ". Those people are " + String.join(" and ", listOfAuthorities));

    }
}
Related