Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Something wrong with this input statement

 
Ben Craig
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
System.out.println("Enter a letter ");
String guess2 = input.readLine();
It keeps giving me this error:

unreported exception java.io.IOException; must be caught or declared to be thrown
String guess2 = input.readLine();

I'm lost. All help appreciated in advnace.
 
Maarten Vergouwen
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You must use a try/catch statement for the read operation.
 
Davy Kelly
Ranch Hand
Posts: 384
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is it your trying to do?
please put some code that would help us see what is going on in your program.
this way you will get a quicker response to you problem
Davy
 
Ben Craig
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok thanks for that. its a hangman game, i'm only a beginner to java, but have done c++ before.
public static void main(){
int wrongGuesses = 0;// A Variable that counts how many wrong
// guesses have already been made. Must be < 6
StringBuffer theWord1 = new StringBuffer("*******");
// choose a word
String word2;
word2 = choose_word();
System.out.println("Hangman!!!");
System.out.println(theWord1);
System.out.println("Enter a letter ");
String guess2 = input.readLine();
char guess = guess2.charAt(0);
wrongGuesses = find_letter(guess, word2, wrongGuesses, theWord1);
// if wrongGuesses == 6, game over, user loses
// if no more letters left to guess in word, and wrongGuess < 6, user wins
}

thats the main program for it, still very sketchy, more of a skeleton than anything else.
 
Ben Craig
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
folks thanks for your help, i'm after having the problem pointed out to me...very basic, actually i'm a bit embarressed about it!!!
 
Sadanand Murthy
Ranch Hand
Posts: 382
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
readLine() method throws IOException. You must handle this exception. You can do this one of 2 ways.
1. Have the method that calls readLine() throw IOException. In this case the method that calls this method (which in turn calls readLine()) will have to handle this exception.
2. Wrap the call to readLine() in a try-catch block. Then in the catch block you can do what you want to if IOException does occur.
 
Kim Kantola
Ranch Hand
Posts: 276
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One way to implement a solution is:
 
Ben Craig
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks a million folks, that was a big help!
 
Sadanand Murthy
Ranch Hand
Posts: 382
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


This could be just a nit. Ben, if you want to use the above solution, make sure that guess2 is declared outside the try-catch block.
[ February 04, 2004: Message edited by: Sadanand Murthy ]
[ February 04, 2004: Message edited by: Sadanand Murthy ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic