Hello I 'm new to java and I want just to ask about the shortest possible
way to read user input from console. I found this :
char tmp;
Integer liczba = new Integer (0);
StringBuffer buffer;
buffer=new StringBuffer();
while((tmp = (char)System.in.read())!='\n')
buffer=buffer.append(tmp);
liczba=Integer.parseInt(buffer.toString().trim());
Since in c++ it does take significantly less code to implement.
Manish Pandit - 03 Jul 2007 19:14 GMT
> Hello I 'm new to java and I want just to ask about the shortest possible
> way to read user input from console. I found this :
[quoted text clipped - 9 lines]
>
> Since in c++ it does take significantly less code to implement.
A shorter (in terms of lines of code) version would be :
BufferedReader reader = new BufferedReader(new
InputStreamReader(System.in));
String string = reader.readLine();
In the above fragment, 'string' has the keyboard input, which you can
trim/chop/parseInt..etc.
-cheers,
Manish
Roedy Green - 04 Jul 2007 01:46 GMT
> char tmp;
> Integer liczba = new Integer (0);
[quoted text clipped - 6 lines]
>
>Since in c++ it does take significantly less code to implement.
ssee http://mindprod.com/applets/fileio.html
and get it to generate your some code readLine will get you a whole
line at a time. You can trim that. It effectively handles the
StringBuilder stuff for you.
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
Hal Rosser - 04 Jul 2007 01:48 GMT
> Hello I 'm new to java and I want just to ask about the shortest possible
> way to read user input from console.
Look in he API at the new Scanner class.
Arne Vajhøj - 04 Jul 2007 02:30 GMT
> Hello I 'm new to java and I want just to ask about the shortest possible
> way to read user input from console. I found this :
[quoted text clipped - 7 lines]
> buffer=buffer.append(tmp);
> liczba=Integer.parseInt(buffer.toString().trim());
Almost any Java version:
import java.io.*;
public class S14 {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
System.out.print("Enter integer: ");
int iv = Integer.parseInt(br.readLine());
System.out.println(iv);
System.out.print("Enter decimal: ");
double xv = Double.parseDouble(br.readLine());
System.out.println(xv);
System.out.print("Enter string: ");
String sv = br.readLine();
System.out.println(sv);
}
}
Java 1.5 and newer:
import java.io.*;
import java.util.*;
public class S15 {
public static void main(String[] args) throws Exception {
Scanner scn = new Scanner(System.in);
System.out.print("Enter integer: ");
int iv = scn.nextInt();
System.out.println(iv);
System.out.print("Enter decimal: ");
double xv = scn.nextDouble();
System.out.println(xv);
System.out.print("Enter string: ");
String sv = scn.next();
System.out.println(sv);
}
}
Arne
Andrew Thompson - 04 Jul 2007 03:12 GMT
...
>Since in c++ it does take significantly less code to implement.
Boo hoo. How many lines of C++ does it take to write
a minimal HTML/web browser? In Java it takes less
than 60 lines of code for a self contained implementation.
And what does that mean? Java is well optimised
for developing GUI's, and HTML rendering. Java is
not well optimised for reading from the command line
(current thread examples notwithstanding).
What else does it mean? Approximately nothing.

Signature
Andrew Thompson
http://www.athompson.info/andrew/