buffer reader
hi guys.
have the following piece of code..
wanted to knwo if i can simplfy it ..
so that i dont have to keep repeating the code from every line of input
if( args.length < 1 )
{
InputStreamReader ins =new InputStreamReader( System.in );
BufferedReader stdin =new BufferedReader( ins );
System.out.print("Enter Number to be decrypted: ");
String numstr = stdin.readLine();
b =new BigInteger(numstr);
}
{
InputStreamReader ins =new InputStreamReader( System.in );
BufferedReader stdin =new BufferedReader( ins );
System.out.print("Enter oN: ");
String numstr = stdin.readLine();
oN =new BigInteger(numstr);
}
{
InputStreamReader ins =new InputStreamReader( System.in );
BufferedReader stdin =new BufferedReader( ins );
System.out.print("Enter N from the public key: ");
String numstr = stdin.readLine();
N =new BigInteger(numstr);
}
basically i want to input the value to decrypt followed by the value of oN and then N..
but as you can see from the above code i have to keep repeating the code!!
is there any way i can simplify it..
and also wanted to know how to print an error if the user doesnt input a Number..
thanks in advance
[1901 byte] By [
xtodamixa] at [2007-11-26 21:09:20]

Here's a little demo:import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigInteger;
public class InputDemo {
InputStreamReader ins = new InputStreamReader(System.in);
BufferedReader stdin = new BufferedReader(ins);
public BigInteger readBigInteger(String message) throws IOException {
System.out.print(message);
String numstr = stdin.readLine().trim();
return new BigInteger(numstr);
}
public static void main(String[] args) throws IOException {
InputDemo demo = new InputDemo();
BigInteger b = demo.readBigInteger("Enter Number to be decrypted: ");
BigInteger oN = demo.readBigInteger("Enter oN: ");
BigInteger N = demo.readBigInteger("Enter N from the public key: ");
}
}
hi thanks alot for your help..
just one more thing .. how to create an error if the user does not input a value..
say for example s/he input a string ..
right how it just opens opens up the editor window and high lights the fact that only a number can be inserted
i wanted to have to print a statement to say the value is not valid or sumthin like that..
if / else statement it doesnt go and print the statement
it just opens up the editor window..
> hi thanks alot for your help..
>
> just one more thing .. how to create an error if the
> user does not input a value..
> say for example s/he input a string ..
>
> right how it just opens opens up the editor window
> and high lights the fact that only a number can be
> inserted
> i wanted to have to print a statement to say the
> value is not valid or sumthin like that..
>
> if / else statement it doesnt go and print the
> statement
> it just opens up the editor window..
You could do something like this:public BigInteger readBigInteger(String message) throws IOException {
BigInteger bi = null;
while(true) {
try {
System.out.print(message);
String numstr = stdin.readLine();
bi = new BigInteger(numstr.trim());
break;
} catch(NumberFormatException nfe) {
System.out.println("Incorrect value! Try again.");
}
}
return bi;
}