Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

TextIO class - Matter copied from unrelated thread  RSS feed

 
Laurie carrera
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Laurie carrera
greenhorn
Member # 126433
posted June 13, 2006 08:02 AM
--------------------------------------------------------------------------------
Thanks for the reply. That cleared up a lot of things. I have one more question.
I have found that a lot of java examples use TextIO class, and I have noticed that is the only I/O class that reads char input (TextIO.getchar()).
But this class is no where to be found in the API. I am using ver 1.5 of the JDK and 5.0 netbeans. Is there something I am missing? Also is there any other I/O function that reads char input?

Sorry thats two questions.
--------------------------------------------------------------------------------
Posts: 7 | Registered: Jun 2006 | IP: Logged

Campbell Ritchie
ranch hand
Member # 109202
posted June 13, 2006 08:43 AM
--------------------------------------------------------------------------------
I think we are getting off the track here and you really ought to start another thread, but . . .


You are right, there is no such thing as the TextIO class in the API. It must be a leftover from the good old days of J1.4 (or 1.3 or 1.2 or 1.1 or 1.0). In those days everybody who had been programming for more than a few months tended to write their own classes for keyboard-based input, and once they had them running OK, tended to reuse them. You end up with something like this:-
code:
--------------------------------------------------------------------------------

import java.io.BufferedReader;import java.io.InputStreamReader;import java.io.IOException; public class KeyboardIO{ private BufferedReader inRead; public KeyboardIO() { inRead = new BufferedReader(new InputStreamReader(System.in)); }// end constructor /** * This method reads a line of input as a String and returns the * first char of the input * @return The first char of the line input */ public char readChar() { boolean readRight = false; char resultChar; while(!readRight) { try { String input = inRead.readLine(); resultChar = input.charAt(0); readRight = true; }// end try catch(IOException e) { System.err.println("Unable to read: an error has occurred. Trying again."); }// end catch }// end while ! read right return resultChar; }// end read char method}

--------------------------------------------------------------------------------


Alternative readChar method:-
code:
--------------------------------------------------------------------------------

/** * This method reads a single char from the keyboard * @return The char input */ public char readChar() { boolean readRight = false; char resultChar; while(!readRight) { try { resultChar = inRead.read(); readRight = true; }// end try catch(IOException e) { System.err.println("Unable to read: an error has occurred. Trying again."); }// end catch }// end while ! read right return resultChar; }// end read char method

--------------------------------------------------------------------------------


As I said, everybody wrote their own, and TextIO is obviously what somebody wrote. For most things you can use the java.util.Scanner class, but it doesn't have the readChar method. You probably now have enough information to write your own keyboard input method now.

Probably best to copy this posting to a new thread.

CR
 
Campbell Ritchie
Marshal
Posts: 55678
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you.

And should you feel brave or foolhardy enough to write your own keyboard input class, you should be able to work out how to get a line or a number out of it.

Anybody wanting a more legible version of what I wrote, try the last-but-one post on this thread.
[ June 13, 2006: Message edited by: Campbell Ritchie ]
 
Gabriel White
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Again, why wouldn't you use the scanner class?
and assign it to a string or a char?

char answer = 'Y';
Scanner input = new Scanner(System.in);
answer = input.nextLine().charAt(0);

is this what you are looking for?
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!