Very simple Java Question

Pulling Characters from JTextFields

Page 1 of 1

2 Replies - 630 Views - Last Post: 23 April 2008 - 04:45 PM Rate Topic: -----

#1 pbard  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 22-April 08

Very simple Java Question

Posted 22 April 2008 - 05:03 PM

I just have a very simple question. I am teaching myself Java by reading a book. When I do the practice questions I sometime go a little beyond what they ask me to do, and if I need help I can usually find the answer with a quick google search. However I cannot find the answer to this question. Here is my question:

I have a JTextField that the users enter a character in. How do I grab that character and assign it to char variable.

char letter = ?????

Thank you very much for the help.

Is This A Good Question/Topic? 0
  • +

Replies To: Very simple Java Question

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Very simple Java Question

Posted 22 April 2008 - 05:06 PM

View Postpbard, on 22 Apr, 2008 - 05:03 PM, said:

I just have a very simple question. I am teaching myself Java by reading a book. When I do the practice questions I sometime go a little beyond what they ask me to do, and if I need help I can usually find the answer with a quick google search. However I cannot find the answer to this question. Here is my question:

I have a JTextField that the users enter a character in. How do I grab that character and assign it to char variable.

char letter = ?????

Thank you very much for the help.


// get the String enterered in the JTextField
String str = textField.getText();
// convert this String to an array of char (will have a length of 1 if only 1 character was enterered)
char[] digit = str.toCharArray();
Was This Post Helpful? 0
  • +
  • -

#3 pbard  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 22-April 08

Re: Very simple Java Question

Posted 23 April 2008 - 04:45 PM

Thanks for you help. I also found you could do this:

stringCharacter = character.getText();
char chosenCharacter = stringCharacter.charAt(0);

Both will work.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1