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
This seems right to me, but I have six errors. Two say to delete the "else" and the other two say to add ";" in the if-then statement.

Code:
package alphabetical;
import java.util.*;
public class Alphabetical 
{
	static boolean precedes (String s1, s2, s3)
	{		
		lowest = 0;
		
		if (s1.precedes(s2)){
		lowest = s1}
		return true;
		else {
		lowest = s2;}
		return false;
		
		if (lowest.precedes(s3)){
		lowest = lowest}
		return true;
		else {
		lowest = s3;}
		return false;
		
	}
	
	public static void main (Scanner scan) 
	{
		Scanner sc = new Scanner (System.in);
					
		//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();
		
		//answers
		System.out.println("The string that comes first alphabetically is: " + precedes(lowest));
	}
}
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
This seems right to me, but I have six errors. Two say to delete the "else" and the other two say to add ";" in the if-then statement.

Code:
package alphabetical;
import java.util.*;
public class Alphabetical 
{
	static boolean precedes (String s1, s2, s3)
	{		
		lowest = 0;
		
		if (s1.precedes(s2)){
		lowest = s1}
		return true;
		else {
		lowest = s2;}
		return false;
		
		if (lowest.precedes(s3)){
		lowest = lowest}
		return true;
		else {
		lowest = s3;}
		return false;
		
	}
	
	public static void main (Scanner scan) 
	{
		Scanner sc = new Scanner (System.in);
					
		//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();
		
		//answers
		System.out.println("The string that comes first alphabetically is: " + precedes(lowest));
	}
}[/QUOTE]

Precedes should take EXACTLY TWO string arguments as I stated before. You should call precedes with s1 and s2 first, then whichever of those is lower and s3 next. Precedes needs to compare the TWO strings that are passed to it. It needs to do it with charAt if you are not supposed to use String's compareTo. You will use the result of the two calls to precedes in the conditional of an if.

Look at my last post and change precedes to compare TWO strings, that are passed in. String does not have a precedes method, you have to decide this yourself in your precedes method. You most perform a for loop from 0 to one less than the length of the shorter string.

-Lee

Edit: Also, you must state the type of EACH of the arguments of precedes. Call them something other than s1 and s2, since that's what you have things called in main. so, say:
[CODE]
boolean precedes(String stringA, String stringB) {
...
}
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
I AM supposed to use the compareTo method. . .

Sweet, that saves you time and effort in your precedes method.

Instead of having to loop, do charAt, etc. you can just do:
StringA.toUppercase().compareTo(StringB.toUppercase())
or
StringA.compareToIgnoreCase(StringB)

Assign the result of that thing to an int, and compare it to 0 based on the returns detailed here:
http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html#compareTo(java.lang.String)

Based on the result you'll return true or false.

-Lee
 

soccersquirt82

macrumors 6502
Original poster
Mar 11, 2008
296
0
I tried switching everything from String to int, and I tried using the compareTo method.

Code:
package alphabetical;
import java.util.*;
public class Alphabetical 
{
	static boolean precedes (int sA, int sB, int sC)
	{		
		int lowest;
		
		if (sA.compareToIgnoreCase(sB){
		lowest = sA}
		return true;
		else {
		lowest = sB;}
		return false;
		
		if (lowest.compareToIgnorecase(sC)){
		lowest = lowest}
		return true;
		else {
		lowest = sC;}
		return false;
		
	}
	
	public static void main (Scanner scan) 
	{
		Scanner sc = new Scanner (System.in);
		int lowest = 0;
							
		//user input
		System.out.print("Enter name 1:");
		int s1 = sc.nextInt();
		System.out.print("enter name 2: ");
		int s2= sc.nextInt();		
		System.out.print("enter name 3: ");
		int s3= sc.nextInt();
		
		//answers
		System.out.println("The string that comes first alphabetically is: " + precedes(lowest));
	}
}
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
I tried switching everything from String to int, and I tried using the compareTo method.

Code:
package alphabetical;
import java.util.*;
public class Alphabetical 
{
	static boolean precedes (int sA, int sB, int sC)
	{		
		int lowest;
		
		if (sA.compareToIgnoreCase(sB){
		lowest = sA}
		return true;
		else {
		lowest = sB;}
		return false;
		
		if (lowest.compareToIgnorecase(sC)){
		lowest = lowest}
		return true;
		else {
		lowest = sC;}
		return false;
		
	}
	
	public static void main (Scanner scan) 
	{
		Scanner sc = new Scanner (System.in);
		int lowest = 0;
							
		//user input
		System.out.print("Enter name 1:");
		int s1 = sc.nextInt();
		System.out.print("enter name 2: ");
		int s2= sc.nextInt();		
		System.out.print("enter name 3: ");
		int s3= sc.nextInt();
		
		//answers
		System.out.println("The string that comes first alphabetically is: " + precedes(lowest));
	}
}[/QUOTE]

That doesn't make sense. Leave them as strings. Your input in main is fine. Declare lowest as a String, not an int. Assign s1 to lowest. Call precedes after your input in main as:
[CODE]if(precedes(s2,lowest)) {
//If s2 precedes lowest, do what?
}

After that, you will repeat with s3 and lowest.

Change the definition of precedes to what I suggested. It must take exactly TWO strings, not 3 strings, not 3 ints, TWO strings. In precedes, compare the two strings passed in (TWO STRINGS!) using compareToIgnoreCase. You must compare the result of compareToIgnoreCase to 0 to see which is lower.

-Lee
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
I'm not getting it. This will have to wait for tomorrow. . .

I wrote this all out with ints instead of strings to demonstrate, but will only post the parts that demonstrate the sticky things so it doesn't give the whole thing away:
Code:
    int lowest = intA;
    if(precedes(intB,lowest)) lowest=intB;
    if(precedes(intC,lowest)) lowest=intC;

Code:
  static boolean precedes(int intOne, int intTwo) {
    if(...) { //You'd fill in something that compares intOne and intTwo
      return true;
    } else {
      return false;
    }
  }
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.