 |
AppletTalk.com Java discussions newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
crazyguitarchik Guest
|
Posted: Fri Nov 25, 2005 4:57 pm Post subject: System.in.read() repeats its output at least twice!? |
|
|
Hello,
This is my first post, am learning java for fun. Trying to write a hangman
game in TextPad. I haven't finished the code yet but the problem I am having
is that when I enter a letter into the keyboard and press enter, I get the
same output at least twice. I want to know why this is and how I can make
only output once. I have been looking for a way to enter a single character
and use that to check if it exists in the secret word without using any extra
classes like Keyboard().
The code executes if you type "java Hangman word 4" where word is a text file
of words and 4 is thenumber of guesses.
My code so far:
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.StringTokenizer;
import java.util.Random;
import java.io.InputStreamReader;
public class Hangman
{
//method to check that the number of guesses is a number
public static boolean checkInt(String input)
{
boolean valid = true;
int length = input.length();
char ch;
for (int i=0; i
{
ch = input.charAt(i);
if (!Character.isDigit(ch))
valid=false;
}
return valid;
}
public static void main(String[] args)
{
String[] wordArray = null;
BufferedReader reader = null;
//check number of command line arguments
if (args.length <2)
{
System.out.println("Usage: Game file, text file and number of guesses.");
System.exit(1);
}
//if number of guesses is not a number, exit code
if (!checkInt(args[1]) )
{
System.out.println(args[1]+" is an invalid input for number of guesses.");
System.exit(1);
}
int guesses = Integer.valueOf(args[1]);
//Welcome message specifying amount of guesses
System.out.println("nn*~*~*~* Welcome to the word guessing game! *~*~*~
n");
System.out.println("*~*~* You have a maximum of "+guesses+" guesses!
*~*~*~*nn");
//open text file
try
{
reader = new BufferedReader(new FileReader(args[0]+".txt"));
}
catch (FileNotFoundException fnfe)
{
System.out.println("Error opening file '" + args[0] + "'. " +"Please omit .
txt extension.");
System.exit(2);
}
boolean done = false;
String inputLine = null;
//read lines until end of file is reached
while (!done)
{
try
{
inputLine = reader.readLine();
}
catch(IOException ioe)
{
System.out.println("I/O error");
System.exit(3);
}
//end of file?
if (inputLine==null)
{
done=true;
}
else
{
// Put all the "words" in a string into an array.
wordArray=inputLine.split(" ");
}
}
//close file
try
{
reader.close();
}
catch(IOException ioe)
{
System.out.println("Error closing file '" + args[0] + "'");
System.exit(4);
}
//select a word randomly from word array
Random generator = new Random();
String secretWord= wordArray[generator.nextInt(wordArray.length)];
System.out.println(secretWord);
String output="";
char[] chars = secretWord.toCharArray();
//print out array of asterisks of length of word
for (int i=0;i
{
output += "*";
}
System.out.print(output);
char[] asterisksArray = output.toCharArray();
int i=0,j=0;
try
{
while (guesses!=0)
{
System.out.println("nPlease guess a letter:n");
//Read a character from keyboard
i = System.in.read();
// 1 byte character is returned in int so cast to char
ch = (char) i;
for (int k=0; k < chars.length; k++)
{
if (ch == chars[k] )
{
asterisksArray[k] = ch;
}
}
System.out.print(asterisksArray);
guesses--;
}
}
catch (IOException ioe)
{
System.out.println( "IO error:" + ioe );
System.exit(5);
}
Any help is much appreciated! This is a game I plan to show to my 8 year old
cousin to play.
thanks in advance!
}
}
|
|
| Back to top |
|
 |
crazyguitarchik via JavaK Guest
|
Posted: Sat Nov 26, 2005 4:25 pm Post subject: Re: System.in.read() repeats its output at least twice!? |
|
|
Hi,
thanks for your reply. I did what you said, and you're right, everytime I
press a letter and Enter, it reads 3 different values!
Why is this? Anybody know?
Thanks.
--
Message posted via http://www.javakb.com
|
|
| Back to top |
|
 |
Roedy Green Guest
|
Posted: Sat Nov 26, 2005 5:42 pm Post subject: Re: System.in.read() repeats its output at least twice!? |
|
|
On Sat, 26 Nov 2005 16:25:33 GMT, "crazyguitarchik via JavaKB.com"
<u16095@uwe> wrote, quoted or indirectly quoted someone who said :
| Quote: | thanks for your reply. I did what you said, and you're right, everytime I
press a letter and Enter, it reads 3 different values!
Why is this? Anybody know?
|
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
|
|
| Back to top |
|
 |
Roedy Green Guest
|
Posted: Sat Nov 26, 2005 5:43 pm Post subject: Re: System.in.read() repeats its output at least twice!? |
|
|
On Sat, 26 Nov 2005 16:25:33 GMT, "crazyguitarchik via JavaKB.com"
<u16095@uwe> wrote, quoted or indirectly quoted someone who said :
| Quote: | thanks for your reply. I did what you said, and you're right, everytime I
press a letter and Enter, it reads 3 different values!
Why is this? Anybody know?
inputLine = reader.readLine(); |
This would read a line at a time, and strip off the n or rn on the
end.
System.out.println( inputLine );
to see what you got. Perhaps you have some lead/trail space you did
not consider, or perhaps you put more than one letter per line.
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|