Question
I am getting an error in my code and was wondering where I went wrong. It is saying this, Main.java:4: error: class WordSorter is public,
I am getting an error in my code and was wondering where I went wrong. It is saying this, "Main.java:4: error: class WordSorter is public, should be declared in a file named WordSorter.java."
// Import the Scanner class
import java.util.Scanner;
public class WordSorter {
public static void main(String[] args) {
// 1. construct an object of the Scanner class that reads keyboard input.
Scanner keyboard=new Scanner(System.in);
// 2. Declare three String variables that will hold three different words the user enters.
String str1,str2,str3;
// 3.Print three requests to the user asking for the first, second, and third word.
System.out.print("Please enter the first word: ");
str1=keyboard.next();
System.out.print("Please enter the second word: ");
str2=keyboard.next();
System.out.print("Please enter the third word: ");
str3=keyboard.next();
// 4. compare the words and sort them
if(str1.compareToIgnoreCase(str2) < 0 && str1.compareToIgnoreCase(str3) < 0)
{
System.out.println(str1);
if(str2.compareToIgnoreCase(str3) < 0) {
System.out.println(str2);
System.out.println(str3);
}
else{
System.out.println(str3);
System.out.println(str2);
}
}
// check if str2 is small
else if(str2.compareToIgnoreCase(str1) < 0 && str2.compareToIgnoreCase(str3) < 0)
{
System.out.println(str2);
if(str1.compareToIgnoreCase(str3) < 0) {
System.out.println(str1);
System.out.println(str3);
}
else{
System.out.println(str3);
System.out.println(str1);
}
}
// check if str3 is small
else
{
System.out.println(str3);
if(str1.compareToIgnoreCase(str2) < 0) {
System.out.println(str1);
System.out.println(str2);
}
else{
System.out.println(str2);
System.out.println(str1);
}
}
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started