Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

soccersquirt82

macrumors 6502
Original poster
Mar 11, 2008
296
0
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?
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
First off, you should always say when you are working on something for a class, so people don't write a lot of code for you, but point you towards documentation instead.

With that said:
http://java.sun.com/j2se/1.4.2/docs/api/java/util/Arrays.html

There are sort methods. See which ones will help you (I assume this is for Java, as that was what your previous questions were about).

-Lee

p.s. I re-read... to use any of that, you need to have the strings in an array first.
http://java.sun.com/docs/books/jls/third_edition/html/arrays.html
There's the chapter in the language specification about arrays. Once you know how to get an array of some type, you can use the sort methods found in the Arrays document linked above as long as the type implements Comparable.
http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Comparable.html

You only have to worry about compareTo when you define your own class that you want to implement Comparable, or extend a class that does not already implement Comparable and make it so. You can check the API docs to see if a particular class already implements Comparable. For example:
http://java.sun.com/j2se/1.3/docs/api/java/lang/String.html

Every class reference lists what class this class extends, and what interfaces it implements.
 

soccersquirt82

macrumors 6502
Original poster
Mar 11, 2008
296
0
I'm supposed to call a method by referring to an instance. Should I still use an array? I haven't learned how to yet in class, but I can (try to) learn online. Thanks!
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
If you're not using an array, then that would mean you'd be using N separate string instances. When N is very low, say 3, the orderings available is not too high, and it isn't too hard to just write some control logic to deal with this. However, as N grows the orderings grow combinatorially. This means a LOT of control structures, and you'll only have the ability to solve for a fixed number of strings.

If you did have three strings, the solution in pseudocode would be something like:
Code:
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


In this psuedocode i just used indentation level to indicate control structure nesting (like python) and pretended that < could be used to lexographically compare strings. In most languages you cannot do something like that, so you'd have to use some comparison function like compareTo.

I'm not sure if this is what you meant. Your instructor should clarify the project if you don't know where the strings you are comparing are coming from, if you can use arrays, if you're supposed to write your own sort (if so, what kind? Bubble? Merge?), etc. Otherwise it's just a crapshoot, and you could interpret the problem dozens of ways.

-Lee
 

Flynnstone

macrumors 65816
Feb 25, 2003
1,438
96
Cold beer land
What language?

I "C", you could have a array of pointers to strings.
Then sing something like a bubble sort (or other sort) to make the array ordered.
 

soccersquirt82

macrumors 6502
Original poster
Mar 11, 2008
296
0
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.
 

soccersquirt82

macrumors 6502
Original poster
Mar 11, 2008
296
0
I have no idea where to even start. I read those documents, but I feel like they're talking in a second language. Help!!
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
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.

Oh, that doesn't require sorting of a list at all, then. You just need to be able to compare two strings. I won't tell you what algorithm to use, but you don't even need to be able to store all 4. See if that isn't enough to figure it out. I would not do this project with arrays.

-Lee
 

soccersquirt82

macrumors 6502
Original poster
Mar 11, 2008
296
0
I used boolean. I then used a compareTo method. After I prompted for three strings, I think I should use an if-then statement. Is this right or am I totally off? And if this is right, I don't know what to do after this.

Code:
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))
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
Well, you've got the I/O going, it looks like.

Otherwise, what is compareTo going to be returning? Why call it compareTo since you're not implementing the Comparable interface (which requires a method called compareTo that returns an int, and accepts an Object as its only parameter)? You're not comparing the class Alphabetical to anything, you're comparing some strings.

Once you have all of your strings, if you really want to store all of them, you can put together a short set of ifs. Since you don't need to sort these, you don't need the convoluted, nested logic that I offered in pseudocode above. You only need to know one thing. You don't care what the second string in lexicographical order is, only the first. How would you find this?

-Lee
 

soccersquirt82

macrumors 6502
Original poster
Mar 11, 2008
296
0
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(); 
}
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
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
 

soccersquirt82

macrumors 6502
Original poster
Mar 11, 2008
296
0
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(); 
}
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
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
 

soccersquirt82

macrumors 6502
Original poster
Mar 11, 2008
296
0
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.
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
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.

This is exactly right.

So you can have a function firstAlphabetical that returns a String, and it takes 3 String arguments.

Then you just need to compare them as you described. You can keep a string called lowest, and assign whichever is lowest alphabetically between the first argument and the second argument. Then you can compare your string called lowest to your third argument. If the third argument is lower, that will get assigned to lower, otherwise you do nothing. you can then return lowest.

Then in main, you would read in your 3 strings, then pass them to your firstAlphabetical function. You could either assign the result to a string, or just print it's result.

-Lee
 

soccersquirt82

macrumors 6502
Original poster
Mar 11, 2008
296
0
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);
	}
}
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
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
 

soccersquirt82

macrumors 6502
Original poster
Mar 11, 2008
296
0
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);
	}
}
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
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
 

soccersquirt82

macrumors 6502
Original poster
Mar 11, 2008
296
0
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);
	}
}
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
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
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.