Results 1 to 8 of 8
- 10-14-2009, 05:25 AM #1
Member
- Join Date
- Oct 2009
- Posts
- 5
- Rep Power
- 0
String comparision method problem
Hi guys i am given following problem and have to write the program using string comparison methods.....i am also adding my code but i cant get the out put....please help
Problem:
Write a program that asks user to enter three names and then display the name in ascending order. For example , if user entered "Charlie" , "Leslie" and "Andy" then the program would display:
Andy
Charlie
Leslie
Heres what i tried ...i search the whole book but couldn't find how to arrange names in ascending order...
I not getting any error when i complie but when i run and put three names it just displays second and thirdJava Code:import java.util.Scanner; public class SortedNames { public static void main(String[] args) { String name1,name2,name3; Scanner keyboard = new Scanner(System.in); System.out.print("Enter first name "); name1 = keyboard.nextLine(); System.out.print("Enter second name "); name2 = keyboard.nextLine(); System.out.print("Enter third name "); name3 = keyboard.nextLine(); if (name1.compareTo(name2) < 0 ) { System.out.println(name1); } else if (name1.compareTo(name3) < 0) { System.out.println(name1); } if (name2.compareTo(name1) < 0 ) { System.out.println(name2); } else if (name2.compareTo(name3) < 0) { System.out.println(name2); } if (name3.compareTo(name1) < 0 ) { System.out.println(name3); } else if (name3.compareTo(name1) < 0) { System.out.println(name3); } } }
So please hepl me i want them to come in ascending order. Thanks
- 10-14-2009, 06:03 AM #2
Have you checked out / are you allowed to use the built-in sorting functionality?
Option 1: Store you values in an array and use the static method, Arrays.sort(Object[]).
Option 2: Store your values in a List and use the static method, Collections.sort(List).
Regardless which option you choose, if you are allowed to use sorting methods available in Java, it will make your job much easier.
Just beware that sorting is by ASCII value - so "Leslie" comes before "charlie". If you need a case-insensitive sort, you can either convert all names to lower case (or upper case), or write your own comparator.Last edited by CodesAway; 10-14-2009 at 06:06 AM.
CodesAway - codesaway.info
writing tools that make writing code a little easier
- 10-14-2009, 02:04 PM #3
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Yep, in most of the cases converting the String into one format is much easier to handle, either lower case or upper case. Then process accordingly.
- 10-14-2009, 03:06 PM #4
One more suggestion.Try out using equalsIgnoreCase()..always better
Ramya:cool:
-
- 10-15-2009, 02:02 AM #6
Member
- Join Date
- Oct 2009
- Posts
- 5
- Rep Power
- 0
"Yep, in most of the cases converting the String into one format is much easier to handle, either lower case or upper case. Then process accordingly.
"
We cannot use the sorting method just the string comparision .....me so confused
@ guys i been told to use any case i.e lower or upper
But using that will get the output...theres nothing to do for the names to come in ascending order????
Sorry if my questions are dumb but me learningLast edited by rons_sacramental; 10-15-2009 at 04:41 AM.
- 10-15-2009, 02:03 AM #7
Member
- Join Date
- Oct 2009
- Posts
- 5
- Rep Power
- 0
So can actually anyone tell me the code (not the whole but where i need corrections ) please
- 10-15-2009, 05:15 AM #8
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
You have to recode the if-else logic parts actually. Before compare them each other, add all the strings in an array and try to sort them as display it to check the result. Then think about the case conflict in sorting.
Similar Threads
-
Invoking method from String
By Supamagier in forum Advanced JavaReplies: 12Last Post: 05-29-2009, 09:21 AM -
Calling a method in a different class from within a method problem
By CirKuT in forum New To JavaReplies: 29Last Post: 09-25-2008, 07:55 PM -
comparision between two lists
By suprabha in forum Advanced JavaReplies: 14Last Post: 08-01-2008, 02:49 PM -
Method for String to int conversion
By Java Tip in forum Java TipReplies: 0Last Post: 01-02-2008, 06:35 PM -
String replace method
By venkata.tarigopula in forum Advanced JavaReplies: 1Last Post: 07-10-2007, 08:14 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks