-2

I'm write everything according to my book Head First Java

public class SimpleDotComTestDrive{
public static void main(String[] rip ){
SimpleDotCom dot = new SimpleDotCom();
int[] locations ={2,3,4};
dot.setLocationCells(locations);
String userGuess ="2";
String result = dot.checkYourself(userGuess);
}
}

public class SimpleDotCom{
    int[] locationCells;
    int numOfHits=0;
    public void setLocationCells(int[] locs){
    locationCells=locs;
    }
public String checkYourself(String stringGuess){
    int guess= Integer.parseInt(stringGuess);
    String result="miss";
    for(int cell : locationCells){
        if(guess== cell){
            result = "hit";
            numOfHits++;
            break;
            }
    }
    if(numOfHits == locationCells.length){result="Kill";}
System.out.println(result);
return result;
    }
    
}

This is the Error

class SimpleDotCom is public, should be declared in a file named SimpleDotCom.java

AND if i tried to save my file on the SimpleDotCom class i still got error like ename expected(brackets).

1

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Nili
  • 89
  • 2
  • 8
  • You have two differently named public top-level classes in a single file. That won't fly. Put them in two different files or make one of them non-public. – Joachim Sauer Sep 08 '20 at 19:28
  • Please note: this is really basic stuff. Try using a search engine before asking a new question here. Rest assured: anything you run into right now ... has been asked here. Many many times. Then: avoid using images. Your console is pure text, too. So copy text as text, and format it nicely. – GhostCat Sep 08 '20 at 19:30
  • 1
    And note: the typical answer is to have something like "src/java/main" for your "production code", and "src/java/test" for your test cases. And then X.java goes into main, and XTest.java can go into test ... so: two public classes, each one with its own file. – GhostCat Sep 08 '20 at 19:31
  • https://stackoverflow.com/questions/13811020/error-class-x-is-public-should-be-declared-in-a-file-named-x-java – starball Apr 23 '23 at 22:46

1 Answers1

0

It is allowed to have only ONE public class in file. The name of .java file should be the same as public class. In your case file should be named as "SimpleDotComTestDrive.java" There is another issue in your code - you have two public classes. Please remove "public" modifier from one of classes. OR You could remove "public" modifier from both classes and name file as you wish.