• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Reading characters

 
kunagu varun
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How to read a single character using showInputDialog of JOptionPane???

Please help guys...
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
kunagu varun wrote:How to read a single character using showInputDialog of JOptionPane???


Dialog Tutorial
 
kunagu varun
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The tutorial was extremely useful. Thanks a lot

But what i need actually is how to read only a single character from showInputDialog and store it in a variable of type char.
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
kunagu varun wrote:The tutorial was extremely useful. Thanks a lot

But what i need actually is how to read only a single character from showInputDialog and store it in a variable of type char.


You can get only Strings from a showInputDialog. You can then extract the first char of the String (after testing that it's not null and that its length is > 0) just as you would for any String charAt(0).

If this doesn't help and you're still running into problems, please show your code.
 
kunagu varun
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok thanks for the information provided. I will definitely try it out and inform you...
 
kunagu varun
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank You very much pete stein...

This is the code that i have used after you have posted. It works 101% successfully

 
Rob Spoor
Sheriff
Pie
Posts: 20661
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
kunagu varun wrote:It works 101% successfully

No it doesn't. Try either of the following and you'll see what I mean:
- press Cancel instead of OK
- press Enter without entering any text

That's why Pete said this:
pete stein wrote:(after testing that it's not null and that its length is > 0)
 
kunagu varun
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ya right i just want to know whether it is possible to read characters form an input box...
 
Rob Spoor
Sheriff
Pie
Posts: 20661
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your code is 100% correct - if the user enters what you expect him to enter. With just one or two simple checks your code will be perfect:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic