• 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
  • Bear Bibeault
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Frits Walraven
Bartenders:
  • Carey Brown
  • salvin francis
  • Claude Moore

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: 24366
55
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: 2212
92
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.
 
Marshal
Posts: 13441
222
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.

 
Seriously? That's what you're going with? I prefer this tiny ad:
Create Edit Print & Convert PDF Using Free API with Java
https://coderanch.com/wiki/703735/Create-Convert-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!