Win a copy of Modern frontends with htmx this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to declare program as public class?

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm taking an online intro to Java course through my local community college. This is a very beginning Java class, where we turn in assignments through our book publisher's website. The website has its own compiler and for some reason I am getting this error on every project this semester and I can't figure this out.

Here are examples with the compiler error bolded:







It seems like I've gotten the rest of the code correctly, but what simple thing am I missing when I "should declare file names for a public class"? If i can fix this, all of my projects for the course will be correct. Please help!
 
Marshal
Posts: 4475
569
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The messages report that there is a mis-match between the source file name and the public class name

  • File is named FeetToMeters.java, class is named feetToMeters
  • File is named CharCounter.java, class is named ProgrammingExercise6_23
  • File is named SumMajorDiagonal.java, class is named ProgramingEx8_2
  •  
    Winston Luo
    Greenhorn
    Posts: 5
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    So how would I write the code/what modifications would I make so I don't get this message/error?
     
    Ron McLeod
    Marshal
    Posts: 4475
    569
    VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Make the name of the file and the public class the same.

    For example, change the name of the class in the FeetToMeters.java file to FeetToMeters rather than feetToMeters.
     
    Marshal
    Posts: 8853
    637
    Mac OS X VI Editor BSD Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Winston Luo wrote:So how would I write the code/what modifications would I make so I don't get this message/error?


    There are some rules you need to remember regarding this.

    1. Within one source file there cannot be more than one public class. There can be more classes, but at most one can be public, that means other classes need to be non public.
    2. If a class is public, it needs to match its file name in which it is defined.
    3. If a class isn't public, name of the file and of the class doesn't need to match.

    I may forgot some rules, but these should help you avoid some of the errors.
     
    Winston Luo
    Greenhorn
    Posts: 5
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I still don't understand what I'm doing wrong. Can anyone please just correct the original code I posted in this thread? It's literally just an error with one line...

    PLEASE HELP! I'm getting desperate bc the semester is over in a week and this public class error thing for the name of the project code is only thing holding me back from getting it 100 percent correct.

    I appreciate the feedback so far but will someone actually run my code through their compiler or just tell me what to fix!?

    Again these are the errors that I am getting from the textbook publisher's website for three projects:

    1.  (Process a string ) Write a program  that prompts the user to enter a string  and displays its length and its first character .

    "StringLength.java:3: error: class ProgrammingEx4_20 is public, should be declared in a file named ProgrammingEx4_20.java
    public class ProgrammingEx4_20 {"

    for



    2. Count positive and negative numbers and compute the average of numbers) Write a program  that reads an unspecified number of integers , determines how many positive and negative values  have been read, and computes the total and average of the input values  (not counting zeros). Your program  ends with the input 0. Display the average as a floating-point number.


    "AvgWithLoop.java:3: error: class ProgrammingEx5_1 is public, should be declared in a file named ProgrammingEx5_1.java
    public class ProgrammingEx5_1 {"

    for



    3. (Occurrences of a specified character ) Write a method  that finds the number of occurrences of a specified character  in a string  using the following header:
    public static  int  count(String  str, char  a). Add a main method  to the class  that tests the method  by prompting for a string  and a character , invoking the method  and printing the returned count.

    "CharCounter.java:3: error: class ProgrammingExercise6_23 is public, should be declared in a file named ProgrammingExercise6_23.java
    public class ProgrammingExercise6_23 {"

    for



    PLEASE HELP!



     
    Ron McLeod
    Marshal
    Posts: 4475
    569
    VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    The code seems fine - the problem is that name of the file: StringLength.java does not match the name of the public class: ProgrammingEx4_20.  Have you tried changing either the name of the file or the name of the public class so that they are the same?

    I compiled and ran for first project (naming the source file to ProgrammingEx4_20.java) and it ran fine:
        Enter some string:I can't seem to ping the other network
        The string length is 38
        The first character is I


    As you mentioned, your compiler is web-based, and without have access to that system it's difficult to provide much more help.

    Winston Luo wrote:This is a very beginning Java class, where we turn in assignments through our book publisher's website. The website has its own compiler and for some reason I am getting this error on every project this semester and I can't figure this out.


     
    Winston Luo
    Greenhorn
    Posts: 5
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thanks for the reponse...could you possibly copy n paste the changes youve made of the code that ran fine? Which lines should i change?

    I can't traditionally save the code with a file name because the web based publisher's homework check website just has a box to write code in, submit, and then runs it through their own compiler and verifies whether the code was writtem correctly.

    I know I'm asking for a lot but could you directly copy n paste the changes you made in my original code to the forum so I can use that as a model for 8 other projects that all have this 1 stupid error...
     
    Ron McLeod
    Marshal
    Posts: 4475
    569
    VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I didn't make any changes to your code - I ran it as-is.  Maybe try renaming your class to match file name mentioned in the error message - from ProgrammingEx4_20 to StringLength

    StringLength.java:3: error: class ProgrammingEx4_20 is public, should be declared in a file named ProgrammingEx4_20.java public class ProgrammingEx4_20 {

    I'm not sure how I can help you more.  The problem is specific to the system being used to upload and compile the code.  I suggest you contact the school/publisher/website-owner to get support.

    I can offer this as an example of what another on-line compiler looks like, and how it needs to be configured/used to compile and run the code for your first project - maybe it will help you understand what you need to do with the system you are trying to use:
     
    Sheriff
    Posts: 17644
    300
    Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    The rule for Java is that if you have a file named MyClass.java, the only public class that you can declare in it is MyClass, like so:

    The error message you're getting suggests that this is what is happening:

    The error message is technically correct but it's a little misleading as far as telling you exactly what you need to do. However, given these examples, you should be able to figure it out now.
     
    Java Cowboy
    Posts: 16084
    88
    Android Scala IntelliJ IDE Spring Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Winston Luo wrote:I can't traditionally save the code with a file name because the web based publisher's homework check website just has a box to write code in, submit, and then runs it through their own compiler and verifies whether the code was writtem correctly.


    Then you need to rename your class, so that it conforms to what the website expects.

    For example, if the website uses the filename CharCounter.java then you should name your class CharCounter, and not ProgrammingExercise6_23.
    reply
      Bookmark Topic Watch Topic
    • New Topic