Results 1 to 4 of 4
  1. #1
    FOX427 is offline Member
    Join Date
    Jun 2011
    Posts
    52
    Rep Power
    0

    Post printing in lexicographically sorted order

    Hi All! having issue with this program, it need to read three strings and print them in lexicographically order....Here is my code so far:
    Java Code:
    /**
     * This program reads three strings and prints them in lexicographically order.
     * @author FOX
     *
     */
    public class StringSorter {
    	private String string1, string2, string3;
    	
    	
    	public StringSorter(String name1, String name2, String name3) 
    	{
    		string1=name1;
    		string2=name2;
    		string3=name3;
    	}
    	
    	public String getComparison()
    	{
    		String firstName, secondName, thirdName;
    		
    		
    		if ((string1.compareTo(string2)>0)&&(string1.compareTo(string3)>0));
    		{
    			firstName=string1;
    			if (string2.compareTo(string3)>0)
    			{
    				secondName=string2;
    				thirdName=string3;
    			}
    			else
    				secondName=string3;
    				thirdName=string2;
    		}
    		
    		if((string2.compareTo(string1)>0)&&(string2.compareTo(string3)>0));
    			{
    				firstName=string2;
    				if (string1.compareTo(string3)>0)
    				{
    					secondName=string1;
    					thirdName=string3;
    				}
    				else
    					secondName=string3;
    					thirdName=string1;
    			}
    		if ((string3.compareTo(string1)>0)&&(string3.compareTo(string2)>0));
    		{
    			firstName=string3;
    			if (string1.compareTo(string2)>0)
    			{
    				secondName=string1;
    				thirdName=string2;
    			}
    			else
    				secondName=string2;
    				thirdName=string1;
    		}
    		
    		return (firstName+secondName+thirdName);
    	}
    	
    
    }
    And Tester:
    Java Code:
    /**
     * This program prints three strings in lexicographically order.
     * @author FOX
     *
     */
    
    import java.util.Scanner;
    
    public class StringSorterTester {
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		Scanner in = new Scanner(System.in);
    		System.out.println("Please enter first string");
    		String name1=in.next();
    		System.out.println("Please enter second string");
    		String name2=in.next();
    		System.out.println("Please enter third string");
    		String name3=in.next();
    		
    		StringSorter order= new StringSorter(name1, name2, name3);
    		System.out.println(order.getComparison());
    	}
    
    }
    It gives my wrong answer:"HarryTomTom"
    I was going to use "else if" statement but Eclipse constantly keep saying that there is "syntax error" in it, have no clue...
    And don't know where I got wrong and I would be very thankful for any suggestions, thanks!

  2. #2
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    6

    Default Re: printing in lexicographically sorted order

    Lets see the errors and code you have using the if else. You have logic errors in this code.

  3. #3
    FOX427 is offline Member
    Join Date
    Jun 2011
    Posts
    52
    Rep Power
    0

    Default Re: printing in lexicographically sorted order

    do you know where are those errors? and should I do?

  4. #4
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    6

    Default Re: printing in lexicographically sorted order

    No, you said
    I was going to use "else if" statement but Eclipse constantly keep saying that there is "syntax error" in it, have no clue...
    I asked to see those errors because you most likely have logic errors in your existing code, but I have not run it with a debugger to find out!

Similar Threads

  1. Ordering strings lexicographically from a file?
    By Wumbo105 in forum New To Java
    Replies: 6
    Last Post: 09-08-2012, 09:44 PM
  2. Replies: 0
    Last Post: 12-26-2011, 03:52 PM
  3. Replies: 1
    Last Post: 11-13-2011, 06:23 PM
  4. Replies: 1
    Last Post: 08-11-2011, 07:56 PM
  5. Casting an int from a sorted set
    By Bmack in forum New To Java
    Replies: 2
    Last Post: 03-17-2010, 07:09 PM

Tags for this Thread

Posting Permissions

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