EDIT: Now I need Forte help (was Java Keyboard Input help)

Freeze

Senior member
Oct 15, 1999
681
0
0
Hey guys (and gals).

This is a really stupid question. For the life of me, I can't get keyboard input to work for my Java program, which really is a drag when you are trying to create a menu system.

I create a BuffereReader:

BufferedReader kbd = new BufferedReader(new InputStreamReader(System.in));

I then go try an read a char from the keyboard:

try {i = kbd.read();}
catch (IOException ioe)
{
System.out.println("Can't open Input Stream for Read");
System.out.println("IOError:" + ioe);
break;
}

input = (char) i;

System.out.println(input);

I want to use the input as a character in my program.

My IDE shows that the program is hanging at the try statement...

Thanks for any input (heh heh bad pun :)).


edit: updating title, see last post ~10:55 EST thanks
 

m0ti

Senior member
Jul 6, 2001
975
0
0
you're problem may be the fact that you're trying to read from the keyboard using a buffered reader. What happens with buffered readers is that they fill up their buffer before transferring any information. Since you're only reading a single character, the buffer isn't full and your program hangs (since it's waiting for data from the buffer. try pressing a lot of keys when you run it and the program should continue). BufferedReaders are generally used in cases where it would be very inefficienct to read the data a single character at a time, due to overhead costs (e.g. when reading from a file, one reads the entire block even if one only wants a single byte of it. not using a buffered reader would result in the same block being read as many times as there are bytes in it, instead of being read just a single time, for a large enough buffer). If you're going to be using kbd for keyboard input just use the standard InputStreamReader.
 

Freeze

Senior member
Oct 15, 1999
681
0
0
Thanks for that good info.

It still doesn't seems that the debugger didn't like it. If I run it without the debugger on, it worked fine. hmmm.

Thanks again.
 

Freeze

Senior member
Oct 15, 1999
681
0
0
How do I keep from processing the '\n\r' from the enter key? All I want is the enter key to indicate that something has been entered.
 

manly

Lifer
Jan 25, 2000
13,083
3,849
136
read() works the way you just stated (it returns the character pressed; the subsequent Enter keypress is ignored).

If your menu system requires trapping every discrete keypress (without a subsequent Enter keypress), then I'm not sure what the answer is.
 

m0ti

Senior member
Jul 6, 2001
975
0
0
if you want to use the enter key to signal that data has been entered, then use readLine() instead of read().
 

Freeze

Senior member
Oct 15, 1999
681
0
0
My problem is that the menu prints itself out 3 times, once for the key stroke entered and then for the linefeed and newline characters. I want to eliminate the second 2 prints...
 

m0ti

Senior member
Jul 6, 2001
975
0
0
if I understand this correctly, you don't want the enter to be printed out, or is it that you're reading in the newline and linefeed chracters as well. Did any different behaviour happen for readLine()? What you could also try is (I assume you're using a switch-case for deciding what to do in your menu) is set a couple of options for these keys, so that it doesn't do anything. You may have a problem with the way your code is structured in terms of consistency (i.e. your states aren't what you think they should be at each stage. for example, before you ask for more input you expect the input stream to be empty, which it isn't). you might try adding in a couple of asserts (in Java 1.4), or do a couple of tests to see exactly what the problem is.
 

Freeze

Senior member
Oct 15, 1999
681
0
0
I decided to switch IDEs, since I couldn't properly debug my program (it would hang on the readLine or read functions). I was using codewarrior since I had it. I DL'ed Forte from Sun and it seems to work pretty good. I am getting the proper response from the data input. This leads me to believe that it's a DOS problem, interpreting the extra charaters from the enter key.

Anyway, in my program, I create a file when the program starts and it is attepmting to create it in the forte/bin directory instead of the source directory. Any ideas why this is happening? I can't seem to find it in the properties menu and help really isn't assisting me that much. I'm going to check the Forte page tomorrow, but I though I'd throw it out here and see what happens.

Thanks for all the help.
 

m0ti

Senior member
Jul 6, 2001
975
0
0
it's probably because your starting directory is forte/bin when you open up forte, and if you don't change it, it defaults to the current directory. if your on windows this usually is a "starts in directory" or "working directory" option, under the properties of the program/shortcut.
 

manly

Lifer
Jan 25, 2000
13,083
3,849
136
That's because in specifying the filename (i.e. as an argument to the FileWriter constructor), you're not specifying a fully-qualified path.

So the file is written in the current working directory (which happens to be forte/bin) as m0ti said.

If you really *must* write to the same path as the .class file, there is a way to map a running class to it's filesystem location, but I can't recall.

The correct thing is to just decide on a place to write the file, and fully specify it in your code.

Btw, you need a pretty beefy machine to use Forte. If you're willing to use a less flashy tool, I'd recommend Emacs + JDEE. You'll love the BeanShell, which comes w/ JDEE. The BeanShell is an interpreted scripting environment for Java. It's sweet for testing.
 

Freeze

Senior member
Oct 15, 1999
681
0
0
Thanks for all the replies. You guys have been a great help.

So how beefy is beefy for Forte? :) It seems to run OK right now, but it does take a while to load...
 

manly

Lifer
Jan 25, 2000
13,083
3,849
136
If it's fast enough for your use, that's all that matters. In general, programmers want good performance from their IDE, because it directly influences their productivity.

Personally (although I haven't used it much), I wouldn't use Forte on a machine less than 500 MHz w/ 256 MB RAM. You can optimize Forte's load time and performance by excluding plugins you don't need.

I have used a similarly beefy IDE called Together ControlCenter, and if I recall correctly, the SPARC system recommendation is a 400 MHz UltraSPARC III w/ 512 MB RAM. Java on x86 Windows doesn't require as much RAM as on Solaris.