• 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

Need help to fix error

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey hi, i am new here and new to java.

while complining this code i got an error:class, interface or enum expected

plss help me.

 
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch!

Please UseCodeTags (this is a link) when posting. I have added them for you this time.

Take a close look at line 1. There is something missing.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch again.

As well as Paweł adding code tags, I have broken the long lines in your code for reasons explained here, and you can see a good way to shorten lines.
You can get that compiler error in two circumstances. One is that you have too many } in your code and the error gives a line number at the end of your code. The other is that you have done what Paweł hopes you will find and then you get a compiler error with a line number at the beginning of the code. If you write Public instead of public you get a similar error message, again at the beginning of the code.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The biggest problem is you wrote too much code before compiling. I'd have stopped after writing this much:


You need to get THIS to work before you worry about ANYTHING else. If this works, add in another line or two, then compile again. If this DOESN'T work, you have a lot fewer places to look for the problem.
 
Rancher
Posts: 1093
29
Netbeans IDE Oracle MySQL Database Tomcat Server C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, here is the rub... what your phraseOMatic? You should probably declare it as such.

Do you use an IDE? If so you should probably get a better one or pay attention to the highlighting, hints, and errors that it displays.
 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
At line number one, it should be. You have forgotten to write class keyword.
 
Marshal
Posts: 8857
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

Sankalp Bhagat wrote:At line number one, it should be. You have forgotten to write class keyword.

That is incorrect too. Class names suppose to start with an upper case I think.
 
Sankalp Bhagat
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
just to resolve the error, class keyword should be there. If someone is following Java Naming Conventions, then the class name should always starts with capital letter. But if you don't wanna follow naming conventions, still it's fine.
After successful compilation of the code, you have to execute with the same name as class name.
java phraseOMatic
that's it.
 
Liutauras Vilda
Marshal
Posts: 8857
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

Sankalp Bhagat wrote:If someone is following Java Naming Conventions, then the class name should always starts with capital letter.

I think everyone should follow. And if you notice that someone is not following, it is something that needs to be told right away, especially to beginners, as later on could be very difficult to get rid off the bad habits. Simillarly as you need to direct child to the right direction when he needs that.

Sankalp Bhagat wrote:But if you don't wanna follow naming conventions, still it's fine.

I disagree. It is not fine at all, we found out that above. Probably is better not to think about this approach in all cases

Welcome to the Ranch
 
Sankalp Bhagat
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:

Sankalp Bhagat wrote:If someone is following Java Naming Conventions, then the class name should always starts with capital letter.

I think everyone should follow. And if you notice that someone is not following, it is something that needs to be told right away, especially to beginners, as later on could be very difficult to get rid off the bad habits. Simillarly as you need to direct child to the right direction when he needs that.

Sankalp Bhagat wrote:But if you don't wanna follow naming conventions, still it's fine.

I disagree. It is not fine at all, we found out that above. Probably is better not to think about this approach in all cases

Welcome to the Ranch



You said it is incorrect. class name is supposed to be started with an upper case. So, to just let you know, I said it's fine if we don't write first letter capital. You can never say it's Incorrect. you shouldn't use such words "it is supposed to be". If the first letter was supposed to be written capital, then compiler would have thrown error.

Instead, you should have said "It is always a good programming practice to write the first letter of the class name as capital".
 
Les Morgan
Rancher
Posts: 1093
29
Netbeans IDE Oracle MySQL Database Tomcat Server C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
According to the most popular accepted style standards class names should begin with a capital letter.

Sankalp Bhagat wrote:

Liutauras Vilda wrote:

Sankalp Bhagat wrote:If someone is following Java Naming Conventions, then the class name should always starts with capital letter.

I think everyone should follow. And if you notice that someone is not following, it is something that needs to be told right away, especially to beginners, as later on could be very difficult to get rid off the bad habits. Simillarly as you need to direct child to the right direction when he needs that.

Sankalp Bhagat wrote:But if you don't wanna follow naming conventions, still it's fine.

I disagree. It is not fine at all, we found out that above. Probably is better not to think about this approach in all cases

Welcome to the Ranch



You said it is incorrect. class name is supposed to be started with an upper case. So, to just let you know, I said it's fine if we don't write first letter capital. You can never say it's Incorrect. you shouldn't use such words "it is supposed to be". If the first letter was supposed to be written capital, then compiler would have thrown error.

Instead, you should have said "It is always a good programming practice to write the first letter of the class name as capital".

 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

