-2

Im trying to make a persons name be printed in alphabetical/reverse alphabetical order. I have this so far:

  Scanner name = new Scanner(System.in);
  String N1;                             
  String N2;                            
  String N3;                             
  double az_za;                         

   System.out.print("First name: ");
     N1 = name.nextLine();                    
   System.out.print("Middle name: ");
     N2 = name.nextLine();                  
   System.out.print("Last name: ");
     N3 = name.nextLine();                    

   System.out.print("Enter 1 (alphabetical order) or 2 (reverse alphabetical order):");
     az_za = name.nextDouble();                

String names[] = {N1, N2, N3}

Ive tried using a string but i wasnt sure where to go with that as you can see. I also tried using

if (N1.compareTo(N2) < 0) && (N1.compareTo(N3) < 0)

but i wasnt sure what to put in the {} as in what do I put equal to what to isolate the names alphabetically.

I HAVE TRIED EVERYTHING MY ABILITIES ALLOW. I researched and looked up so many things but none helped my case, so dont say "what have you tried, were not gonna do your homework for you". Btw I also tried using arrays and char(?) but we havent learned that in class so i dont think we can use those.

TY in advance

EDIT:

Ok so i think i got it:

String names[] = {N1, N2, N3};
         if (az_za == 1) {
         Arrays.sort(names);
        } else if (az_za == 2) {
             Arrays.sort(names, Collections.reverseOrder());

From here it all compiles but im not sure what to put in the actual print statements here "____"

      if (az_za == 1) {              
         System.out.println("Your name in alphabetical order is " + __________);  
     }if (az_za == 2) {               
         System.out.println("Your name in reverse alphabetical order is " +_____________);  

Because if i put what was in the Arrays.sort(HERE) its a mess...

Tom
  • 16,842
  • 17
  • 45
  • 54
JNV
  • 5
  • 2
  • 8
  • `String names[] = {N1, N2, N3}` *is* an array. – shmosel Mar 03 '17 at 02:28
  • 2
    Just call `Arrays.sort(names)`. – shmosel Mar 03 '17 at 02:28
  • 2
    Alphabetically as in rearrange just the names? Or the letters within the names? – Peter Mar 03 '17 at 02:30
  • I am trying to alphabetize the names as a whole Ex) John Adam Smith -> Adam John Smith and vv for reverse alphabetical order – JNV Mar 03 '17 at 02:35
  • see if this helps... i cant comment because less than 50 rep lol http://stackoverflow.com/questions/12681103/java-alphabetizing-strings – Talking Mango Mar 03 '17 at 02:37
  • most of the solutions below will work. Not that they all change the value in the array. If you care just about output. I suggest you use stream e.g: http://stackoverflow.com/questions/28607191/how-to-use-a-java8-lambda-to-sort-a-stream-in-reverse-order – Bojan Petkovic Mar 03 '17 at 19:16

3 Answers3

3

Add this to bottom.

if (az_za == 1) {
    Arrays.sort(names);
} else if (az_za == 2) {
    Arrays.sort(names, Collections.reverseOrder());
} 

for (String namePart: names) {
    System.out.print(namePart + "   ");
}
Wasi Ahmad
  • 35,739
  • 32
  • 114
  • 161
Nick Ziebert
  • 1,258
  • 1
  • 9
  • 17
  • For some reason it keeps saying cannot find symbol for variable Arrays. Do you know what this means? – JNV Mar 03 '17 at 02:54
  • Yeah, add this to top, before class. import java.util.Arrays; import java.util.Collections; import java.util.Scanner; – Nick Ziebert Mar 03 '17 at 02:55
0

Here is the easiest way to do this using the Java8.

public class SortNames {    
    public static void main(String[] args) {
        List<String> names = Arrays.asList("Steven", "Allen", "Bo");
        names.sort(String::compareToIgnoreCase);
        System.out.println(names);
    }    
}

The output looks like this.

[Allen, Bo, Steven]
Wasi Ahmad
  • 35,739
  • 32
  • 114
  • 161
Ravindra Ranwala
  • 20,744
  • 6
  • 45
  • 63
0
String[] arr = { "d", "a", "b", "c" };
Arrays.sort(arr); // for alphabetical order
Arrays.sort(arr, (a, b) -> b.compareTo(a)); // for reverse alphabetical order
System.out.println(Arrays.toString(arr));
Wasi Ahmad
  • 35,739
  • 32
  • 114
  • 161
Hatem
  • 31
  • 3