I am trying to create a void method that uses 3 string parameters and puts the strings in alphabetical order. So far I have used if statements and I believe the if statements are correct however I keep getting a message that says "void cannot be converted into string" I am suppose to use a void method and I am very confused this is my code
public class AlphabeticalOrder {
public static void inOrder(String s1, String s2, String s3) {
if (s1.compareTo(s2) < 0 && s1.compareTo(s3) < 0)
if (s2.compareTo(s3) < 0)
System.out.println(s1 + s2 + s3);
else
System.out.println(s1 + s2 + s3);
else if (s2.compareTo(s1) < 0 && s2.compareTo(s3) < 0)
if (s1.compareTo(s3) < 0)
System.out.println(s2 + s1 + s3);
else
System.out.println(s2 + s3 + s1);
else if (s3.compareTo(s1) < 0 && s3.compareTo(s2) < 0)
if (s2.compareTo(s1) < 0)
System.out.println(s3 + s2 + s1);
else
System.out.println(s3 + s1 + s2);
}
public static void main(String[] args) {
String ans1 = inOrder("abc", "mno", "xyz");
System.out.println(ans1);
}
}
Change your main method to this:
public static void main(String[] args)
{
inOrder("abc", "mno", "xyz");
}
Your function returns "void", which means "nothing", so you can't assign it to a variable or print it.
The better way to do this is almost certainly to have your method return a String[]
, but if your assignment is to return void
, then this is the best you've got.