There are examples where using a small letter to start a class name can cause compiler errors or even logic errors, so Liutauras was quite right to say the class name is supposed to start with a Capital Letter.
 
Les Morgan
Rancher
Posts: 1093
29
Netbeans IDE Oracle MySQL Database Tomcat Server C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Considering Java is case sensitive, it makes for some interesting debugging when you change case and don't realize it ;)

Sankalp Bhagat wrote:

Liutauras Vilda wrote:

Sankalp Bhagat wrote:If someone is following Java Naming Conventions, then the class name should always starts with capital letter.

I think everyone should follow. And if you notice that someone is not following, it is something that needs to be told right away, especially to beginners, as later on could be very difficult to get rid off the bad habits. Simillarly as you need to direct child to the right direction when he needs that.

Sankalp Bhagat wrote:But if you don't wanna follow naming conventions, still it's fine.

I disagree. It is not fine at all, we found out that above. Probably is better not to think about this approach in all cases

Welcome to the Ranch



You said it is incorrect. class name is supposed to be started with an upper case. So, to just let you know, I said it's fine if we don't write first letter capital. You can never say it's Incorrect. you shouldn't use such words "it is supposed to be". If the first letter was supposed to be written capital, then compiler would have thrown error.

Instead, you should have said "It is always a good programming practice to write the first letter of the class name as capital".

 
Liutauras Vilda
Marshal
Posts: 8857
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

Sankalp Bhagat wrote:If the first letter was supposed to be written capital, then compiler would have thrown error.


Not only the compiler errors defines code incorrectness. Could be other reasons as unexpected output format, wrong calculations, or naming convention requirements. And this is one of the case, as this exercise comes from the "Head First Java book", page 16, where reader is required to rewrite the program with text editor. The mentioned book follows good naming convention and OP failed to rewrite it as shown in a book. It is important for OP to follow recommended coding convention, especially at early stages. I didn't want to cause your anger
 
Sankalp Bhagat
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree we should always follow Java Naming Conventions.
But for the program in query/question, the one who asked the question wrote the name in small letters. It's not a good programming practice but still that code will be compiled fine. For that particular question I was talking. You should better read it. As it was user defined class, I said it's fine it will still be compiled.
 
Liutauras Vilda
Marshal
Posts: 8857
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

Sankalp Bhagat wrote:It's not a good programming practice but still that code will be compiled fine... As it was user defined class, I said it's fine it will still be compiled.

We don't know that yet, let's give OP to tell us that

As Campbell Ritchie pointed out earlier, could be the case where lower case of the class name would cause a compiler error, and it would happen if source file would be named as "PhraseOMatic.java". Worth to mention, that this wouldn't happen if class name would not have access modifier "public", but it would still add confusion during the program execution.
 
Sankalp Bhagat
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If and only if the class is public, then and then only the .java file name and the class name must be same
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is another instance where the compiler may throw an error if you don't follow the usual capitalisation practices.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is another instance where incorrect capitalisation will cause errors. The same applies to incorrect spacing and indentation. That instance is when you ask for help. If you confuse the person asked you are risking being given incorrect advice. If you get incorrect advice because you ask the wrong question you only have yourself to blame. There is a risk that such confusion may convert a compiler error to a runtime error or a runtime error to a logic error. Remember that compiler errors are harmless, and runtime errors do little harm.

It is the runtime errors that lose amounts of money that look like telephone numbers or destroy valuable machinery or even kill people by the hundred.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is still one circumstance which I know about because I have read Java Puzzlers by Bloch and Gafter where it is possible to cause compiler errors by incorrect capitalisation.
 
Liutauras Vilda
Marshal
Posts: 8857
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

Sankalp Bhagat wrote:It's not a good programming practice but still that code will be compiled fine.

Agree, you are indeed right.

Sankalp Bhagat wrote:If and only if the class is public, then and then only the .java file name and the class name must be same

Exactly, for OP (veeranjaneyulu kale) this going to be a useful to know as sometimes could give a headache identifying where the problem is
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bloch and Gafter (op cit page 163:-The question is whether the program compiles and runs at all. If it does compile, does it output Black or White? On the next page it asks how you can alter the code so it does something different.That is the same code with all instances of Y Z changed to lower case. Needless to say its behaviour is the same as the original code. Now you can see the confusion which can be caused if class names and variable names don't follow the correct conventions.
 
veeranjaneyulu kale
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks guys for your help.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the information. I fixed the error.
 
Do you pee on your compost? Does this tiny ad?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic