-2

i'm a new to java and just bought this amazing book to start learning with. One of the exercises, it asked me to do this ( it's exactly like in the book ) :

class SimpleDotComTestDrive {
public static void main (String[] args) {

    SimpleDotComTestDrive hu = new SimpleDotComTestDrive();

    int[] locations = {2,3,4};

    hu.setLocationCells(locations);

    String userGuess = "2";

    String result = hu.checkYourself(userGuess);

    String testResult = "failed";

    if (result.equals("hit") ) {
        testResult = "passed";
    }
    System.out.println(testResult);
}

}

I compiled this code on Notepad++ which compiles normally for the pass few weeks , until i compiled this code and got this error :

   SimpleDotComTestDrive.java:8: error: cannot find symbol
    hu.setLocationCells(locations);
      ^
    symbol:   method setLocationCells(int[])
    location: variable hu of type SimpleDotCom

   SimpleDotComTestDrive.java:12: error: cannot find symbol
    String result = hu.checkYourself(userGuess);
                      ^
    symbol:   method checkYourself(String)
    location: variable hu of type SimpleDotCom
   2 errors

It's pretty annoying since I've searched over the internet for the past few hours and couldn't fix it, please, if you have any idea what's wrong with this then please let me know as soon as possible, thanks in advance !!!

LOOK !!! i know notepad++ isn't the best IDE but the book recommended me to use simple IDE for learning purposes so please, don't ask me to use other IDE, thanks !

Toto
  • 89,455
  • 62
  • 89
  • 125
  • The first section of code you posted says the class name is `SimpleDotCom` but your stack trace references `SimpleDotComTestDrive`. I looked up the code online (it's from Head First Java, right?) and it looks like the class you posted is supposed to be called `SimpleDotComTestDrive` and it's testing another class called `SimpleDotCom`. You're doing this in Notepad++... how are you compiling? If you're compiling one class at a time - make sure you recompile *both* `SimpleDotCom` and `SimpleDotComTestDrive`. – Nate Sep 16 '15 at 12:53
  • oops, sorry, i fixed the error code again after you answer, thanks for your advice but it doesn't work, in the book, it only mention 1 class: SimpleDotComTestDrive but i couldn't compile so i changed the class's name into SimpleDotCom but forget to compile again... – Huy Tran Van Sep 17 '15 at 10:23
  • SimpleDotComTestDive is missing a "r" should be "Drive" not "Dive" – kukudas Sep 17 '15 at 11:04
  • @MrChickenongrills -The book is introducing test driven development (TDD) here. Up to this point they've talked conceptually about the `SimpleDotCom` class (what methods it will have, etc.) but given no actual code. They then give you the code for a class called `SimpleDotComTestDrive` to test the conceptual `SimpleDotCom` class. At this point `SimpleDotComTestDrive` *shouldn't* compile because `SimpleDotCom` doesn't exist yet (aka - the test starts off "failing"), but over the next 2-3 pages you think about how to get the test to "pass". The final code for both classes is around page 106. – Nate Sep 17 '15 at 12:59
  • oops, sorry, don't be mad, i didn't finish the book yet so thank you for this answer anyway !!! – Huy Tran Van Sep 18 '15 at 11:47

2 Answers2

0

You mentioned that hu.setLocationCells(locations); which means that there needs to be a method setLocationCells() that takes int[] as parameter. Add that method for you to work.

By the way, Notepad++ is not an IDE at all. But yes, it's right to start with this. Good luck.

Karthik R
  • 5,523
  • 2
  • 18
  • 30
-1

The variable hu which you are trying to use is declared in the main method in SimpleDotCom class and you are trying to access it from SimpleDotComTestDrive class. So the scope of hu is limited to main method itself.
Try declaring it at the class level, either static or instance variable and try to compile the code.

Surya
  • 350
  • 2
  • 11
  • Uh, sorry, it was my bad, it actually is SimpleDotComTestDrive.java but i must have accidentally changed it into SimpleDotCom, anyway, i did what you told me, i still use hu in the main method but it keeps getting me error!!! Thanks for your answer !!! – Huy Tran Van Sep 17 '15 at 10:44