java newbie - basic io question

T

Thomas

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

Manish Pandit

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.

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
 
R

Roedy Green

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.

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

Hal Rosser

Thomas said:
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.
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

Thomas said:
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());

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
 
A

Andrew Thompson

Thomas wrote:
...
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.

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

Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.aspx/java-general/200707/1
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top