-2

im starting my adventure with learning to code in Java. Im stuck on an example from book. It does compile but there is no effect when I run it. The output is just blank. Im using Visual studio code. The example says that all 3 separate files (ProstyPortal, ProstyPortalGra & PomocnikGry) should be in 1 folder. When i try to start a file with main also nothing happens. I decided to join them all into one file but still i do get no result. The code is basically one line battleship game example.

ftp://ftp.helion.pl/przyklady/javrg2.zip - in folder r05 there are files (ProstyPortal, ProstyPortalGra & PomocnikGry) from the example that should work when in the same folder but they dont. Damn, its really hard to find whats wrong when you just start learning :P

Its an example from chapter nr 5.

I did follow all the rules and sugestions as in the book but even a straight copying the code does not help. the previous examples i did run without bigger issues.

import java.io.*; 
class ProstyPortalGra {
  public static void main(String[] args) {
    int iloscRuchow = 0;
    PomocnikGry pomocnik = new PomocnikGry();

    ProstyPortal portal = new ProstyPortal();
    int liczbaLosowa = (int) (Math.random() * 5);

    int[] polozenie = {liczbaLosowa, liczbaLosowa+1, liczbaLosowa+2};
    portal.setPolaPolozenia(polozenie);
    boolean czyIstnieje = true;

    while (czyIstnieje == true) {
      String pole = pomocnik.pobierzDaneWejsciowe("Podaj liczbę");
      String wynik = portal.sprawdz(pole);
      iloscRuchow++;
      if (wynik.equals("zatopiony")) {
        czyIstnieje = false;
        System.out.println(iloscRuchow + " ruchów");
      } // koniec if
    } // koniec while
  } // koniec main
  }

  class ProstyPortal {

    int [] polaPolozenia;
    int iloscTrafien;

    public void setPolaPolozenia(int[] ppol) {
      polaPolozenia = ppol;
    }

    public String sprawdz(String stringPole) {
      int strzal = Integer.parseInt(stringPole);
      String wynik = "pudło";
      for (int pole : polaPolozenia) {
        if (strzal == pole) {
          wynik = "trafiony";
          iloscTrafien++;
          break;
        }
      } // koniec pętli
      if (iloscTrafien == polaPolozenia.length) {
        wynik = "zatopiony";
      }
      System.out.println(wynik);
      return wynik;
    } // koniec metody
  } // koniec klasy

 class PomocnikGry {
  public String pobierzDaneWejsciowe(String komunikat) {
    String wierszWej = null;

    System.out.print(komunikat + " ");
    try {
      BufferedReader sw = new BufferedReader(
        new InputStreamReader(System.in));
      wierszWej = sw.readLine();
      if (wierszWej.length() == 0) return null;
    } catch (IOException e) {
      System.out.println("IOException: " + e);
    }
    return wierszWej;
  }
}
arkusie
  • 1
  • 1
  • Did you debug through it to see it's actually working from a to z? – achAmháin Jul 04 '18 at 14:57
  • Yes, the debugger is your friend here... Maybe it's running an infinite loop. The contents of the file may not be what this algorithm is expecting. – Rodrigo Martin Tato Rothamel Jul 04 '18 at 14:58
  • you could add a System.out.println("something") to the start and end from the main method...that way you can check if it starts and /or ends running – Tom Jul 04 '18 at 15:00
  • `if (wierszWej.length() == 0) return null;` You need to check if `wierszWej == null` first before trying to access `length()`, since `readLine` can return `null`. – 001 Jul 04 '18 at 15:10
  • I did add System.out.println("i do work"); in the main but it does not print anything, so ye, i have no idea where is the problem :D – arkusie Jul 04 '18 at 15:20

2 Answers2

0

So you want it to print something? If that's the problem, then you must make sure that the if-statement that tests whether the wynik is equal to "zatopiony" actually happens to be true. You program will only give an output, if this if-statement is true.

  • There 3 classes but i cant get them to work. I tried all in one file and I tried to separate them into 3 files in the same folder. Im not really sure what is wrong especially i used already existing code after mine didnt work. The code i presented is straight from the book, If there is really a mistake in a book then i dont know how i am supposed to learn further :) – arkusie Jul 04 '18 at 20:00
  • I understand. All classes are separate Java files. But use packages to add the files to folders. You can write "package exercise" in the beginning of the file. – Martin Pekár Jul 04 '18 at 20:59
  • i added package NAME, at the begining of the each file but now i get the error: Error: Could not find or load main class, where clearly i have the main class in the file. I tried to: -classpath . thepackagename.TheClassName but the terminal drops info thatThe term '-classpath' is not recognized. I saw this solution in other post but its not working for me. Seems i got something wrong with classpath ? – arkusie Jul 05 '18 at 18:24
  • You should have a folder named "src". In that folder are other folders depending on the package name. So if you write package NAME on top of the file, then the file should be located in the folder named NAME, which is located in the folder "src". The Java files must then be located in the proper folder – Martin Pekár Jul 05 '18 at 21:35
  • i did place all files in \src\java\name and still its not working. Now i get "The declared package "name" does not match the expected package " problem – arkusie Jul 06 '18 at 10:16
  • Because your path is src/java/name, you must write "package java.name;". – Martin Pekár Jul 06 '18 at 10:18
0

I separated the classes into separate files with proper names located in the same folder. Still doesnt workin and i do get this error : error: cannot find symbol.

this error occurs for every separated class. I checked all common errors from this thread: What does a "Cannot find symbol" or "Cannot resolve symbol" error mean?

Files are saved with UTF-8 encoding if that means anyting. When i saves with BOM encoding i got a lot more errors

miken32
  • 42,008
  • 16
  • 111
  • 154
arkusie
  • 1
  • 1