Results 1 to 8 of 8

Thread: A program that asks for two strings and reports their lexicographical ordering

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2006
    Posts
    12

    A program that asks for two strings and reports their lexicographical ordering

    I need some help with this program. Can somebody tell what I need to did in order to get my program started. Thanks

    A program that asks for two strings and reports their lexicographical ordering, i.e. as they would appear in a lexicon that sorts them according to ASCII values. Do not use any predefined string comparison methods! The only predefined methods you may use are nextLine() of the Scanner class and length() and charAt(int i) of the String class.

    EXTRA CREDIT: allow the user to decide whether they want to ignore case in the comparison (again without using predefined methods). Make sure you get the program working first without this feature.

    Examples not ignoring case:
    "abc" == "abc", "ABC" < "abc", "fishery" > "fish", "run?" > "run!"...
    Examples ignoring case:
    "abc$" == "ABC$", "Hat" == "haT", "cat" < "Hat", "Hatter" > "hat", ...

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: A program that asks for two strings and reports their lexicographical ordering

    ASCII values? Java's got no stinkin' ASCII values. Only UTF-16 unit values, and on a higher level Unicode code points.

    But whatever.

    Which part do you have problems with?
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  3. #3

    Thread Starter
    New Member
    Join Date
    Sep 2006
    Posts
    12

    Re: A program that asks for two strings and reports their lexicographical ordering

    I don't how I should start this program. If you could help me get started maybe I can figure the rest out. Thanks

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: A program that asks for two strings and reports their lexicographical ordering

    You would start by implementing a lexicographical compare. If you don't know how that works, you look it up on the web.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  5. #5

    Thread Starter
    New Member
    Join Date
    Sep 2006
    Posts
    12

    Re: A program that asks for two strings and reports their lexicographical ordering

    I use compare to but my teacher only want us to use only predefined methods you may use are nextLine() of the Scanner class and length() and charAt(int i) of the String class. So can you tell my how I would set this up. Thanks


    public class StringCompare
    {
    public static void main(String[] args)
    {

    String s1 = "abc",
    s2 = "def";

    // Compare "abc" and "def"

    if (s1.compareTo(s2) < 0)
    {
    System.out.println(s1 + " comes before " + s2);
    }
    else if (s1.compareTo(s2) == 0)
    {
    System.out.println(s1 + " is equal to " + s2);
    }
    else if (s1.compareTo(s2) > 0)
    {
    System.out.println(s1 + " follows " + s2);
    }

  6. #6
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: A program that asks for two strings and reports their lexicographical ordering

    You need to re-implement compareTo on your own. As I said, look up lexicographical comparing on the web. Don't expect me to give you any code.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  7. #7

    Thread Starter
    New Member
    Join Date
    Sep 2006
    Posts
    12

    Re: A program that asks for two strings and reports their lexicographical ordering

    I change my code to the way my teacher wanted it, but still have a little trouble. I am having trouble with my program counting all the character in the string. Like if I typed (tanned) in string1 and (tanning) in string2 it would print tanned follows tanning when it suspose to be tanned comes before tanning. Also having trouble getting it to equal to each other (print tanning is equal to tanning or print the two strings are equal). Can somebody please help me finish this program. Thanks

    Here is my code below

    Code:
    import java.util.Scanner;
    
    public class Strings
    {
    	public static void main(String[] args)
    	{
    		String s1;
    		String s2;
    
    		
    		Scanner scan = new Scanner (System.in);
    		
    		System.out.println("Enter the first string:");
    		s1 = scan.nextLine();
    		
    		System.out.println("Enter the second string:");
    		s2 = scan.nextLine();
    			
    		//Sting length of the characters
    		System.out.println("Sting Length of s1: " + s1.length());
    		System.out.println("Sting Length of s2: " + s2.length());
    
    		//Compare the characters
    		char ch1 = s1.charAt(1);
    		char ch2 = s2.charAt(1);
    		
    		for ( int count = 0; count < 1; count++) {
    			
    			if(ch1 < ch2)
    				System.out.print(s1 + " comes before " + s2);
    			else
    				System.out.print(s1 + " follows " + s2);
    		}
    	}
    }

  8. #8
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Re: A program that asks for two strings and reports their lexicographical ordering

    You're not comparing each character in the strings. I would find the smaller string and use that as indexing. Then compare each character:


    Code:
       for (int index=0; index<smallerString.length(); ++index)
       {
           //-larger string comes first
           if (largetString.charAt(index) < smallerString.charAt(index))
           {
               //-print out
                //-break;
           }
          //-other comparisions

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width