• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

import java.io.*; Causing beginner programme not to work (Head first Java)

 
sean cronin
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone,

My first post on here, as only started programming java since last week using Head First Java book. I'm working on the the Chapter 5 Battleships game and just can't figure out why this piece of code is causing me an error. "error: class, interface or enum expected". I thought this maybe of been a semi-colon or something but everything seems to be in place. The code is below!



Any help would be great!

Thanks,

Seán.
 
sean cronin
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok maybe I shouldn't of posted the whole code as that looks a mess on screen lol?
 
Kemal Sokolovic
Bartender
Posts: 825
5
Java Python Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch!

To make code look nicer please UseCodeTags (<-click). I have added them for you this time.

As per the problem you have, I guess that all the classes you posted are in single .java file. If I'm rigth, you should pay attention to the following:
  • import statement(s) must be placed before any class definition statement and after package statement (you don't have the second one in your code, so import statement would be the first one).
  • Only one class per .java file can be declared as public, you have two of them declared as such.
  •  
    sean cronin
    Ranch Hand
    Posts: 33
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hey, thanks for the quick reply and putting in those tags for me. I'll be sure to do that from now on.

    Ya i've noticed that with the public classes actually. In the book Each class is set to public but they don't work that way so will get rid of the extra one there.

    So i've called the Import in the wrong place right!? Book doesn't seem to say much about placement of the import, just says copy this code. T'll give the code a new run so!!
     
    sean cronin
    Ranch Hand
    Posts: 33
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Excellent stuff that works. Thanks!!
     
    Kemal Sokolovic
    Bartender
    Posts: 825
    5
    Java Python Ruby
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    sean cronin wrote:In the book Each class is set to public but they don't work that way so will get rid of the extra one there.

    If that's the case, it is probably intended for each class to be placed in a separate .java file with the same name.
     
    sean cronin
    Ranch Hand
    Posts: 33
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    So I save each class in its own .java instead of all 3 in the same one? How would I name them the same, cause they will only overwrite each other??
     
    Kemal Sokolovic
    Bartender
    Posts: 825
    5
    Java Python Ruby
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Not all the files should have the same name, but each file should be named the same as the (public) class defined within it.
    So, if you define Foo class you save it in Foo.java.
     
    sean cronin
    Ranch Hand
    Posts: 33
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    How does the main class then know to call up the others?
     
    Kemal Sokolovic
    Bartender
    Posts: 825
    5
    Java Python Ruby
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    If they are all in the same package, there won't be a problem with that; otherwise you would use import statement for other classes.
    Though it's a question for another topic, I would suggest you read a Java tutorial, specifically the Packages section, to get more details. I'm posting from phone now so I can't provide a link, but if you Google for it you'll find it easily.
     
    sean cronin
    Ranch Hand
    Posts: 33
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I'll look into that. There seems to be a good few ones when I googled so will try a few out. Nice one!!
     
    Kemal Sokolovic
    Bartender
    Posts: 825
    5
    Java Python Ruby
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Oracle's tutorial is always the best place to start.
     
    sean cronin
    Ranch Hand
    Posts: 33
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Cheers Kemal, I'll check that spot out!! I'm so new to Java it's good to see what people are using. Thanks!
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic