Hi,
I would like to know how can i accept user input in the middle of the execution.
I know using java prgnam paramete we can get user input but in middle of
the program I need a way to get a value from the user.
Please help me how to do this.
Thanks
Basu
04-19-2000, 12:13 AM
Tom Duffy
Re: Command line input
Hello Basu:
Here's an example that gathers input for a Quadratic equation and then solves
it. It really only uses the readDouble() method of the Input class and I've
included readInt() and readString().
Hope this helps.
Tom Duffy
import java.io.*;
import java.util.*;
public class Quadratic {
static public void main(String args[]) {
System.out.println("This program computes the roots of quadratic
equations of the form Ax^2 + Bx + C");
//Read in coefficients
System.out.print("Please enter a value for A > ");
double a = Input.readDouble();
System.out.print("Please enter a value for B > ");
double b = Input.readDouble();
System.out.print("Please enter a value for C > ");
double c = Input.readDouble();
//Calculate discriminant
double discriminant = (Math.pow(b, 2))-(4*a*c);
if(discriminant<0)
System.out.println("Equation has no real roots.");
else{
//Find real roots
double x1 = (-1*b+(Math.sqrt(discriminant)))/(2*a);
double x2 = (-1*b-(Math.sqrt(discriminant)))/(2*a);
//If x1=x2 the there is only one root
if(x1==x2)
System.out.println("The root of the equation is " + x1);
else
System.out.println("The roots of the equation are " +
x1 + " and " + x2);
}
System.out.println();
}
}
class Input{
static private StringTokenizer stok;
static private BufferedReader br
= new BufferedReader(new InputStreamReader(System.in));
public static int readInt()
{ int i = 0;
try
{ String str = br.readLine();
StringTokenizer stok = new StringTokenizer (str);
i = new Integer (stok.nextToken ()).intValue ();
}
catch (IOException e)
{ System.out.println(e); }
return i;
}
public static double readDouble()
{ double d = 0;
try
{ String str = br.readLine();
stok = new StringTokenizer (str);
d = new Double (stok.nextToken ()).doubleValue ();
}
catch (IOException e) { System.out.println(e); }
return d;
}
"Basu" <basusm@hotmail.com> wrote:
>
>Hi,
>I would like to know how can i accept user input in the middle of the execution.
>I know using java prgnam paramete we can get user input but in middle of
>the program I need a way to get a value from the user.
>
>Please help me how to do this.
>
>Thanks
>Basu
>
>