> Well, lets just say they weren't working well and
> they were all pretty much not satisfying enough.
>
> I just want a "simple" code where the input stream is
> used.
I'd suspect anything anyone writes here would be pretty much a duplicate of what you've already alledgedly encountered, so I wouldn't waste my time rewriting what you've already seen, only to be told that it, just like the others, doesn't satisfy you either.
I can't understand you. You said:
1. I just want a "simple" code where the input stream is used.
2. I have searched and found different solutions, but none of them work like they should.
So you not only want "code where the input stream is used", you have some requirements and existing code did not fit into.
Again - what do you want, what did you find, what was wrong?
No need to get hatefull, or?
The reason why I dint post the codes I've been using is because I dont understand anything of them.
Anyway, heres one of them that didnt work at all:
import java.io.*
byte [] b=new byte[255];
System.in.read(b);
And:
InputStreamReader isr = new InputStreamReader( System.in );
BufferedReader stdin = new BufferedReader( isr );
System.out.print( "Test " );
String input = stdin.readLine();
System.out.println( "input = " + input );
I also had one that I deleted wich contained the keyword "try" which worked pretty good but the code was on a level that I didnt understand at all.
All of theese code examples are just found with google and tried with no positive results.
Not trying to be mean, just want to see some specific questions!
InputStreamReader isr = new InputStreamReader( System.in );
BufferedReader stdin = new BufferedReader( isr );
System.out.print( "Test " );
String input = stdin.readLine();
System.out.println( "input = " + input );
That can work. Why is it not acceptable to you?
I will go with that one if I cant get this one to work:
public static void feedIn() {
byte [] b=new byte[255];
System.in.read(b);
}
And the error message returned from the compiler is:
Test.java:90: unreported exception java.io.IOException; must be caught or declared to be thrown
System.in.read(b);
^
1 error
Help!
Setting IOException aside for a minute, there's a fundamental difference
between bytes and text. You want to read text, so that means you need to
use a Reader like BufferedReader. It is just plain, logically wrong to work
with bytes when text reading is what is required.
As for exception handing goes, every Java programmer needs to
understand exceptions. Take the tutorial: http://java.sun.com/docs/books/tutorial/essential/exceptions/index.html
If using Java 5, you can just use a scanner:
class ReadText {
public static void main(String[] argv) {
System.out.println(new java.util.Scanner(System.in).nextLine());
}
}
No need for streams.
it did the trick, thanks!
Just a quick question about randomizing: I want to generate 1 of 3 names (string) and then print it out. My current code looks something like this:
public static void randomizename() {
for (int i = 0; i < 2; i++)
String[] mankind = {"Alien", "Green"}; //Here is the first problem, I cant print 'mankind' out with the correct and randomized name.
System.out.print("What kind are you?: ");
System.out.print ("So you really are a " + mankind); //Something like this for output.
}