new programmer: alphabetical order?

Discussion in 'Mac Programming' started by soccersquirt82, Aug 23, 2008.

  1. soccersquirt82 macrumors 6502

    Joined:
    Mar 11, 2008
    #1
    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?
     
  2. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #2
    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.
     
  3. soccersquirt82 thread starter macrumors 6502

    Joined:
    Mar 11, 2008
    #3
    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!
     
  4. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #4
    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
     
  5. Flynnstone macrumors 65816

    Flynnstone

    Joined:
    Feb 25, 2003
    Location:
    Cold beer land
    #5
    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.
     
  6. soccersquirt82 thread starter macrumors 6502

    Joined:
    Mar 11, 2008
    #6
    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.
     
  7. soccersquirt82 thread starter macrumors 6502

    Joined:
    Mar 11, 2008
    #7
    I have no idea where to even start. I read those documents, but I feel like they're talking in a second language. Help!!
     
  8. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #8
    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
     
  9. soccersquirt82 thread starter macrumors 6502

    Joined:
    Mar 11, 2008
    #9
    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))
    
     
  10. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #10
    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
     
  11. soccersquirt82 thread starter macrumors 6502

    Joined:
    Mar 11, 2008
    #11
    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(); 
    }
    
     
  12. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #12
     
  13. soccersquirt82 thread starter macrumors 6502

    Joined:
    Mar 11, 2008
    #13
    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(); 
    }
    
     
  14. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #14
     
  15. soccersquirt82 thread starter macrumors 6502

    Joined:
    Mar 11, 2008
    #15
    I give up on this one. Thanks for trying, though.
     
  16. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #16
    Quite a waste of a day then, eh?

    If you have a chance, go back and answer the questions I posed. If you still don't know what to do with them, post the answers and we'll see where it leads.

    -Lee
     
  17. soccersquirt82 thread starter macrumors 6502

    Joined:
    Mar 11, 2008
    #17
    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.
     
  18. soccersquirt82 thread starter macrumors 6502

    Joined:
    Mar 11, 2008
  19. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #19
    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
     
  20. soccersquirt82 thread starter macrumors 6502

    Joined:
    Mar 11, 2008
    #20
    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);
    	}
    }
    
     
  21. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #21
     
  22. soccersquirt82 thread starter macrumors 6502

    Joined:
    Mar 11, 2008
    #22
    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);
    	}
    }
    
     
  23. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #23
     
  24. soccersquirt82 thread starter macrumors 6502

    Joined:
    Mar 11, 2008
    #24
    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);
    	}
    }
    
     
  25. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #25
     

Share This Page