Harish Chandra
February 20th, 2000, 03:15 AM
I want to know how to receive console input in a program. You can use System.out.println for displaying something on the console. What is the corresponding way to receive an input?
As an alternative way, I tried writing a small class myself for this purpose, but I am getting some errors that I am unable to understand. Can somebody please help with the following code:
import java.io.*;
class IoServices {
public static int getInt () throws IOException {
InputStream in = System.in;
byte [] buffer = new byte [1024];
in.read (buffer);
String str = new String (buffer);
return Integer.parseInt (str);
}
}
class Testing {
public static void main (String [] args) throws IOException{
System.out.println ("Enter your age ");
int age = IoServices.getInt ();
System.out.println ("You entered " + age);
}
}
As an alternative way, I tried writing a small class myself for this purpose, but I am getting some errors that I am unable to understand. Can somebody please help with the following code:
import java.io.*;
class IoServices {
public static int getInt () throws IOException {
InputStream in = System.in;
byte [] buffer = new byte [1024];
in.read (buffer);
String str = new String (buffer);
return Integer.parseInt (str);
}
}
class Testing {
public static void main (String [] args) throws IOException{
System.out.println ("Enter your age ");
int age = IoServices.getInt ();
System.out.println ("You entered " + age);
}
}