简体   繁体   中英

Java: receiving multiple errors when compiling

NOT LOOKING FOR SOMEONE TO DO THIS FOR ME, PLEASE JUST GIVE ME ADVICE ON WHERE I AM GOING WRONG*

I am completing a homework assignment and have run into a bit of a snag. Here are the basics of the assignment

"Write a program that consists of three classes. The first class will be the actual program.

The second class will simply convert a string to lower case.

The third class will have three methods:

public static String trimmed(String str)

and

public static String trimmed(String str, int len)

and

public static String squeeze(String str)"

I have written the code for the second and third class and they compile cleanly. For my program I keep getting errors when compiling and I cannot figure it out. I have researched, read my text book over and over, yet I am still drawing a blank.

Here is the code I have

package bmclasses;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class BmClasses {

public static void main(String[] args) {

        String text1,text2,text3,text4,text5;
        // Reading from the file
        try {
        BufferedReader in = new BufferedReader(new
        FileReader("bmIN.txt"));
        while (in.ready()) {
            String text = in.readLine();
            text1 = sUtilities.trimmed(text);
            text2 = sUtilities.squeeze(text);         
            System.out.println("As entered: " + text + "Trimmed and Squeezed: text2");
            text3 = sUtilities.trimmed(text2, 10);
            System.out.println("As entered: " + text + "trimmed, squeezed, and shortened to 10 characters :"+text3);
            text4 = s2Lower.Cap2Low(text2);
            text5 = sUtilities.trimmed(text4, 25);         
        }
        in.close();
        }
        catch (IOException e)
        {System.out.println("Input/Output error !");  }
}
}

Here are the errors that I am getting:

bmclasses/BmClasses.java:29: error: cannot find symbol
            text1 = sUtilities.trimmed(text);
                    ^
symbol:   variable sUtilities
location: class BmClasses
bmclasses/BmClasses.java:30: error: cannot find symbol
            text2 = sUtilities.squeeze(text);         
                    ^
symbol:   variable sUtilities
location: class BmClasses
bmclasses/BmClasses.java:32: error: cannot find symbol
            text3 = sUtilities.trimmed(text2, 10);
                    ^
symbol:   variable sUtilities
location: class BmClasses
bmclasses/BmClasses.java:34: error: cannot find symbol
            text4 = s2Lower.Cap2Low(text2);
                    ^
symbol:   variable s2Lower
location: class BmClasses
bmclasses/BmClasses.java:35: error: cannot find symbol
            text5 = sUtilities.trimmed(text4, 25);         
                    ^
symbol:   variable sUtilities
location: class BmClasses
5 errors

What am I doing wrong?

edit

Here is the code for my class sUtilities

package bmclasses;

public class sUtilities {
 public static String trimmed(String str) {
        // calculate the discount amount and total
        String oString = "";
        if (str == null)
            return oString;
        else
            return str.trim();
 }       


 public static CharSequence trimmed(String str, int len) {
        // calculate the discount amount and total
        String oString = "";
        oString = trimmed(str);
        return oString.subSequence(1, len);
}

 public static String squeeze(String str) {
        // calculate the discount amount and total
        return str.replaceAll("  ", " ");
}
}

Correct me if I'm wrong, but it looks like you are compiling your code using javac on the command line locally in the bmclasses directory like so:

C:\some\path\bmclasses> javac BmClasses.java

By default the compiler will use the current directory as the classpath, which will work fine when you try to compile sUtilities , but when you try to compile BmClasses and it tries to access the class sUtilities , the compiler will attempt to resolve it by its fully qualified name, which is bmclasses.sUtilities .

In Java, the compiler expects a correspondence between the qualified name and the directory structure where the source files live. When it tries to look for the dependency bmclasses.sUtilities it is really looking for bmclasses\\sUtilities.java . Since you are already in that directory, the compiler will fail to resolve the relative path and give you the error that it cannot find the symbol to which you are referring.

What you need to do is compile your code from the root of the project, so that the compiler can find the dependencies (you could also manually set the classpath, but it's not worth it in this case) like so:

C:\some\path> javac bmclasses\BmClasses.java

I see that you have defined BmClasses to be a part of the bmclasses package. Make sure that "package bmclasses" is at the top of sUtilities and s2Lower classes. If your classes are in different packages, then BmClasses cannot find them unless you import them.

It can also result from how you are compiling the code. Are you using an IDE such as Eclipse? If not, I would highly recommend it.

Ignore all of the comments saying you didn't declare the variables because they are not variables.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM