aspose file tools*
The moose likes Beginning Java and the fly likes import java.io.*; Causing beginner programme not to work (Head first Java) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "import java.io.*; Causing beginner programme not to work (Head first Java)" Watch "import java.io.*; Causing beginner programme not to work (Head first Java)" New topic
Author

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

sean cronin
Greenhorn

Joined: Jun 20, 2013
Posts: 8
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.


Life is a soundtrack, Play it loud! https://soundcloud.com/sean-cronin
sean cronin
Greenhorn

Joined: Jun 20, 2013
Posts: 8
Ok maybe I shouldn't of posted the whole code as that looks a mess on screen lol?
Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

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.


  • The quieter you are, the more you are able to hear.
    sean cronin
    Greenhorn

    Joined: Jun 20, 2013
    Posts: 8
    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
    Greenhorn

    Joined: Jun 20, 2013
    Posts: 8
    Excellent stuff that works. Thanks!!
    Kemal Sokolovic
    Bartender

    Joined: Jun 19, 2010
    Posts: 825
        
        5

    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
    Greenhorn

    Joined: Jun 20, 2013
    Posts: 8
    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

    Joined: Jun 19, 2010
    Posts: 825
        
        5

    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
    Greenhorn

    Joined: Jun 20, 2013
    Posts: 8
    How does the main class then know to call up the others?
    Kemal Sokolovic
    Bartender

    Joined: Jun 19, 2010
    Posts: 825
        
        5

    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
    Greenhorn

    Joined: Jun 20, 2013
    Posts: 8
    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

    Joined: Jun 19, 2010
    Posts: 825
        
        5

    Oracle's tutorial is always the best place to start.
    sean cronin
    Greenhorn

    Joined: Jun 20, 2013
    Posts: 8
    Cheers Kemal, I'll check that spot out!! I'm so new to Java it's good to see what people are using. Thanks!
     
    wood burning stoves
     
    subject: import java.io.*; Causing beginner programme not to work (Head first Java)