Java BufferReader/IO Exception?? need just a little help :)

ChrisCramer247

Platinum Member
Dec 6, 2001
2,171
1
0
Here's my coding:

public class UsingForMod {
//Written by Chris Cramer
//Assignment 4 Part IIa
//This is a program that prints multiples of a particular number
//with input from the keyboard

public static void main(String arg[]){
int baseNumber=getNumber("base number");
int multiplier=getNumber("multiplier");
doMultiplesOf(baseNumber,multiplier);

}//end main

public static void doMultiplesOf(int n, int m) {
for (int x=0; x<=m; x++)
{
display("The number "+n+" multiplied by "+x+" is "+x*n);
}//close for
}//close doMultiplesOf

public static int getNumber(String n){
int number=0;
display("Please enter the "+n);
try {
String integerString=keyboard.readLine();
number=Integer.parseInt(integerString);
}//close try
catch(Exception e){
display("You need to enter an integer");
exitProgram();
}//close catch
return number;
}//close getNumber
public static void exitProgram(){
display("Exiting the program");
System.exit(0);
}

public static void display(String n){
System.out.println(n);
}//close display
}//close UsingFor


I get an error saying that this:
> javac UsingForMod.java
UsingForMod.java:25: Undefined variable or class name: keyboard
String integerString=keyboard.readLine();

Do i have to add a new BufferReader in there to take the term to make it a defined varaible or a throws? what do i need to do? Thanks everyone! :D

-Chris
 

m0ti

Senior member
Jul 6, 2001
975
0
0
First clue: keyboard starts with a lowercase letter, therefore it's not a class.

What you want is System.in.readLine().
 

alisajid

Member
Jun 29, 2001
194
0
0
1. There's no keyboard object that I'm aware of in the Java API.

2. System.in is an InputStream instance, i.e. it is a plain byte stream. I don't think it has a readLine method.

If all you need is character i/o then use an InputStreamReader to wrap System.in, and then use a BufferedReader to wrap the InputStreamReader.

BufferedReader bufferedKeyboard = new BufferedReader( new InputStreamReader( System.in ) );

From there you can execute:

try {
String line = bufferedKeyboard.readLine();
} catch (IOException ioex) {
}