Question

Trying to of-else statement to sort 3 strings . import java.util.*; public class test {   ...

Trying to of-else statement to sort 3 strings .

import java.util.*;

public class test {

   public static void main(String[] args) {
       Scanner scan = new Scanner(System.in);
       String w1 = "", w2 ="", w3 = "";
       String top = null, mid = null, bot = null;
       System.out.println("Please enter the first word");
       w1 = scan.next();
       System.out.println("Please enter the Second word");
       w2 = scan.next();
       System.out.println("Please enter the third word");
       w3 = scan.next();
      
       if(w1.compareTo(w2) > 0 && (w1.compareTo(w3)) > 0) {
           top = w1;
       }else if(w1.compareTo(w2) < 0 && (w1.compareTo(w3) > 0)) {
           mid = w1;
       }
       else {
           bot = w1;
       }
      
       if(w2.compareTo(w1) > 0 && (w2.compareTo(w3)) > 0) {
           top = w2;
       }else if(w2.compareTo(w1) < 0 && (w2.compareTo(w3) > 0)) {
           mid = w2;
       }
       else {
           bot = w2;
       }
       if(w3.compareTo(w1) > 0 && (w3.compareTo(w2)) > 0) {
           top = w3;
       }else if(w3.compareTo(w2) < 0 && (w3.compareTo(w1) > 0)) {
           mid = w3;
       }
       else {
           bot = w3;
       }
      
       System.out.println(top);
       System.out.println(mid);
       System.out.println(bot);
   }
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1
import java.util.*;

public class test {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        String w1 = "", w2 = "", w3 = "";
        String top = null, mid = null, bot = null;
        System.out.println("Please enter the first word");
        w1 = scan.next();
        System.out.println("Please enter the Second word");
        w2 = scan.next();
        System.out.println("Please enter the third word");
        w3 = scan.next();

        if (w1.compareTo(w2) <= 0 && w2.compareTo(w3) <= 0) {
            top = w1;
            mid = w2;
            bot = w3;
        } else if (w1.compareTo(w3) <= 0 && w3.compareTo(w2) <= 0) {
            top = w1;
            mid = w3;
            bot = w2;
        } else if (w2.compareTo(w1) <= 0 && w1.compareTo(w3) <= 0) {
            top = w2;
            mid = w1;
            bot = w3;
        } else if (w2.compareTo(w3) <= 0 && w3.compareTo(w1) <= 0) {
            top = w2;
            mid = w3;
            bot = w1;
        } else if (w3.compareTo(w1) <= 0 && w1.compareTo(w2) <= 0) {
            top = w3;
            mid = w1;
            bot = w2;
        } else {
            top = w3;
            mid = w2;
            bot = w1;
        }

        System.out.println(top);
        System.out.println(mid);
        System.out.println(bot);
    }
}

Add a comment
Know the answer?
Add Answer to:
Trying to of-else statement to sort 3 strings . import java.util.*; public class test {   ...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Consider the following sample program: import java.util.Scanner; public class Palindrome { public static void main(String[] args){...

    Consider the following sample program: import java.util.Scanner; public class Palindrome { public static void main(String[] args){ Scanner kb = new Scanner(System.in); System.out.println("Enter a word:"); String word = kb.next();    String reverse = ""; for (int i=word.length()-1; i>=0; i--) reverse += word.charAt(i); boolean result = reverse.equalsIgnoreCase(word);    if (result) System.out.println("The word " +word+ " is a Palindrome."); else System.out.println("The word " +word+ " is not a Palindrome."); } } Rewrite the program so that the main method is: public static void...

  • Fix the following null pointer error. import java.util.*; import java.io.*; public class PhoneBook { public static...

    Fix the following null pointer error. import java.util.*; import java.io.*; public class PhoneBook { public static void main(String[]args)throws IOException { PhoneBook obj = new PhoneBook(); PhoneContact[]phBook = new PhoneContact[20]; Scanner in = new Scanner(System.in); obj.acceptPhoneContact(phBook,in); PrintWriter pw = new PrintWriter("out.txt"); obj.displayPhoneContacts(phBook,pw); pw.close(); } public void acceptPhoneContact(PhoneContact[]phBook, Scanner k) { //void function that takes in the parameters //phBook array and the scanner so the user can input the information //declaring these variables String fname = ""; String lname = ""; String...

  • Provide comments for this code explaining what each line of code does import java.util.*; public class...

    Provide comments for this code explaining what each line of code does import java.util.*; public class Chpt8_Project {    public static void main(String[] args)       {        Scanner sc=new Scanner(System.in);        int [][]courses=new int [10][2];        for(int i=0;i<10;i++)        {            while(true)            {                System.out.print("Enter classes and graduation year for student "+(i+1)+":");                courses[i][0]=sc.nextInt();                courses[i][1]=sc.nextInt();                if(courses[i][0]>=1...

  • Why does my program generate [] when viewing all guests names? ----------------------------------------------- import java.util.*; public class...

    Why does my program generate [] when viewing all guests names? ----------------------------------------------- import java.util.*; public class Delete extends Info { String name; int Id; int del; public Delete() { } public void display() { Scanner key = new Scanner(System.in); System.out.println("Input Customer Name: "); name = key.nextLine(); System.out.println("Enter ID Number: "); Id = key.nextInt(); System.out.println("Would you like to Delete? Enter 1 for yes or 2 for no. "); del = key.nextInt(); if (del == 1) { int flag = 0; //learned...

  • How can I make this program sort strings that are in a text file and not...

    How can I make this program sort strings that are in a text file and not have the user type them in? I need this done by 5:00 AM EST tommorow please. import java.util.*; public class MergeDemo { public static void main(String args[]) { Scanner input=new Scanner(System.in); System.out.print("How many lines to be sorted:"); int size=input.nextInt(); String[] lines=new String[size]; lines[0]=input.nextLine(); System.out.println("please enter lines..."); for(int i=0;i { lines[i]=input.nextLine(); } System.out.println(); System.out.println("Lines Before Sorting:"); System.out.println(Arrays.toString(lines)); mergeSort(lines); System.out.println(); System.out.println("Lines after Sorting:"); System.out.println(Arrays.toString(lines)); } public...

  • I need help running this code. import java.util.*; import java.io.*; public class wordcount2 {     public...

    I need help running this code. import java.util.*; import java.io.*; public class wordcount2 {     public static void main(String[] args) {         if (args.length !=1) {             System.out.println(                                "Usage: java wordcount2 fullfilename");             System.exit(1);         }                 String filename = args[0];               // Create a tree map to hold words as key and count as value         Map<String, Integer> treeMap = new TreeMap<String, Integer>();                 try {             @SuppressWarnings("resource")             Scanner input = new Scanner(new File(filename));...

  • import java.util.Scanner; public class SCAN { public static void main(String[ ] args) { int x, y,...

    import java.util.Scanner; public class SCAN { public static void main(String[ ] args) { int x, y, z; double average; Scanner scan = new Scanner(System.in); System.out.println("Enter an integer value"); x = scan.nextInt( ); System.out.println("Enter another integer value"); y = scan.nextInt( ); System.out.println("Enter a third integer value"); z = scan.nextInt( ); average = (x + y + z) / 3; System.out.println("The result of my calculation is " + average); } } What is output if x = 0, y = 1 and...

  • write a program which include a class containing an array of words (strings).The program will search...

    write a program which include a class containing an array of words (strings).The program will search the array for a specific word. if it finds the word:it will return a true value.if the array does not contain the word. it will return a false value. enhancements: make the array off words dynamic, so that the use is prompter to enter the list of words. make the word searcher for dynamic, so that a different word can be searched for each...

  • I need a java flowchart for the following code: import java.util.*; public class Main {   ...

    I need a java flowchart for the following code: import java.util.*; public class Main {    public static void main(String[] args) {    Scanner sc=new Scanner(System.in);           System.out.print("Enter the input size: ");        int n=sc.nextInt();        int arr[]=new int[n];        System.out.print("Enter the sequence: ");        for(int i=0;i<n;i++)        arr[i]=sc.nextInt();        if(isConsecutiveFour(arr))        {        System.out.print("yes the array contain consecutive number:");        for(int i=0;i<n;i++)        System.out.print(arr[i]+" ");       ...

  • Please Refactor this Assignment by using Arraylist instead of Array. import java.util.Scanner; public class DaysOfWeeks {...

    Please Refactor this Assignment by using Arraylist instead of Array. import java.util.Scanner; public class DaysOfWeeks { public static void main(String[] args) { String DAY_OF_WEEKS[] = {"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"}; char ch; int n; Scanner scanner = new Scanner(System.in); do { System.out.print("Enter the day of the Week: "); n = scanner.nextInt() - 1; if (n >= 0 && n <= 6) System.out.println("The day of the week is " + DAY_OF_WEEKS[n] + "."); else System.out.println("Invalid Entry"); System.out.print("Try again (Y/N): "); ch = scanner.next().charAt(0); }while(ch=='Y'); }...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT