4 Replies - 1247 Views - Last Post: 11 February 2014 - 11:58 PM Rate Topic: -----

#1 chrismb   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 11-January 14

Prompt user for inputs and then sort correctly

Posted 08 February 2014 - 01:18 PM

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 and how to prompt the user to enter the three strings. 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 one word words : "); //Prompt user to enter one word
         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.
          
     }
 }


Please help!

Is This A Good Question/Topic? 0
  • +

Replies To: Prompt user for inputs and then sort correctly

#2 Limecore   User is offline

  • D.I.C Head

Reputation: 18
  • View blog
  • Posts: 92
  • Joined: 28-November 12

Re: Prompt user for inputs and then sort correctly

Posted 08 February 2014 - 02:06 PM

You could put them into an array and then use the Arrays.sort() method
Look here for an example
Was This Post Helpful? 0
  • +
  • -

#3 chrismb   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 11-January 14

Re: Prompt user for inputs and then sort correctly

Posted 08 February 2014 - 02:12 PM

View PostLimecore, on 08 February 2014 - 02:06 PM, said:

You could put them into an array and then use the Arrays.sort() method
Look here for an example


we havnt covered arrays yet, the furthest we've gotten to is compareTo
Was This Post Helpful? 0
  • +
  • -

#4 chrismb   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 11-January 14

Re: Prompt user for inputs and then sort correctly

Posted 08 February 2014 - 02:49 PM

alright i tried it with this code but does anyone know how to print the output result for the middle word?

 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 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.
          
       if((str1.compareTo(str3) < 0) && (str1.compareTo(str2) <0) && (str2.compareTo(str3) <0) )
       System.out.println(str2);
      
      else if (( str3.compareTo(str1) <0) && (str1.compareTo(str2) <0) && (str3.compareTo(str2) <0) )
       System.out.println(str1);
      
      else if ( (str1.compareTo(str2) <0) && (str3.compareTo(str2) <0) && (str1.compareTo(str3) <0) )
       System.out.println(str3);
         
         
         System.out.println("The middle word is " ); // Outputs the middle word in alphabetical order.
          
     }
 }


Was This Post Helpful? 0
  • +
  • -

#5 Limecore   User is offline

  • D.I.C Head

Reputation: 18
  • View blog
  • Posts: 92
  • Joined: 28-November 12

Re: Prompt user for inputs and then sort correctly

Posted 11 February 2014 - 11:58 PM

In your if statement, you are only checking half the outcomes. What happens if the order is 321?
I wont give you the answer, but as a hint, || is the or operator, and brackets can be used in boolean logic too

Another pointer: wouldn't it be simpler to replace the 3 if statements with just 2 comparing the suspected middle string to the 2 others
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1