0

Well im really new to java and im really trying hard to understand what can be done in java and what can't. I'm making a console application based on the well known game Hangman. Basiclay what i'am trying to do is stop the user from typing 'e's more than twice, to do that i made 2 methods:

The first one adds 1 to the int variable mManyTimes whenever the user types e.

  public boolean adder() {
   boolean tooMuch = letter == 'e';
   if(tooMuch) {
     mManyTimes ++;
   }
   return tooMuch;
  }

the second method is the one that sends an exception to the user when the user types e more then twice.

public void cheatStopper() {
  if(mManyTimes == 3) {
   throw new IllegalArgumentException("You cant type more Es");
  }
 }

Basicly i created two files, one that holds the code of the game(Which those two methods are in) and other one.

the file that holds the logic of the game is Game.java and here is the code that is inside it:

public class Game {
  private String mAnswer;
  private String mHits;
  private String mMisses; 
  private int mManyTimes;
  public char letter;

  public Game(String answer) {
    mAnswer = answer;
    mHits = "";
    mMisses = "";
  }

  public boolean applyGuess(char letter) {
   //checks for char letter inside the mAnswer variable.
   //If it is there the indexOf() method should return the index of the letter.
   //If it is not  there it will return -1.
   //We are basicly saing if indexOf() method returns 0 or more then that then the isHit 
   //if it is not then the isHit boolean will return false.
   boolean isHit = mAnswer.indexOf(letter) >= 0;

   if (isHit) {
    mHits = mHits + letter;
    } else {
      mMisses = mMisses + letter;
   }
   adder();
   return isHit;


  }

  public boolean adder() {
   boolean tooMuch = letter == 'e';
   if(tooMuch) {
     mManyTimes ++;
   }
   return tooMuch;
  }

 public void cheatStopper() {
  if(mManyTimes == 3) {
   throw new IllegalArgumentException("You cant type more Es");
  }
 }

The other file that holds the main() method and prints the code to the console is Hangman.java:

    public class Hangman {

    public static void main(String[] args) {
        // Enter amazing code here:
        Game game = new Game("treehouse");
        game.applyGuess('e');
        game.applyGuess('e');

        System.out.println(game.cheatStopper());
    }

}

So here is the question that frustrated me and i never found and answer for: How do i get my code to work and stop the user from typing more then two e. Well well i know my code has many errors and bad structure but dont forget that im new to java, and thanks for advance :).

  • 1
    Your title and your post are asking two different questions. As for your title: Yes methods can return Exceptions. Exceptions are objects, after all. – Tennyson H Feb 10 '16 at 20:30
  • `return` and `throw` are two different concepts and just so happen to be keywords in Java – OneCricketeer Feb 10 '16 at 20:32
  • You could have got this googling "how to handle exceptions java", btw. – djechlin Feb 10 '16 at 20:32
  • `cheatStopper` will only throw the exception if there's 3 `e`s typed. Try chanhging the 3 to 2 and the exception will be thrown – Kenney Feb 10 '16 at 20:32
  • You can return an Exception from a method but this is very rarely done. Usually you throw an Exception in an method. – Peter Lawrey Feb 10 '16 at 20:48
  • Well I had two questions in my mind that's way my content and title are different but whay can i print cheatStopper to the console then. – Ibrahim Crauf Feb 11 '16 at 05:35

1 Answers1

0

You need a catch block. Look up how to catch exceptions.

djechlin
  • 59,258
  • 35
  • 162
  • 290