-
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.
Please help me how to do this.
Thanks
Basu
-
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;
}
public static String readString()
{
String str = "";
try
{ str = br.readLine();
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.
>
>Please help me how to do this.
>
>Thanks
>Basu
>
>
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks