Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Sorting user given arraylist of Strings  RSS feed

 
Colby Toner
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hey I'm currently having an issue with taking user input of strings and then using Collections.sort(); to then organize them from A-Z. With me putting the strings in without asking for user input it works perfectly. What is the difference from me the programmer putting in strings to the user putting them in.
 
salvin francis
Bartender
Posts: 1604
35
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How does the user enter 5 words , space separated or comma separated ?

You didnt write code to break down the user's input into 5 words
 
Liutauras Vilda
Marshal
Posts: 4641
318
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

Well, you have more issues then the ones you have listed.

1. Imagine in one of your hands you're holding basket, and with the other hand you need to put 5 apples in it. What would you do? Please tell us exactly your steps you'd do.
2. The problem you're trying to solve is too big to accomplish in one go. You need to decompose it into small tasks:
  • Fill in the list with 5 elements
  • Print list elements
  • Sort elements
  • Print list elements
  • What does that mean? That means you'll need at least 4 methods to accomplish that task. The problem in your code is, that you don't know what works and what's not. In different words - do one task at a time only. Once you know you got first task done and simple testing gave you an expected results - move onto second task.

    So, please answer the question from the 1st point. It is very related with the first task you need to accomplish.
     
    Colby Toner
    Greenhorn
    Posts: 11
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I see what you mean, I need to create 5 separate string vars instead of just the 1. Due to right now the size of the arraylist is only 1 string.
     
    Liutauras Vilda
    Marshal
    Posts: 4641
    318
    BSD
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Colby Toner wrote:I see what you mean, I need to create 5 separate string vars instead of just the 1. Due to right now the size of the arraylist is only 1 string.
    Yes. So, what is needed there, maybe loop? Don't forget to introduce separate method for this particular task with a name, wich would reveal its full intention.
    Spend some time to choose accurate method name, it is important.
     
    Zachary Griggs
    Ranch Hand
    Posts: 83
    10
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Instead of using 5 different String variables, you could use a single array - it's the perfect structure for this kind of problem. A possible way to do this would be to ask for 5 words, separated by spaces, and then use the String's Split method by space (or some other delimiter).
     
    Campbell Ritchie
    Marshal
    Posts: 55717
    163
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    What is more, there is a method in the Arrays class which allows you to turn your array into a List. You will have to declare the variable as List<String> not ArrayList<String>, however.
     
    Colby Toner
    Greenhorn
    Posts: 11
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    How would I use a list due to never using one aside from arraylists
     
    Campbell Ritchie
    Marshal
    Posts: 55717
    163
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    An ArrayList is a List. You simply declare it as List<String> and when you have the appropriate imports in place it will work.
     
    Winston Gutkowski
    Bartender
    Posts: 10573
    65
    Eclipse IDE Hibernate Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Another little point to save you from CTS:
      System.out.println(listofcountries);
    will print out your entire list in one go.

    Winston
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!