aspose file tools*
The moose likes Beginning Java and the fly likes Something wrong with this input statement Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Something wrong with this input statement" Watch "Something wrong with this input statement" New topic
Author

Something wrong with this input statement

Ben Craig
Greenhorn

Joined: Feb 03, 2004
Posts: 6
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

Joined: Jan 12, 2004
Posts: 60
You must use a try/catch statement for the read operation.
Davy Kelly
Ranch Hand

Joined: Jan 12, 2004
Posts: 384
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


How simple does it have to be???
Ben Craig
Greenhorn

Joined: Feb 03, 2004
Posts: 6
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

Joined: Feb 03, 2004
Posts: 6
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

Joined: Nov 26, 2003
Posts: 382
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.


Ever Existing, Ever Conscious, Ever-new Bliss
Kim Kantola
Ranch Hand

Joined: May 17, 2001
Posts: 274
One way to implement a solution is:
Ben Craig
Greenhorn

Joined: Feb 03, 2004
Posts: 6
thanks a million folks, that was a big help!
Sadanand Murthy
Ranch Hand

Joined: Nov 26, 2003
Posts: 382


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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Something wrong with this input statement