(String) System.in.read(); ?

alright, how would i go about converting System.in.read(); to a string instead of just a charthis is what i got so far, why wont it work if it works with char?String name = (String) System.in.read();
[220 byte] By [ByronTheOmnipotenta] at [2007-11-27 8:40:17]
# 1

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

String line = br.readLine();

OR

Scanner sc = new Scanner(System.in);

sc.nextLine();

OR

sc.next();

OR

sc.nextInt();

Or something like that...

http://java.sun.com/docs/books/tutorial/essential/io/index.html

jverda at 2007-7-12 20:38:40 > top of Java-index,Java Essentials,New To Java...
# 2

Here is one more approach that I would take to solve the problem, the only thing is it required error handling, but it shows how you can successfully convert byte[] and char[] directly to strings

import java.io.*;

public class Prime {

public static void main(String args[]){

byte[] data = new byte[30];

try{

System.in.read(data);

}

catch(IOException e){

System.out.println("Could not get data from user!!!!");

}

String s = new String(data);

System.out.println(s);

}

}

the line String s = new String(data) I think is what you were curious in, this works with char arrays as well. If you don't like error handeling then I would go with the other solution.

noone392a at 2007-7-12 20:38:40 > top of Java-index,Java Essentials,New To Java...
# 3
> Here is one more approach that I would take to solve> the problem, Unless you really need to process a character at a time, there's no good reason to do that. Even then, it's probably better to read a line and split it into characters.
jverda at 2007-7-12 20:38:40 > top of Java-index,Java Essentials,New To Java...
# 4

> Here is one more approach that I would take to solve

> the problem, the only thing is it required error

> handling, but it shows how you can successfully

> convert byte[] and char[] directly to strings

>...

That's not the way: now you're always creating a String with 30 characters in it. If the OP really wants to read it like that, then s/he should not use the new String(byte[]) constructor, but the:

http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html#String(byte[],%20int,%20int)

But I suggest to follow the advice given by jverd.

prometheuzza at 2007-7-12 20:38:40 > top of Java-index,Java Essentials,New To Java...
# 5
alright thx for feedback everyone, i think i got it now
ByronTheOmnipotenta at 2007-7-12 20:38:40 > top of Java-index,Java Essentials,New To Java...