First I want to say hey, new here, hopefully this places is welcoming!

Second, I am in a java class and it is my first class and my only experience with programming. So the questions I will be asking will be based off of my assignments.

What I want to get from this, is help understanding what I am doing/not doing to get the programs to run as they should.

What I don't want is for others to do my assignments, I want to do what I can to learn this language.

So with that, here is my first question (dumb it down if you have to):

1.Allow for input by a user of the three strings to be compared and sorted. We also must use a another class and call upon it to run in the main.

//==========================================main============================================
package sortstrings;

import java.util.Scanner;



    public class SortStrings {



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

      System.out.println("Please input 3 strings");
      String string1 = inputStrings.next();
      String string2 = inputStrings.next();
      String string3 = inputStrings.next();

      Stringsort sorting = new Stringsort(string1, string2, string3);
      sorting.getStringOrder();

    }
}
//=====================================end main=================================




//=================================Stringsort class=============================
package sortstrings;


public class Stringsort {
    private String word1;
    private String word2;
    private String word3;

    Stringsort (String string1, String string2, String string3){

        word1 = string1;
        word2 = string2;
        word3 = string3;

}
    public void getStringOrder(){

        if(word1.compareToIgnoreCase(word2) < 0 && 
                word1.compareToIgnoreCase(word3) < 0){
            System.out.println(word1);
        }
        else if(word1.compareToIgnoreCase(word2) > 0 && 
                word2.compareToIgnoreCase(word3) < 0){
            System.out.println(word2);
        }
        else{
            System.out.println(word3);
        }
        if(word2.compareToIgnoreCase(word1) < 0 && 
                word2.compareToIgnoreCase(word3) > 0){
            System.out.println(word2);    
        }
        else if(word2.compareToIgnoreCase(word1) > 0){
            System.out.println(word2);
        }
        else if(word3.compareToIgnoreCase(word1) < 0){
            System.out.println(word2);
        }
        else{
            System.out.println(word1);
        }
    }

}

I have done my reading and I just can't grasp the concept of what I need to do. Everytime I put in the strings I get it to run but I will try every combination and I'll get it to print the same word twice. I guess I just don't get the algorithm for this particular question.

I understand that you are new to the language, but for something like this you need to use an array or something similar. If you haven't learned about arrays or java.util.List, then you should either learn one of them now or choose a different project to learn on.

You should never represent a list like thing1, thing2, thing3, etc. It can sometimes work if there are only two things, but for any more than that you are just creating pointless work and confusion for yourself. The confusion you are having now is entirely created by trying to use word1, word2, word3 instead of a single array of String called words. Trying to figure out where your getStringOrder goes wrong is not worth the trouble.

You would be much better off if you started with something like this:

public class Stringsort {
    private final String[] words;
    public Stringsort(String... words) {
        this.words = words;
    }
    public void getStringOrder() {
        // Do sorting then printing here
    }
}

I must mention that getStringOrder is brutally misnamed. Any method that starts with get should have a return value. Please consider changing the name of that method to printStringOrder or printSortedStrings.

Now that you are using an array you can easily sort it without any work by using java.util.Arrays.sort, but I expect that you want to do it yourself as an exercise. Even so, it will be much easier with an array.

Edited 3 Years Ago by bguild

bguild- Thank you so much.... I went and did some reading on Arrays and with your help got it figured out. My teacher has mentioned Arrays, but hasn't explained them so I didn't even think to look them up.

Thanks again!

Also to add we were working with if/else statements this week that is why I had all of that in there. I was assuming it was suppose to be done that way. However I think as long as the assignment runs as it should he shouldn't have any issues.

I know we have students in class that have been programming for awhile and I'm sure they use tricks they know.

This article has been dead for over six months. Start a new discussion instead.