• 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
  • Ron McLeod
  • Paul Clapham
  • Rob Spoor
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Himai Minh

Program won't run

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello, I'm trying to run this program but I keep getting a "top-level function main() not found" error. How do I fix this? I already have a main method, so I'm not sure what else could be the cause.




 
Saloon Keeper
Posts: 8458
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
missing the word "static"

public static void main( String[] args ) {
 
Carey Brown
Saloon Keeper
Posts: 8458
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should not be using a double for your grades. Doubles are not exact and when compared using "==" may not find the match you were expecting. Change everything to use "int".

You can then simplify your if's and have only one compare in them by starting with the highest grade and working down.


Also you attempted to use Code Tags but didn't quite get it right. Read this link UseCodeTags (<-link).
 
Avery Lia
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I added the static and had to create fields for my arrays as a result. I'm still getting the main class error, and so I'm wondering what else it could be. Also I have some skepticism towards other parts of my code. I'm not sure if I nested the if else statements correctly, and if I incorrected tried to incorporate them incorrectly. Pretty much not sure if this is the best code to do the job I want, which is have a user  input their number grade, and receive a letter grade.
 
Carey Brown
Saloon Keeper
Posts: 8458
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:

As you can see here, "else if"s shouldn't be nested.
 
Carey Brown
Saloon Keeper
Posts: 8458
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You've got a conflict in that main() MUST be "static", which means all your grade arrays MUST also be "static" (e.g. APlus[]).
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!

1) is the filename "Lab4A.java"? or something else? Because you need to call it as Lab4A.java if your class is called Lab4A.

2) check if you have corrected your main method like this


3) start small ... remove all the code inside your main method and add just one System.out.println ... like this



and then if this works continue adding more of your code.

Which IDE do you use?
How do you start / compile your class? With plain javac or with Eclipse / IntelliJ / VisualCode ?
 
Marshal
Posts: 73766
332
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
PL: welcome to the Ranch
 
Campbell Ritchie
Marshal
Posts: 73766
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:. . . starting with the highest grade and working down. . . .

I have a concept that the boundary value (98 for A+) is in the higher category, so I start from the smallest grade and work up.
 
Campbell Ritchie
Marshal
Posts: 73766
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If the boundary value is in the lower category, as for weights of parcels, then I start high and work down. Both those formats avoid the use of <= and >=.
 
Sheriff
Posts: 16578
277
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

Carey Brown wrote:As you can see here, "else if"s shouldn't be nested.


[pedantic mode=ON]

Technically, they *are* nested regardless of how the statements are formatted. Formatting-wise, you don't need to indent them to show the nesting. The if-else-if... construct is so common that there's a formatting convention specifically for it.

Typically, it's advisable to always enclose the body parts of an if-else statement in {} to avoid inadvertently injecting bugs during maintenance. For example,

The reason the former snippet could attract bugs is this:

When the call to doAnotherThing() was added in the first snippet, the indentation clearly shows that it was meant to be called as part of the else clause. However, Java doesn't care about indentation; that's just a programmer convention. Without the braces in the else part, the code is interpreted by Java as this:

The convention used for a series of nested if-else-if-... statements is the exception to this rule. In this case, you purposely don't use braces in the else parts (except the last one) because there is only one statement in it's body: the next nested if-statement. This way, the indentation is flatter than the actual nesting is.

Ignoring this convention and following the "normal" indentation convention instead, the code for an if-else-if series of statements would be formatted like this:

You'll notice that in the last, most deeply-nested if-else statement, the convention to use {} in the else clause is again followed. The flat formatting convention  makes it look like the last closing brace is ending the entire series of nested if-else-if statements but it's really just closing out the else part of the last most deeply-nested if-else statement.

[pedantic mode=OFF]
 
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic