How do I put four strings in alphabetical order? I haven't learned how to use arrays yet and I think I'm supposed to use something with "compareTo". Where do I start?
if(stringA < stringB) then
if(stringB < stringC) then
//The ordering is A->B->C
else
if(stringA < stringC) then
//The ordering is A->C->B
else
//The ordering is C->A->B
else
if(stringC < stringB) then
//The ordering is C->B->A
else
if(stringC < stringA) then
//The ordering is B->C->A
else
//The ordering is B->A->C
OK. That seems confusing, so it might be better if I learned what arrays are and try it that way. I have to prompt for four strings and display the one that comes first in alphabetical order.
package alphabetical;
import java.util.*;
public class Alphabetical
{
static boolean compareTo (Scanner scan)
{
String string1, string2, string3;
System.out.print("Enter name 1:");
string1= scan.nextLine();
System.out.print("enter name 2: ");
string2= scan.nextLine();
System.out.print("enter name 3: ");
string3= scan.nextLine();
if(string2.compareTo(string1))
package alphabetical;
import java.util.*;
public class Alphabetical
{
public boolean precedes (String s1, String s2, String s3)
{
Scanner scan = new Scanner (System.in);
System.out.print("Enter name 1:");
s1= scan.nextLine();
System.out.print("enter name 2: ");
s2= scan.nextLine();
System.out.print("enter name 3: ");
s3= scan.nextLine();
if (s1.charAt(k) = s2.charAt(k))
return s1.charAt(k) < s2.charAt(k);
}
return sl.length() < s2.length();
}
Is it something like this? This was on the compareTo method, and I'd thought I'd try it. I used precedes instead of compareTo.
Code:package alphabetical; import java.util.*; public class Alphabetical { public boolean precedes (String s1, String s2, String s3) { Scanner scan = new Scanner (System.in); System.out.print("Enter name 1:"); s1= scan.nextLine(); System.out.print("enter name 2: "); s2= scan.nextLine(); System.out.print("enter name 3: "); s3= scan.nextLine(); if (s1.charAt(k) = s2.charAt(k)) return s1.charAt(k) < s2.charAt(k); } return sl.length() < s2.length(); }[/QUOTE] You've got to think a bit more about the design. What does precedes return? You've passed in 3 strings (though you then throw away their values and get new ones from your Scanner), so what does a true return represent? What does a false represent? You need to know which of the 3 (or 4, you stated earlier) is the first alphabetically. Also, charAt is not necessary for string comparison. There's already a string method that does comparisons on the whole strings for you. Also, in an if you have an assignment statement. This will not compile in Java. In some other languages this would always evaluate to true, in some it will evaluate to true unless the value being assigned is 0. == is the equivalence operator. You also have a return statement outside of a method. This will not compile, either. If you are required to use charAt and length to determine which String is first alphabetically, you can. I would put this in a separate method. -Lee
package alphabetical;
import java.util.*;
public class Alphabetical
{
public boolean precedes (Scanner scan)
{
Scanner scan = new Scanner (System.in);
String s1, s2, s3;
System.out.print("Enter name 1:");
s1= scan.nextLine();
System.out.print("enter name 2: ");
s2= scan.nextLine();
System.out.print("enter name 3: ");
s3= scan.nextLine();
if (s1.compareTo(null) == s2.compareTo(null))
return s1.compareTo(null) < s2.compareTo(null);
}
return sl.length() < s2.length();
}
Any closer? I have no idea what I am doing.
Code:package alphabetical; import java.util.*; public class Alphabetical { public boolean precedes (Scanner scan) { Scanner scan = new Scanner (System.in); String s1, s2, s3; System.out.print("Enter name 1:"); s1= scan.nextLine(); System.out.print("enter name 2: "); s2= scan.nextLine(); System.out.print("enter name 3: "); s3= scan.nextLine(); if (s1.compareTo(null) == s2.compareTo(null)) return s1.compareTo(null) < s2.compareTo(null); } return sl.length() < s2.length(); }[/QUOTE] It will not help you learn anything if this is designed for you, so you need to work that out on your own. What should your function return? (Hint: What does your program need to display?) String's compareTo method will compare the String it is called on to something else. Passing null to compareTo will result in a NullPointerException. Look at this documentation: http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html#compareTo(java.lang.String) What is the return value, and what does it represent? What is the parameter that is passed in? Stop writing code and start thinking about the design. If you don't have any idea what's going on, try to fix that before you start writing your program. Try to think about this independently of a computer program. If you are asking someone to give you some names. They give you the name susie. You have to remember that name. You ask them for another name, they give you abe. What do you need to remember? What do you need to decide about abe and susie? -Lee
I give up on this one. Thanks for trying, though.
OK, I'm trying again. My function should return the string that comes first in alphabetical order. I need three strings to compare. I should compare the first string with the second string. The one that comes first in alphabetical order should be compared to the third string. The one that comes first out of that comparison is the one that comes first of all three strings. That is the one my program needs to display.
ackage alphabetical;
import java.util.*;
public class Alphabetical
{
static boolean precedes (Scanner scan)
{
int s1, s2, s3;
int firstAlphabetical = 0;
if (s1 > s2)
else if (s1 > s3){
firstAlphabetical = s1;}
else if (s3 > s1){
firstAlphabetical = s3;}
if (s2 > s1)
else if (s2 > s3){
firstAlphabetical = s2;}
else if (s3 > s2){
firstAlphabetical = s3;}
}
public static void main(String[] args)
{
Scanner sc = new Scanner (System.in);
int firstAlphabetical = 0;
double s1, s2, s3;
//user input
System.out.print("Enter name 1:");
s1 = sc.nextDouble();
System.out.print("enter name 2: ");
s2= sc.nextDouble();
System.out.print("enter name 3: ");
s3= sc.nextDouble();
//answer
System.out.println("First alphabetically: " + firstAlphabetical);
}
}
Here is what I tried. I get two errors that say, "Syntax error on "else", delete this token." I don't use the compareTo method as the teacher recommended, either.
Code:ackage alphabetical; import java.util.*; public class Alphabetical { static boolean precedes (Scanner scan) { int s1, s2, s3; int firstAlphabetical = 0; if (s1 > s2) else if (s1 > s3){ firstAlphabetical = s1;} else if (s3 > s1){ firstAlphabetical = s3;} if (s2 > s1) else if (s2 > s3){ firstAlphabetical = s2;} else if (s3 > s2){ firstAlphabetical = s3;} } public static void main(String[] args) { Scanner sc = new Scanner (System.in); int firstAlphabetical = 0; double s1, s2, s3; //user input System.out.print("Enter name 1:"); s1 = sc.nextDouble(); System.out.print("enter name 2: "); s2= sc.nextDouble(); System.out.print("enter name 3: "); s3= sc.nextDouble(); //answer System.out.println("First alphabetically: " + firstAlphabetical); } }[/QUOTE] OK. So you are going to write a precedes function, and you aren't going to use String's compareTo function to accomplish it. This is absolutely fine. Precedes can only tell you information about two things at a time, though. Either x precedes y or y precedes x. So you need to have a function that operates on two things, and returns a boolean. You cannot use < and > with strings, as this will be comparing their memory locations. This has no relevance in terms of their alphabetical or lexicographical order. you started getting towards an implementation of precedes before, but were still a little off. You can use charAt to compare individual characters of a string. You have to be careful about how many characters you compare, though. You need to make sure you don't go past the end of the shorter string. If you find that two strings are identical to the length of the shortest, you can say that the shorter precedes the longer at that point. Also note, you are being asked to compare alphabetically rather than lexicographically. This is important because B is less than a lexicographically, but not alphabetically (case matters lexicographically). Once you have your precedes function properly comparing two strings and returning whether the first precedes the second alphabetically, it gets pretty simple. In pseudocode: lowest = a if precedes(b,lowest) lowest=b if precedes(c,lowest) lowest=c At the end, lowest will be the first alphabetically. -Lee
package alphabetical;
import java.util.*;
public class Alphabetical
{
static boolean precedes (Scanner scan)
{
string s1, s2, s3;
if (s1.precedes(s2))
return true;
else;
return false;
if (s1.precedes(s3))
return true;
else;
return false;
if (s2.precedes(s3))
return true;
else;
return false;
}
public static void main(String[] args)
{
Scanner sc = new Scanner (System.in);
int firstAlphabetical = 0;
double s1, s2, s3;
//user input
System.out.print("Enter name 1:");
s1 = sc.nextDouble();
System.out.print("enter name 2: ");
s2= sc.nextDouble();
System.out.print("enter name 3: ");
s3= sc.nextDouble();
//answer
System.out.println("First alphabetically: " + firstAlphabetical);
}
}
I tried using precedes, but making if then statements aren't working too well. This is what I've got.
Code:package alphabetical; import java.util.*; public class Alphabetical { static boolean precedes (Scanner scan) { string s1, s2, s3; if (s1.precedes(s2)) return true; else; return false; if (s1.precedes(s3)) return true; else; return false; if (s2.precedes(s3)) return true; else; return false; } public static void main(String[] args) { Scanner sc = new Scanner (System.in); int firstAlphabetical = 0; double s1, s2, s3; //user input System.out.print("Enter name 1:"); s1 = sc.nextDouble(); System.out.print("enter name 2: "); s2= sc.nextDouble(); System.out.print("enter name 3: "); s3= sc.nextDouble(); //answer System.out.println("First alphabetically: " + firstAlphabetical); } }[/QUOTE] Precedes needs to take 2 string values as its arguments. s1 in main is not at all the same as s1 in precedes. They are in a different scope. You need to pass s1 and s2 (for example) as arguments to precedes, and return true if s1 is alphabetically less than s2, and false otherwise. The syntax of if-else can be found here: http://java.sun.com/docs/books/tutorial/java/nutsandbolts/if.html You need to get some {}s going to put your conditional statements in, and there are no ;s needed. -Lee
package alphabetical;
import java.util.*;
public class Alphabetical
{
static boolean precedes (String s1, s2, s3)
{
if (s1.precedes(s2))
return true
else
return false
if (s1.precedes(s3))
return true
else
return false
if (s2.precedes(s3))
return true
else
return false
}
public static void main (Scanner scan)
{
Scanner sc = new Scanner (System.in);
int firstAlphabetical = 0;
//user input
System.out.print("Enter name 1:");
String s1 = sc.nextLine();
System.out.print("enter name 2: ");
String s2= sc.nextLine();
System.out.print("enter name 3: ");
String s3= sc.nextLine();
//answer
System.out.println("First alphabetically: " + firstAlphabetical);
}
}
I''m still not understanding the if-then statements.
Code:package alphabetical; import java.util.*; public class Alphabetical { static boolean precedes (String s1, s2, s3) { if (s1.precedes(s2)) return true else return false if (s1.precedes(s3)) return true else return false if (s2.precedes(s3)) return true else return false } public static void main (Scanner scan) { Scanner sc = new Scanner (System.in); int firstAlphabetical = 0; //user input System.out.print("Enter name 1:"); String s1 = sc.nextLine(); System.out.print("enter name 2: "); String s2= sc.nextLine(); System.out.print("enter name 3: "); String s3= sc.nextLine(); //answer System.out.println("First alphabetically: " + firstAlphabetical); } }[/QUOTE] First, let's get the logic right. In main you will read 3 strings. You will keep track of the one that is the lowest, assuming it to be the first one. You will use precedes to compare the current lowest with the second. You will set the lowest to the second, if so. You will use precedes to compare the current lowest with the third. You will set the lowest to the third, if so. You will print the lowest. In precedes you will take EXACTLY TWO strings as arguments. We will call them stringA and stringB. You will set stringA to the uppercase version of stringA. You will set stringB to the uppercase version of stringB. You will get the length of stringA, and compare it to the length of stringB. You will check each character in A and B to the length of the shortest of the two, as found in the previous step. If you find that a character is less from one string than in the other, you can return true or false immediately. Return true if stringA was less, return false if stringB was less. If the strings are equal up to the length of the shorter of the two, the shorter is precedes the other, return true or false respectively. Right now you are calling precedes inside itself, this will not work. An if-then-else statement looks like: [CODE]if(true) { System.out.println("True is true"); } else { System.out.println("This is impossible. True is false."); }
-Lee