ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I am not very familiar with Java in particular, but from my experience with C/C++, it could be that when you read A the newline character (\n) is left in the buffer and is automatically read in for B. Basically make sure to clean the buffer before reading next value.
thanks, that worked. By the way, does anyone know who to get the square root of a number in java? I went to sun.java.com and they had something saying
public static double sqrt(double a);
but this doesn't seem to work.
I wrote this program to find the length of a missing side of a right triangle. You enter in the value for a, b, and c (c being the hypotenuse) and if you want to find the value for a side you enter in 0 for that value. I have compiled that program and when I run it I get prompted for the values but after that I don't get any output. Any ideas on why this isn't working?
Code:
class path {
public static void main(String arg[])
throws java.io.IOException {
double a;
double b;
double c;
System.out.println("A is? "); //asking for a
a = (double) System.in.read();
System.out.println("B is? "); //asking for b
b = (double) System.in.read();
b = (double) System.in.read();
System.out.println("C is? "); //asking for c
c = (double) System.in.read();
c = (double) System.in.read();
if(c == 0) { //the block that finds out what c is
b = b * b; //getting b squared
a = a * a; //getting a squared
c = a + b; //a squared + b squared equals c squared
double x=Math.sqrt(c); //finding the square root
System.out.println("C is " + x); //telling what c is
}
if(b == 0) {
c = c * c;
a = a * a;
b = a - c;
if(b <= 0) b = b * -1; //ensuring that the program will not to try to find the square root of a negative number
double y=Math.sqrt(b);
System.out.println("B is " + y);
}
if(a == 0) {
b = b * b;
c = c * c;
a = c - b;
if(a <= 0) a = a * -1;
double z=Math.sqrt(a);
System.out.println("A is " + z);
}
}
}
Don't use the InputStream (System.in) directly.
And another thing is that you are reading as the character, not the numeric. So you need to convert it to the double value and you cannot just cast it in order to do so.
Here is the correct way of doing this. Any question, feel free to ask me back.
import java.io.*;
public class path {
public static void main(String arg[]) throws java.io.IOException {
double a;
double b;
double c;
System.out.println("A is? "); //asking for a
String strA = new BufferedReader(new InputStreamReader(System.in)).readLine();
a = Double.parseDouble(strA);
System.out.println("B is? "); //asking for b
String strB = new BufferedReader(new InputStreamReader(System.in)).readLine();
b = Double.parseDouble(strB);
System.out.println("C is? "); //asking for c
String strC = new BufferedReader(new InputStreamReader(System.in)).readLine();
c = Double.parseDouble(strC);
if(c == 0) { //the block that finds out what c is
b = b * b; //getting b squared
a = a * a; //getting a squared
c = a + b; //a squared + b squared equals c squared
double x=Math.sqrt(c); //finding the square root
System.out.println("C is " + x); //telling what c is
}
if(b == 0) {
c = c * c;
a = a * a;
b = a - c;
if(b <= 0) b = b * -1; //ensuring that the program will not to try to find the square root of a negative number
double y=Math.sqrt(b);
System.out.println("B is " + y);
}
if(a == 0) {
b = b * b;
c = c * c;
a = c - b;
if(a <= 0) a = a * -1;
double z=Math.sqrt(a);
System.out.println("A is " + z);
}
}
}
Last edited by moeminhtun; 07-30-2003 at 02:07 AM.
I see how in String strA is so going to give input and how in the next line it is assigning 'a' the user input. I have two questions though.
First, what does the
"import java.io.*;"
line do, is it just saying that there is going to be input, like declaring a variable before it is used, and in
String strA = new BufferedReader(new InputStreamReader(System.in)).readLine();
is the first one always going to be A, or is that related to my variables?
Thanks a lot the for the help, I really appreciate it.
Originally posted by darin3200
First, what does the
"import java.io.*;"
line do, is it just saying that there is going to be input, like declaring a variable before it is used, and in
String strA = new BufferedReader(new InputStreamReader(System.in)).readLine();
"import java.io.* is something like "#include <blabla.h>" in c/c++.
If you use "BufferedReader", "InputStreamReader" classes, you have to import "io" package. Remove this line and try to compile it, the compiler will not understand these two classes.
Quote:
is the first one always going to be A, or is that related to my variables?
you can give any name you want. They are just variable names.
Last edited by moeminhtun; 07-30-2003 at 09:03 AM.
It's for fraction calculations.
Yeah! That will do.
Thanks for the link. It would be also useful for me.
You can do like this, for example,
Fraction fa = new Fraction(2, 4); // which is 2/4
Fraction fb = new Fraction(1, 4); // which is 1/4
Fraction result = fa.plus(fb); // fa + fb (2/4 + 1/4 in this case)
System.out.println(result.toString()); // this will print out the result, 3/4 in this case.
Last edited by moeminhtun; 07-30-2003 at 08:46 PM.
The easiest way is, create a new file called "Fraction.java" in the same folder and copy the program and paste it there. Then remove the following line at the top,
package EDU.oswego.cs.dl.util.concurrent.misc;
and compile it.
Now, when you compile it, you need to give the "classpath" to the current folder otherwise the compiler will not find your "Fraction.java".
It's like this. (notice the dot).
javac -classpath . path.java
When you run it, you can just run it normal (no need to give classpath) , like
java path
because the JVM will automatically look for the "Fraction.java" in the current folder.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.