Re: Basic Java Program Help
Do you have to read in the entire formula? Or can you ask the user for separate values? The latter is going to be much much easier.
Code :
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.println("Enter a: ");
double a = scan.nextDouble();
System.out.println("Enter b: ");
double b = scan.nextDouble();
System.out.println(Enter c: ");
double c = scan.nextDouble();
// compute quadratic ...
}
If you must extract all the numbers from a single string, then you can still use the Scanner so long as they are separated somehow (usually spaces), and there is nothing else in the string.
Code :
String values = "1.0 2.3 4.5"; // a = 1, b = 2.3, c = 4.5
Scanner reader = new Scanner(values);
double a = values.nextDouble();
double b = values.nextDouble();
double c = values.nextDouble();
However, if the given string is like this:
"1.0X^2 + 3.5X + 2 = 0"
, you'll have an extremely hard time extracting the numbers you want. It's not impossible, but I doubt that's what your teacher wants from you. If you really want to know, try doing a google search on regular expressions.