/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package complexstringanalysis;
import java.util.Scanner;
public class ComplexStringAnalysis {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter String One: ");
String stringOne = input.next();
System.out.println("Enter String Two: ");
String stringTwo = input.next();
System.out.println("Enter String Three: ");
String stringThree = input.next();
if (stringOne.length() > stringTwo.length() && stringOne.length()
> stringThree.length()) {
System.out.println(" Longest string is " + stringOne + " with a "
+ "length of " + stringOne.length());
}
if (stringTwo.length() > stringOne.length() && stringTwo.length()
> stringThree.length()) {
System.out.println(" Longest string is " + stringTwo + " with a "
+ "length of " + stringTwo.length());
}
if (stringThree.length() > stringOne.length() && stringThree.length()
> stringTwo.length()) {
System.out.println(" Longest string is " + stringThree + " with a "
+ "length of " + stringThree.length());
}
/**
* Now that the lengths themselves are complete, we will add in the
* clause if two are both the longest.
*/
if (stringOne.length() == stringTwo.length() && stringOne.length() > stringThree.length()) {
System.out.println(" Longest string is " + stringOne + " with a "
+ "length of " + stringOne.length());
}
if (stringOne.length() == stringThree.length() && stringOne.length() > stringTwo.length()) {
System.out.println(" Longest string is " + stringOne + " with a "
+ "length of " + stringOne.length());
}
if (stringTwo.length() == stringOne.length() && stringTwo.length() > stringThree.length()) {
System.out.println(" Longest string is " + stringTwo + " with a "
+ "length of " + stringTwo.length());
}
if (stringTwo.length() == stringThree.length() && stringTwo.length() > stringOne.length()) {
System.out.println(" Longest string is " + stringTwo + " with a "
+ "length of " + stringTwo.length());
}
if (stringThree.length() == stringOne.length() && stringThree.length() > stringTwo.length()) {
System.out.println(" Longest string is " + stringThree + " with a "
+ "length of " + stringThree.length());
}
if (stringThree.length() == stringTwo.length() && stringThree.length() > stringOne.length()) {
System.out.println(" Longest string is " + stringThree + " with a "
+ "length of " + stringThree.length());
}
/**
* We have now finished the clause. We will now begin alphabetization.
* ex. S1.compareTo(S2), > 0 if s1 comes after s2. = 0 is s1 = s2. < 0
* if s1 comes before s2.
*/
String stringOneLower = stringOne.toLowerCase();
System.out.println("stringOneLower = " + stringOneLower);
String stringTwoLower = stringTwo.toLowerCase();
System.out.println("stringTwoLower = " + stringTwoLower);
String stringThreeLower = stringThree.toLowerCase();
System.out.println("stringThreeLower = " + stringThreeLower);
int oneToTwo = stringOneLower.compareTo(stringTwoLower);
System.out.println("oneToTwo = " + oneToTwo);
int oneToThree = stringOneLower.compareTo(stringThreeLower);
System.out.println("oneToThree = " + oneToThree);
int twoToOne = stringTwoLower.compareTo(stringOneLower);
System.out.println("twoToOne = " + twoToOne);
int twoToThree = stringTwoLower.compareTo(stringThreeLower);
System.out.println("twoToThree = " + twoToThree);
int threeToOne = stringThreeLower.compareTo(stringOneLower);
System.out.println("threeToOne = " + threeToOne);
int threeToTwo = stringThreeLower.compareTo(stringTwoLower);
System.out.println("threeToTwo = " + threeToTwo);
System.out.println("Words in ABC order: ");
if (oneToTwo < 0 && oneToThree < 0) {
System.out.println("First: " + stringOne);
}
if (oneToTwo > 0 && oneToThree < 0) {
System.out.println("Second: " + stringOne);
}
if (oneToTwo < 0 && oneToThree > 0) {
System.out.println("Third: " + stringOne);
}
if (twoToThree < 0 && twoToOne < 0) {
System.out.println("First: " + stringTwo);
}
if (twoToThree > 0 && twoToOne < 0) {
System.out.println("Second: " + stringTwo);
}
if (twoToThree < 0 && twoToOne > 0) {
System.out.println("Third: " + stringOne);
}
if (threeToOne < 0 && threeToTwo < 0) {
System.out.println("First: " + stringTwo);
}
if (threeToOne > 0 && threeToTwo < 0) {
System.out.println("Second: " + stringTwo);
}
if (threeToOne < 0 && threeToTwo > 0) {
System.out.println("Third: " + stringOne);
}
}
}
Hi all. I am having quite the logical issue here. My code seems to work fine up until the last section, where I attempt to print the first, second, and third (ABC order) words. When trying to run, it prints sometimes the first and third, the second and third, only the first, none at all, and so on. I was told the error is with my logic (specifically something about the lines containing &&?) and I have been unable to figure out what is going on.
It's made clear in the class that we should not steal code, so could someone answer with the intention to teach rather than to give me the code itself? Such as pointing the problem itself out to me, but letting me fix it on my own, or at least trying to?
It may be an issue of me needing to nest my if statements, but I am unsure of how I would change my current format up to suit that.
if (threeToOne > 0 && threeToTwo < 0) {
System.out.println("Second: " + stringTwo);
Take this line for instance. My logic here is that it compares the length of string three to string one and two. With the two comparison symbols it decides if it's the second or not, and would then print such. I am lost and would appreciate any help.
Aucun commentaire:
Enregistrer un commentaire