THIS QUESTION HAS TWO PARTS:
A) Write an application that accepts three Strings from the user and displays one of two messages depending on whether the user entered the Strings in alphabetical order without regard to case.
Save the file as Alphabetize.java.
B) Write an application that accepts three Strings from the user and displays them in alphabetical order without regard to case.
Save the file as Alphabetize2.java
Alphabetize.java
import java.util.*;
public class Alphabetize{
public static void main(String []args){
Scanner sc=new Scanner(System.in);
System.out.println("Enter 1st string");
String s1=sc.next();
System.out.println("Enter 2nd string");
String s2=sc.next();
System.out.println("Enter 3rd string");
String s3=sc.next();
if(s2.compareTo(s1)>0 && s3.compareTo(s2)>0)
System.out.println(" strings are in alphabetical order");
else
System.out.println(" strings are NOT in alphabetical order");
}
}
output:
Alphabetize2.java
import java.util.*;
public class Alphabetize2{
public static void main(String []args){
String temp;
Scanner sc=new Scanner(System.in);
System.out.println("Enter 1st string");
String s1=sc.next();
System.out.println("Enter 2nd string");
String s2=sc.next();
System.out.println("Enter 3rd string");
String s3=sc.next();
if (s1.compareTo(s2)>0)
{
temp =s1;
s1 = s2;
s2 = temp;
}
if (s2.compareTo(s3)>0)
{
temp =s2;
s2 = s3;
s3 = temp;
}
if (s1.compareTo(s3)>0)
{
temp =s1;
s1 = s3;
s3 = temp;
}
System.out.print("strings in Sorted Order:");
System.out.println(s1);
System.out.println(s2);
System.out.println(s3);
}
}
:
Get Answers For Free
Most questions answered within 1 hours.