• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

Java program with alphabetical order  RSS feed

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, I am trying to make a program where it takes 2 strings from the user and sorts them in alphabetical order but it isnt working. here's the code i have:




OUtput::

Enter the first string: apple
Enter the second string: Banana
Sorted strings are: Banana apple
Would you like to play again? [Y/N]: Y
Enter the first string:
 
Dee Vai
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have the Scanner input and variables and public static void main and all that as well but did not include it in here
 
Sheriff
Posts: 23876
50
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your code is working. Since the letter 'B' comes before the letter 'a' in Unicode, it follows that "Banana" comes before "apple". Have a look at this overly complicated article: Latin script in Unicode and you'll see that 'B' is represented by 66 and 'a' is represented by 97.

But you don't care whether it's "Banana" or "banana", right? You don't care about upper and lower case. So I'd suggest using the toLowerCase() method of String and compare the lower-case versions of the two strings.
 
Bartender
Posts: 1983
57
Eclipse IDE Google Web Toolkit Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suggest compareToIgnoreCase() method instead of converting the strings to lower case.
 
Sheriff
Posts: 12748
210
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check lines 4 and 7.  Are you sure those statements are actually changing any values that the rest of your program is using? Remember, strings are immutable. That is, you can't change a String; you can only get new Strings.

 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!