LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-29-2003, 03:42 PM   #1
darin3200
LQ Guru
 
Registered: Dec 2002
Distribution: Gentoo!
Posts: 1,153

Rep: Reputation: 45
Java program skipping a line


Hi,
I have written the following program
Code:
class linux {
public static void main(String args[])
throws java.io.IOException {
char a;
char b;
char c;
System.out.print("A is?");
a = (char) System.in.read();
System.out.print("B is?");
b = (char) System.in.read();
System.out.print("C is?");
c = (char) System.in.read();
System.out.println("A is " +a );
System.out.println("B is " +b );
System.out.println("C is " +c );
}
}
It compilies without any probelm but when I run it get this.
Quote:
[darinf@localhost Java]$ java linux
A is?1
B is?C is?2
A is 1
B is

C is 2
[darinf@localhost Java]$
So the program with never give me the chance to tell it what 'b' is. Any have ideas?
Thanks,
 
Old 07-29-2003, 05:09 PM   #2
coolman0stress
Member
 
Registered: Jun 2003
Location: Toronto, Ontario, Canada
Posts: 288

Rep: Reputation: 30
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.

Hope this gets you on the right track...
 
Old 07-29-2003, 05:45 PM   #3
Looking_Lost
Senior Member
 
Registered: Apr 2003
Location: Eire
Distribution: Slackware 12.0, OpenSuse 10.3
Posts: 1,120

Rep: Reputation: 45
Yup,

just do another read to catch the newline after every read

System.in.read();
 
Old 07-29-2003, 06:01 PM   #4
darin3200
LQ Guru
 
Registered: Dec 2002
Distribution: Gentoo!
Posts: 1,153

Original Poster
Rep: Reputation: 45
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.
 
Old 07-29-2003, 06:20 PM   #5
Looking_Lost
Senior Member
 
Registered: Apr 2003
Location: Eire
Distribution: Slackware 12.0, OpenSuse 10.3
Posts: 1,120

Rep: Reputation: 45
Use it this sort of way

double mynumber=Math.sqrt(4);


have to call it Math.sqrt();
 
Old 07-29-2003, 09:04 PM   #6
darin3200
LQ Guru
 
Registered: Dec 2002
Distribution: Gentoo!
Posts: 1,153

Original Poster
Rep: Reputation: 45
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);
}
}
}
 
Old 07-29-2003, 11:48 PM   #7
moeminhtun
Member
 
Registered: Dec 2002
Location: Singapore
Distribution: Fedora Core 6
Posts: 647

Rep: Reputation: 30
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 03:07 AM.
 
Old 07-30-2003, 09:27 AM   #8
darin3200
LQ Guru
 
Registered: Dec 2002
Distribution: Gentoo!
Posts: 1,153

Original Poster
Rep: Reputation: 45
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.
 
Old 07-30-2003, 10:02 AM   #9
moeminhtun
Member
 
Registered: Dec 2002
Location: Singapore
Distribution: Fedora Core 6
Posts: 647

Rep: Reputation: 30
Quote:
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 10:03 AM.
 
Old 07-30-2003, 05:48 PM   #10
darin3200
LQ Guru
 
Registered: Dec 2002
Distribution: Gentoo!
Posts: 1,153

Original Poster
Rep: Reputation: 45
One more questions, is there any easy way to do fractions in Java? I have found this but what should I do with it?
 
Old 07-30-2003, 09:37 PM   #11
moeminhtun
Member
 
Registered: Dec 2002
Location: Singapore
Distribution: Fedora Core 6
Posts: 647

Rep: Reputation: 30
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 09:46 PM.
 
Old 07-30-2003, 10:08 PM   #12
darin3200
LQ Guru
 
Registered: Dec 2002
Distribution: Gentoo!
Posts: 1,153

Original Poster
Rep: Reputation: 45
What do I do with the file? I can't just put in Fraction fa =... because the complier doesn't recognize it.
 
Old 07-30-2003, 10:41 PM   #13
moeminhtun
Member
 
Registered: Dec 2002
Location: Singapore
Distribution: Fedora Core 6
Posts: 647

Rep: Reputation: 30
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
BASH: read every line in the files and use the line as parameters as another program tam3c36 Programming 10 12-07-2010 02:42 PM
Hiding password in Java command line program simon_w Programming 2 05-02-2005 07:01 AM
FC2 test 3 mouse skipping a beat, movie skipping also jang Fedora 1 10-28-2004 08:42 PM
Command Line...Skipping the Cc: in a Pine rutman Linux - General 0 06-14-2004 01:45 PM
Command line arguments to java program Majjj Programming 3 10-23-2003 06:48 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:41 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration