Can compareTo Handle All String Comparisons in Java?

  • Java
  • Thread starter tbrown427
  • Start date
  • Tags
    Java
In summary, the "import java.util.Scanner" statement allows the use of the Scanner class in Java programs, which provides methods for reading input from the user. To use the Scanner class, you need to import it and create a new instance, remembering to close it when finished. Multiple classes can be imported in Java using multiple statements or the asterisk symbol. Once imported, the Scanner class does not need to be imported again, but other classes will need to be imported. Custom import statements cannot be created in Java, as they are used to access already existing classes and packages.
  • #1
tbrown427
1
0
I can't get this to test all the way through

Code:
import java.util.Scanner;

public class OrderStrings {
   public static void main (String [] args) {
      String firstString;
      String secondString;

      firstString  = "rabbits";
      secondString = "capes";

     if (secondString.compareTo(firstString) >0){
         System.out.println("capes rabbits");
     }
     else {
         System.out.println("capes rabbits");
     }
      return;
   }
}
 
Technology news on Phys.org
  • #2
This code compiles and executes, but the output is always
capes rabbits

Look at your if else statement and notice it makes no difference the result of compareTo; it always just prints the above.
 

FAQ: Can compareTo Handle All String Comparisons in Java?

What is the purpose of "import java.util.Scanner" in Java?

The "import java.util.Scanner" statement allows the use of the Scanner class in Java programs. This class provides methods for reading input from the user, making it easier to gather user input and use it in the program.

How do you use the Scanner class in Java?

To use the Scanner class, you first need to import it using the "import java.util.Scanner" statement. Then, you can create a new instance of the Scanner class and use its methods to read input from the user or from a file. Remember to close the Scanner object when you are done using it.

Can you import multiple classes in Java?

Yes, you can import multiple classes in Java by using multiple "import" statements. You can also use the asterisk symbol (*) to import all classes in a specific package.

Do you need to import the Scanner class every time you use it in a Java program?

No, once you have imported the Scanner class in your program, you do not need to import it again. However, if you are using multiple classes that require importing, you will need to include the "import" statement for each class at the top of your program.

Is it possible to create a custom import statement in Java?

No, you cannot create your own import statement in Java. Import statements are used to access classes and packages that have already been created and compiled. If you want to use a custom class, you will need to create it yourself or import it from a library or package that contains it.

Similar threads

Replies
12
Views
15K
Replies
2
Views
2K
Replies
2
Views
2K
Replies
2
Views
1K
Replies
3
Views
1K
Replies
3
Views
3K
Replies
2
Views
12K
Back
Top