5

Im a java noob. Basically I am trying to create a program that compares two command line arguments while ignoring case and prints out the lesser of the two strings. Here is what I have so far:

public class CompareStrings
{
   public static void main(String[] args) 
   {
      String s1 = new String(args[0]);
      String s2 = new String(args[1]);

      if ( s1.compareToIgnoreCase(s2) > 0 )
         System.out.println(s2);
      else if ( s1.compareToIgnoreCase(s2) < 0 )
         System.out.println(s1);
      else   
         System.out.println("Both strings are equal.");
   }
}

I keep getting the error

Error: Could not find or load main class CompareString

when I try to run it. What am I doing wrong?

No Idea For Name
  • 11,411
  • 10
  • 42
  • 70
user2681595
  • 97
  • 1
  • 4

5 Answers5

16

"Error: Could not find or load main class CompareString"

Your error message says you couldn't load class "CompareString", but your code says your class name is CompareStrings.

Jud
  • 1,324
  • 3
  • 24
  • 47
5

Your class name is wrong.

Your error says

Error: Could not find or load main class CompareString

but the name of class is CompareStrings not CompareString

launch using java CompareStrings

Read this good tutorial on compiling and launching java programs

Prasad Kharkar
  • 13,410
  • 5
  • 37
  • 56
2

First of all you are trying to use wrong class, should be CompareStrings and not CompareString.

Second, I would recommend using a nice utility lib for handling command line called Cliche from Google Code site

And third, it would be good to check if the given string is null before you call compareToIgnoreCase on it

Kris
  • 5,714
  • 2
  • 27
  • 47
1

as per your error i can gues that you are running CompareString but your class is CompareStrings So either run java CompareStrings or rename your class to CompareString

SpringLearner
  • 13,738
  • 20
  • 78
  • 116
1

If this is the only code you have, save that file as CompareStrings.java not CompareString, and in command prompt javac CompareStrings.java. (to do this you need to configure java). then use java CompareStrings abc cbs to run this. and this will give you out put as abc

Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115