0

I'm trying to have the scanner scan for each city, analyze the first character and arrange it alphabetically. I couldn't figure out how to use a for statement for this task and I've googled a lot, but now I'm thinking that would've been the right option. Unless an If Else would work which even under correct conditions, it still runs else instead. If I could use a For() how would I use it, if not how else could I get this to work.

For the sample cities I used "Austin [enter] Chicago [Enter] Denver [Enter]".

edit: let me clarify I added the "Error" part of the else to test the If argument.

package homework2;

    import java.util.Scanner;

    public class OrderCities {

public static void main(String[] args) {
    // Create a scanner
    Scanner scanner = new Scanner(System.in);

    // Prompt user to input cities
    System.out.println("Please enter each city");
    String c1 = scanner.nextLine();
    String c2 = scanner.nextLine();
    String c3 = scanner.nextLine();

    int first = 0;
    int i1 = c1.charAt(first);
    int i2 = c2.charAt(first);
    int i3 = c3.charAt(first);

    if ((i1 > i2 && i1 > i3) && i2 > i3){
        System.out.println(c1 + " " + c2 + " " + c3);
    } else
        System.out.println("Error");



        }

    }

So My Professor told me I had it correct and found that the signs [Greater than, Less than] should be swapped since the program reads them in Unicode. He added System.out.println(i1 + " " + i2 + " " + i3); at int i2 = c2.charAt(first); int i3 = c3.charAt(first); System.out.println(i1 + " " + i2 + " " + i3);

if (i1 < i2 && i1 < i3){
    if (i2 < i3){
        System.out.println(c1 + ", " + c2 + ", " + c3);` 

to have it read out the Unicode. Thanks everyone for your help! Here's what my original code came out to (this is just for documentation incase any other beginner like me has similar issues):

` package homework2;

        import java.util.Scanner;

        public class OrderCities {

    public static void main(String[] args) {
        // Create a scanner
        Scanner scanner = new Scanner(System.in);

        // Prompt user to input cities
        System.out.println("Please enter each city");
        String c1 = scanner.nextLine();
        String c2 = scanner.nextLine();
        String c3 = scanner.nextLine();

        int first = 0;
        int i1 = c1.charAt(first);
        int i2 = c2.charAt(first);
        int i3 = c3.charAt(first);
        System.out.println(i1 + " " + i2 + " " + i3);

        if (i1 < i2 && i1 < i3){
            if (i2 < i3){
                System.out.println(c1 + ", " + c2 + ", " + c3);
            }else
                System.out.println(c1 + ", " + c3 + ", " + c2);
        } else if (i2 < i1 && i2 < i3){
            if (i1 < i3){
                System.out.println(c2 + ", " + c1 + ", " + c3);
            }else
                System.out.println(c2 + ", " + c3 + ", " + c1);
       } else if (i3 < i1 && i3 < i2){
            if (i2 < i1){
                System.out.println(c3 + ", " + c2 + ", " + c1);
            }else
                System.out.println(c3 + ", " + c1 + ", " + c2);
        } else
            System.out.println("ROFLOL, there seems to be an 3RR0R, which means you somehow found a way to break my code!! TR0LL0L0L0L0L");
            }





}

3 Answers3

2

This should have everything you need: How can I sort a List alphabetically?

There's no need to pull the first char for comparison. Java's collections can do the work.

(Throw strings into a new ArrayList<String>(); or any other collection.)

Community
  • 1
  • 1
mattklamp
  • 293
  • 4
  • 13
  • But how do I have it insert the user's input into an array? – GarryHasQuestions Mar 01 '15 at 03:16
  • I'd say the use of Scanner/charAt/printing demonstrates beyond basics. Shrug. – mattklamp Mar 01 '15 at 03:16
  • 1
    Quick and easy: List stringList = new ArrayList(); stringList.put(STRING); – mattklamp Mar 01 '15 at 03:18
  • @mattklamp, I thing I found the CharAt on google. Will that code automatically allow input? – GarryHasQuestions Mar 01 '15 at 03:22
  • can someone show me an example of how I would use it? I can't get it to work. I even checked Oracle's website. this is what I currently have somehow because the others weren't working and threw some errors. – GarryHasQuestions Mar 01 '15 at 04:59
  • @GarryHasQuestions see http://tutorials.jenkov.com/java-collections/list.html it's short and has all the basic info you need for using lists. Also for more advanced but complete info see http://docs.oracle.com/javase/7/docs/api/java/util/List.html . put it together with the sorting link in this answer and you'll be good to go. – Jason C Mar 01 '15 at 05:11
0

I liked your first approach, the only thing I saw that made this harder than it needed to be was how you were checking the first letter of all the inputs taken by the scanner (what if two cities have the same first letter, uh-oh).

Your second approach is better in that you used a List (ArrayList is a fine choice) to collect the input and it seems you're trying to sort it based on the contents of the list. It's a good thing to know that Java has built-in classes that can do operations on certain types of lists (the javadoc is a beautiful tool). Looking at the skill level required for your assignment I doubt you'll need to have a synchronized anything, let alone a list.

Take a look at the Collections class and have it perform the neccessary operation on your list of cities. The answer is in the javadoc, I don't want to give away too much ;)

mastrgamr
  • 631
  • 1
  • 11
  • 21
0

Use Array.sort(yourArray), it will return yourArray alphabetically.