Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Prompt user for inputs and then sort correctly

  1. #1
    Junior Member
    Join Date
    Jan 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Prompt user for inputs and then sort correctly

    I'm currently in my first semester. I have a project requiring me to build a program having a user input 3 words, sort them alphabetically and output the middle word. I have done some searching and seem to only come back with results for sorting 2 words. I so far have code to get the user input but I am completely lost as to how to sort them alphabetically. Please be patient with me as I am very new to programming. If anyone can provide me with any advice or the best or easiest way to go about sorting these I would greatly appreciate it

     import java.util.Scanner; //The Scanner is in the java.util package.
     
     public class MiddleString {
         public static void main(String [] args){
             Scanner input = new Scanner(System.in); //Create a Scanner object.
     
                String str1, str2, str3; 
     
             System.out.println("Please enter three word words : "); //Prompt user to enter the three words
             str1=input.next(); //Sets "str1" = to first word.
             str2=input.next(); //Sets "str2" = to second word.
             str3=input.next(); //Sets "str3" = to third word.
     
     
     
             System.out.println("The middle word is " ); // Outputs the middle word in alphabetical order.
     
         }
     }

    we havnt done arrays yet and I THINK i have to do compareTo.....if so can someone please show me example on how to use it?


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Prompt user for inputs and then sort correctly

    Have you read the String API page and the explanation of String.compareTo()? That's where you should start. Then you should play with it to see if your understanding and the documentation agree, and then play with it some more to answer any questions you may have. If questions remain that you cannot answer, come back and ask them.

    Sorting 3 inputs is something to think about. Imagine someone hands you a nickel, dime, and then a quarter in random order. You don't know which coins they're going to pass you or in which order. How do you put them in order? Can you program that?

Similar Threads

  1. Printing arrays from user inputs (2 dimensional)
    By kevinsauerzxc in forum What's Wrong With My Code?
    Replies: 5
    Last Post: March 13th, 2013, 07:28 AM
  2. [SOLVED] How can I get my dice roller to completely loop and sort results correctly?
    By Skynet928 in forum Loops & Control Statements
    Replies: 1
    Last Post: January 31st, 2013, 02:58 AM
  3. Classpath override not working correctly in command prompt?
    By MeteoricDragon in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 20th, 2011, 12:05 PM
  4. Prompt user to repeat
    By Callcollect in forum Loops & Control Statements
    Replies: 18
    Last Post: December 4th, 2011, 07:15 PM
  5. how do I keep a persistent prompt in JTextArea and allow user input
    By cazmo in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 23rd, 2010, 01:14 PM