How to Format Method Returns With Commas in Java
Java methods are functions that perform some sort of work for an object, often returning some sort of output. This output can come in the form of other objects, numbers, letters or strings. In many cases, you might wish to format the output of a method by separating different sections of the output based on a deliminator symbol such as a comma. In this case, you can use the String class's "split" method to separate the data based on comma placement and then format the output data.
Instructions
-
-
1
Call an object's method and store the return value in string variable. This assumes the method "func" returns a string value:
FakeObject f = new FakeObject();
String s = new String();
s = f.func(); -
2
Declare a string array:
String[] strings = new String[10]; //array of 10 strings
-
-
3
Split the String "s" into sub-strings, delimited by the commas in String "s":
strings = s.split(",");
-
4
Format by column:
for (String x : strings){
System.out.println(x);
}
-
1