简体   繁体   中英

Multi class in Notepad++

So I am unfamiliar with Notepad++ and have a working program in Blue J. I tried to transfer it over but I continue to receive the error:

"Average.java:5: error: class UserInput is public, should be declared in a file named UserInput.java public class UserInput ^

Note: Average.java uses unchecked or unsafe operations.

Note: Recompile with -Xlint:unchecked for details. 1 error"

I think it has to do with how I wrote each class in but I am unsure how to fix it.

在此处输入图片说明

Only one public class is allowed in a file, and it should have the same name as the file.

The first solution is quite well define in the error. " UserInput [...] should be declared in a file named UserInput.java ".
The second solution is to change the visibility of the class

in A.java

public class A {}
class B{}
protected class C{}
private class D{}

public class E{} //THIS IS NOT ALLOWED, it should be in E.java

Note that even if only one public class can be define in a file, it is not mandatory to have a public class.

X.java

protected class X{} //this is valid. 

But don't add another class in this file as public, only X can be public in X.java

In Java there can only be 1 public class declared per java file. So to quickly resolve your issue you could simply split out the UserInput class into it's own file called UserInput.java , it is as simple as that.

For a bit complexity though you could look into Inner Classes or Local Classes which would allow you declare an additional class(es) within the one file.

Have a read of

Here is an example of a LocalClass (Code taken from Local Inner Classes Documentation )

public class LocalClassExample {

    static String regularExpression = "[^0-9]";

    public static void validatePhoneNumber(String phoneNumber1, String phoneNumber2) {
        final int numberLength = 10;

        // Valid in JDK 8 and later:

        // int numberLength = 10;

        class PhoneNumber {

            String formattedPhoneNumber = null;

            PhoneNumber(String phoneNumber) {
                // numberLength = 7;
                String currentNumber = phoneNumber.replaceAll(
                regularExpression, "");
                if (currentNumber.length() == numberLength)
                    formattedPhoneNumber = currentNumber;
                else
                    formattedPhoneNumber = null;
            }

            public String getNumber() {
                return formattedPhoneNumber;
            }

            // Valid in JDK 8 and later:

            // public void printOriginalNumbers() {
                // System.out.println("Original numbers are " + phoneNumber1 +
               // " and " + phoneNumber2);
            // }
        }

    PhoneNumber myNumber1 = new PhoneNumber(phoneNumber1);
    PhoneNumber myNumber2 = new PhoneNumber(phoneNumber2);

    // Valid in JDK 8 and later:

    // myNumber1.printOriginalNumbers();

    if (myNumber1.getNumber() == null) 
        System.out.println("First number is invalid");
    else
        System.out.println("First number is " + myNumber1.getNumber());
    if (myNumber2.getNumber() == null)
        System.out.println("Second number is invalid");
    else
        System.out.println("Second number is " + myNumber2.getNumber());

    }

    public static void main(String... args) {
       validatePhoneNumber("123-456-7890", "456-7890");
    }
}

This code would print out

First number is 1234567890
Second number is invalid

As @AxelH has stated this could be a bit complex for someone without much knowledge, however this is a fun little exercise to play with. For a more simple I would refer you to @AxelH answer.

class UserInput is public, should be declared in a file named UserInput.java

I guess the exception is self-explanatory. Just rename to File name to UserInput.java and it will work!

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