Re: java newbie - basic io question
Thomas wrote:
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