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.