Join 353,816 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 3,501 people online right now.Registration is fast and FREE... Join Now!
Hey guys,
I am working on a homework assignment and am just looking for some guidance or direction in where I'm going wrong. I am new to java and have been learning it for a couple weeks now. My assignment is to compute the roots of a quadratic using the quadratic formula. My program needs to read in three doubles with the values being separated by blanks in the test input. The roots need to be displayed to exactly 4 places past the decimal point, with a leading zero shown before the decimal point if it is between -1.0 and 1.0. No words are ever displayed because I turn it in electronically where the system compiles and tests my code automatically.
An example being:
If the values input are,
1.0 4.0 3.0
The output would be,
-1.0000
-3.0000
When I run my program and type in an input like whats above I get...
?
?
import java.util.Scanner;
import static java.lang.Math.*;
import static java.lang.System.*;
import java.text.DecimalFormat;
class ComputeRoots
{
public static void main (String [] args)
{
Scanner scan = new Scanner(in);
double a, b, c, discr, root1, root2;
a = scan.nextDouble();
b = scan.nextDouble();
c = scan.nextDouble();
discr = Math.sqrt ((b*b) - ( 4 * a * c));
root1 = ( -b + discr ) / 2 * a;
root2 = ( -b + discr ) / 2 * a;
DecimalFormat fmt1 = new DecimalFormat (" 0.####");
out.println(fmt1.format(root1));
out.println(fmt1.format(root2));
}
}
This is the semester of the Quadratic equation 4 posts in a week
That will make a change from the Inventory program and its DVD which extends Product
Now problem with Scanner and new line
a = scan.nextDouble();
b = scan.nextDouble();
c = scan.nextDouble();
this implies that the 3 numbers are typed on the same line (one single line like: 1.2 2.3 4.5)
Is is what you are doing ? Not easy to find as you do not prompt the user to enter values and which way to enter them ?
Also make sure 2 * a is not == 0.0 because you will divide by 0
yea he wants us to do this because the program I write is not turned into him physically. The code is uploaded into an online application that is fairly sensitive. The application will compile the code I write it will then test it and will give me an instant grade response based on whether or not the test passed. The application is expecting this program and is prepared for just those three blank spots without any words. The program will throw random numbers in the input and expects an exact response, overall the program is not meant to be user friendly.
This post has been edited by heathro03: 05 October 2009 - 05:10 PM
"a = scan.nextDouble();
b = scan.nextDouble();
c = scan.nextDouble();
this implies that the 3 numbers are typed on the same line (one single line like: 1.2 2.3 4.5)
Is is what you are doing ? Not easy to find as you do not prompt the user to enter values and which way to enter them ?"
I was trying to inform you on why it is like so. Maybe I misunderstood what you were getting at?
"a = scan.nextDouble();
b = scan.nextDouble();
c = scan.nextDouble();
this implies that the 3 numbers are typed on the same line (one single line like: 1.2 2.3 4.5)
Is is what you are doing ? Not easy to find as you do not prompt the user to enter values and which way to enter them ?"
I was trying to inform you on why it is like so. Maybe I misunderstood what you were getting at?
Do you enter the 3 numbers on a single line on the console without doing a <CR> between them ?
yep nothing between them, only a single space on a single line. I don't type in any numbers, all I do is type the code and upload it to the grading application where it runs all the numbers it wants to make sure my coding is correct.
Once I upload the code it will run numbers in only that specific format, any other way or extra code and it will not come out properly.
example,
Input:
1.0 2.0 1.0
Output:
-1.0000
-1.0000
That is exactly how the online grading application expects the input for the program to work, as well it expects that type of output.
I am in the US (Ohio, not sure if it makes a difference). I am not aware of the differences between the two and why such would happen. Thanks for all your continuous help...
I am in the US (Ohio, not sure if it makes a difference). I am not aware of the differences between the two and why such would happen. Thanks for all your continuous help...
US is the last country in the world where the decimal point is a "." rather than "," actually we should call it a decimal coma but in US
So you are in US with a US PC build by Dell or HP or Compaq and you download a US Java from a US site and your Regional Settings in Windows are US ? Or you use Linux and you install the US version ?
Just for fun... try to input your numbers as 1,2 3,4 and 5,6
wow, I had no idea. That's real interesting.. I have an HP with Windows Vista with Regional settings of US. I am using BlueJ as my compiler. I do not use Linux.
wow, I had no idea. That's real interesting.. I have an HP with Windows Vista with Regional settings of US. I am using BlueJ as my compiler. I do not use Linux.
Did you tried what I have suggested in previous post
yes I did, it was unsuccessful. My compiler (BlueJ) is not working on my computer for some reason so I have been compiling with my command prompt if that makes any difference to you. This time it did not give me the two ?s like it does when I normally run it with the decimal points. This time it gave me an error saying, exception in thread "main" java.util.InputMismatchException. Being as I am fairly new to everything I am not exactly sure what this error message means.
yes I did, it was unsuccessful. My compiler (BlueJ) is not working on my computer for some reason so I have been compiling with my command prompt if that makes any difference to you. This time it did not give me the two ?s like it does when I normally run it with the decimal points. This time it gave me an error saying, exception in thread "main" java.util.InputMismatchException. Being as I am fairly new to everything I am not exactly sure what this error message means.
Let us not mix apples and oranges here
You are usiong BlueJ as Editor (not compiler)
your compiler is the standard Java compiler provided by Sun
the InputMismatchException telles us (when use with , instead of .) that your settings for US are OK
Now, sorry but I am kind of lost
Hope some Dic from the US can advise... works well on my side with French Canadian settings