• Hey, guest user. Hope you're enjoying NeoGAF! Have you considered registering for an account? Come join us and add your take to the daily discourse.

Java Problem...

Status
Not open for further replies.

GaimeGuy

Volunteer Deputy Campaign Director, Obama for America '16
ok, I'm making a number guessing game in the form of an applet.

The only problem is, I have no idea how to read strings from the keyboard using the AWT. :lol

I think I know how I'm going to do everything else, but I have no clue how to get input from the keyboard.
 

CaptainABAB

Member
I assume you are not using text fields and that this is not a command line type of app.

Here is an example of some code that discards non-alphabetic keyboard presses - you should be able to modify it to suit your needs...


public class OnlyAlpha implements KeyListener {
public void keyPressed(KeyEvent e) {
if (! Character.isLetter(e.getKeyChar())) {
Toolkit.getDefaultToolkit().beep();
e.consume();
}
}
public void keyReleased(KeyEvent e) {
// empty
}
public void keyTyped(KeyEvent e) {
// empty
}
}
 

GaimeGuy

Volunteer Deputy Campaign Director, Obama for America '16
CaptainABAB said:
I assume you are not using text fields and that this is not a command line type of app.

Here is an example of some code that discards non-alphabetic keyboard presses - you should be able to modify it to suit your needs...


public class OnlyAlpha implements KeyListener {
public void keyPressed(KeyEvent e) {
if (! Character.isLetter(e.getKeyChar())) {
Toolkit.getDefaultToolkit().beep();
e.consume();
}
}
public void keyReleased(KeyEvent e) {
// empty
}
public void keyTyped(KeyEvent e) {
// empty
}
}
THANK you!
 

CaptainABAB

Member
--- KeyEvent and KeyListener ---

KeyEvent fired whenever a key is pressed

when a KeyEvent is fired, either of the following is called:
KeyListener.keyPressed() is called when a key is pressed
KeyListener.keyReleased() is called when a key is released
KeyListener.keyTyped() is called when a key is pressed and released
use addKeyListener() to add listeners to text components
use KeyEvent.getKeyCode() and KeyEvent.getKeyChar() to determine what key was pressed

if your listener does not need to process all three kinds of KeyEvents, then extend KeyAdapter

Example:

// Here is a class that implements the KeyListener interface
// (by extending the KeyAdapter class).
class MyKeyListener extends KeyAdapter
{
public void keyPressed (KeyEvent event)
{
if (event.getKeyCode() == KeyEvent.VK_ENTER)
System.out.println("Enter was pressed");
}
}

// Here is a code snippet that creates a text field and
// processes the enter key (assume this is inside a main, etc.)
// Create a text field.
TextField tf = new TextField(30);
tf.setText("");
// Add our custom key listener to the text field.
MyKeyListener customKeyListener = new MyKeyListener();
tf.addKeyListener (customKeyListener);
// Now, when ever the enter key is pressed in the text field,
// MyKeyListener.keyPressed() is called
 

Ghost

Chili Con Carnage!
You mean a program for designing java GUI's or a GUI for java design?

For the former i wouldnt bother (it's really easy to just code, especially if you use gridbaglayout) but netbeans is good, very flexible with what it lets you do.

For the latter people here recommended intelliJ IDEA to me, and its a superb project management tool, saved me hours and hours on my final year project.
 

iapetus

Scary Euro Man
Ghost said:
For the former i wouldnt bother (it's really easy to just code, especially if you use gridbaglayout)

Reminds me of the Java GUI courses I used to teach. "Sometimes the simpler layout managers won't be able to cope with your UI design, and you'll have to use GridBagLayout. If this happens, redesign your UI."
 

GaimeGuy

Volunteer Deputy Campaign Director, Obama for America '16
Is there a way to set up a string reader of some sort in an applet so that I can take the string, check to see if it's an integer between 0 and 100, perform a series of operations upon the number, and then prompting the user to guess the number, giving him or her hints that his or her guesses are too high or too low along the way?



I like how "Java 2: A beginner's guide" has next to nothing on applets. It doesn't even cover text input, just the MouseEvents, for the most part.

It feels like everything in the first 475 pages of the book relating to console input and output doesn't mean jack shit in the last 30 pages involving applets. >_>
 

Pochacco

asking dangerous questions
GaimeGuy said:
Is there a way to set up a string reader of some sort in an applet so that I can take the string, check to see if it's an integer between 0 and 100, perform a series of operations upon the number, and then prompting the user to guess the number, giving him or her hints that his or her guesses are too high or too low along the way?



I like how "Java 2: A beginner's guide" has next to nothing on applets. It doesn't even cover text input, just the MouseEvents, for the most part.

It feels like everything in the first 475 pages of the book relating to console input and output doesn't mean jack shit in the last 30 pages involving applets. >_>
After you read in the string, convert it to an int.
Check if it's between 0 and 100 (if ((input >= 0) && (input <= 100)).
Perform ops on the number.
Prompt the user (by printing to stdout) to guess the number.
Read in this number like the first number.
Convert it to an int.
Compare it to the first number, and tell the user whether (s)he was right, was over, or under...
 

GaimeGuy

Volunteer Deputy Campaign Director, Obama for America '16
And I have NO idea how to read string input through the AWT. Keylisteners read only one key. I can't use System.in.read() to read data, because that's an input stream, a part of the io package, not the awt. Same with any other reader/writer....
Hell, I don't even know of any way to output text in an applet other than using drawString in the paint method (and being limited to output in only the paint method is, well, limiting.)
 

CrunchyB

Member
Can't you just do something like this:

public void keyTyped(KeyEvent e) {
if(e == KeyEvent.VK_ENTER) {newInt=Integer.parseInt(intString); intString='';}
else{ intString += e.getKeyChar();}
}

Append the character to the String, convert the String to an integer (and clear it again) when a return is pressed.
 

GaimeGuy

Volunteer Deputy Campaign Director, Obama for America '16
CrunchyB said:
Can't you just do something like this:

public void keyTyped(KeyEvent e) {
if(e == KeyEvent.VK_ENTER) {newInt=Integer.parseInt(intString); intString='';}
else{ intString += e.getKeyChar();}
}

Append the character to the String, convert the String to an integer (and clear it again) when a return is pressed.

I guess. I just finished reading Java 2: A beginner's guide, so I didn't cover keyevents or intStrings.
 
Status
Not open for further replies.
Top Bottom