Simple input stream

I need some serious help with this. I want to be able to write a string into my program and then make it print it out what I just wrote in.I have searched and found different solutions, but none of them work like they should.
[239 byte] By [Lowtecha] at [2007-11-26 22:42:16]
# 1
What are these solutions?What's wrong with them?
Michael.Nazarov@sun.coma at 2007-7-10 11:57:40 > top of Java-index,Java Essentials,New To Java...
# 2
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.
Lowtecha at 2007-7-10 11:57:40 > top of Java-index,Java Essentials,New To Java...
# 3

> 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.

warnerjaa at 2007-7-10 11:57:40 > top of Java-index,Java Essentials,New To Java...
# 4

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?

Michael.Nazarov@sun.coma at 2007-7-10 11:57:40 > top of Java-index,Java Essentials,New To Java...
# 5
is anyone falling for this? Someone saying they've tried "different solutions",but not posting a line of code, then expecting someone to write a solutionand post it for them... Come on, Lowtech, do you own homework!
DrLaszloJamfa at 2007-7-10 11:57:40 > top of Java-index,Java Essentials,New To Java...
# 6

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.

Lowtecha at 2007-7-10 11:57:40 > top of Java-index,Java Essentials,New To Java...
# 7

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?

DrLaszloJamfa at 2007-7-10 11:57:40 > top of Java-index,Java Essentials,New To Java...
# 8

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!

Lowtecha at 2007-7-10 11:57:40 > top of Java-index,Java Essentials,New To Java...
# 9

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

DrLaszloJamfa at 2007-7-10 11:57:40 > top of Java-index,Java Essentials,New To Java...
# 10
Thanks for the link, I think I got the fundamentals now. I just wish that the input-stream would be as easy as in C++.
Lowtecha at 2007-7-10 11:57:40 > top of Java-index,Java Essentials,New To Java...
# 11
> ...> I just wish that the input-stream would be as> easy as in C++.It is. You're just not used to it.
prometheuzza at 2007-7-10 11:57:40 > top of Java-index,Java Essentials,New To Java...
# 12
IMO it is considerably easier. The I/O in C++ is one of the worst designs ever perpetrated. But this is very OT.
ejpa at 2007-7-10 11:57:40 > top of Java-index,Java Essentials,New To Java...
# 13

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.

Navy_Codera at 2007-7-10 11:57:40 > top of Java-index,Java Essentials,New To Java...
# 14

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.

}

Lowtecha at 2007-7-10 11:57:41 > top of Java-index,Java Essentials,New To Java...