Results 1 to 4 of 4
- 11-04-2012, 07:50 PM #1
Member
- Join Date
- Jun 2011
- Posts
- 40
- Rep Power
- 0
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:
And Tester: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); } }
It gives my wrong answer:"HarryTomTom"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()); } }
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!
- 11-06-2012, 04:42 PM #2
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.
- 11-10-2012, 12:20 AM #3
Member
- Join Date
- Jun 2011
- Posts
- 40
- Rep Power
- 0
Re: printing in lexicographically sorted order
do you know where are those errors? and should I do?
- 11-12-2012, 04:02 PM #4
Re: printing in lexicographically sorted order
No, you said
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!I was going to use "else if" statement but Eclipse constantly keep saying that there is "syntax error" in it, have no clue...
Similar Threads
-
Ordering strings lexicographically from a file?
By Wumbo105 in forum New To JavaReplies: 6Last Post: 09-08-2012, 09:44 PM -
Implement Custom Printing Logic & Enhanced Word Documents Printing
By sherazam in forum Java SoftwareReplies: 0Last Post: 12-26-2011, 03:52 PM -
outputting results from bubble sorted array in correct numerical order
By leoshiner in forum New To JavaReplies: 1Last Post: 11-13-2011, 06:23 PM -
I used a TreeSet model to get a sorted JList but it isn't sorted.
By eLancaster in forum New To JavaReplies: 1Last Post: 08-11-2011, 07:56 PM -
Casting an int from a sorted set
By Bmack in forum New To JavaReplies: 2Last Post: 03-17-2010, 07:09 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks