Win a copy of Kotlin for Android App Development this week in the Kotlin forum!
  • 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
  • Liutauras Vilda
  • Devaka Cooray
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Java program with alphabetical order  RSS feed

 
Greenhorn
Posts: 29
  • 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: 29
  • 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: 24089
54
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.
 
Saloon Keeper
Posts: 2146
80
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: 12994
217
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.

 
I can't beleive you just said that. Now I need to calm down with this tiny ad:
Programmatically Create PDF Using Free Spire.PDF with Java
https://coderanch.com/wiki/703735/Programmatically-Create-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!