• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

import java.io.*; problem

 
Rubens Martins
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone, I'm here cause I'm having this problem in my program:





what should I do about it? the tutorial I'm using does not say to save three different files, so I think this was supposed to work.

Ty


 
Hunter McMillen
Ranch Hand
Posts: 492
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your import statements need to be the first line in the saved file. So instead of having the import in the middle, move it to the top.

-Hunter
 
Rubens Martins
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As I said before, I am not supposed to save three different files. I've tried that already, and it states three errors:



and I have no idea of how to solve this.

Help please???

 
Hunter McMillen
Ranch Hand
Posts: 492
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry I was a little confused at first since the title of this post was regarding imports. Now I think I know what you are asking. Only one class can be public within a java file. So remove the public keyword from all but one of the classes.

-Hunter
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15279
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rubens Martins wrote:As I said before, I am not supposed to save three different files.

One of the rules of Java is that if you make a top-level class public, it has to be saved in a file with the same name as the class (and the .java extension). You have two public classes: SimpleDotCom and GameHelper. These need to be in two separate files named SimpleDotCom.java and GameHelper.java.

You can also make the classes not public, as Hunter suggests.
 
Rob Spoor
Sheriff
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rubens Martins wrote:

That error is unrelated to the classes being public or not. Math.random() is a method so you need to add parentheses.
 
Rob Spoor
Sheriff
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hunter McMillen wrote:Your import statements need to be the first line in the saved file. So instead of having the import in the middle, move it to the top.

-Hunter

It doesn't need to be the first line, but the only things that are allowed to be placed before import statements are the (optional) package declaration, comments and whitespace. Classes definitely do not fall into that group
 
Campbell Ritchie
Sheriff
Pie
Posts: 48940
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yo have got answers, Rubens Martin, and welcome to the Ranch
 
Rubens Martins
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, I'll remove the publics and add parentheses, thanks everyone!
 
Campbell Ritchie
Sheriff
Pie
Posts: 48940
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, removing the public keywords is not the right answer. You ought to correct the names of the files.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic