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