# Command line input

• 04-18-2000, 04:33 PM
Basu
Command line input

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.

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

Hope this helps.

Tom Duffy

import java.io.*;
import java.util.*;

static public void main(String args[]) {
System.out.println("This program computes the roots of quadratic
equations of the form Ax^2 + Bx + C");
System.out.print("Please enter a value for A > ");
System.out.print("Please enter a value for B > ");
System.out.print("Please enter a value for C > ");
//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;

{ int i = 0;
try
StringTokenizer stok = new StringTokenizer (str);
i = new Integer (stok.nextToken ()).intValue ();
}
catch (IOException e)
{ System.out.println(e); }
return i;
}

{ double d = 0;
try
stok = new StringTokenizer (str);
d = new Double (stok.nextToken ()).doubleValue ();
}
catch (IOException e) { System.out.println(e); }
return d;
}

{
String str = "";
try
stok = new StringTokenizer (str);
}
catch (IOException e) { System.out.println(e); }
return str;

}
}

"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.
>