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?

3
  • 2
    Have you set your classpath? Commented Aug 14, 2013 at 8:30
  • 1
    Is that your complete file? How are you compiling it? How are you running it? Commented Aug 14, 2013 at 8:31
  • i want just to be clear about it: the problem is not with your code Commented Aug 14, 2013 at 8:32

5 Answers 5

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.

Sign up to request clarification or add additional context in comments.

4 Comments

+1 for being quicker :)
Dude, if you get two more upvotes, you'll get a badge soon :) that too your first answer on SO :) best luck
This answer needs to be accepted now.
Thanks alot, solved my issue. Accepted your answer :]
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

Comments

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

Comments

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

Comments

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.