-
simple input/output program
ive been using c++ for a while but am having problems with my following code. i want to input 2 floating point values and then do 2 calculations on them. my problem is that it compiles but bypasses the second input and goes straight back to the prompt. but if i initialize length and width with values and dont ask for input the program works. can someone tell me what im doing wrong. thanks in advance
import java.io.*;
public class q4{
public static void main( String args[] )throws IOException
{
float l = 0.0F;
float w = 0.0F ;
System.out.print("Enter the length: ");
l = System.in.read();
System.out.print("Enter the width: ");
w = System.in.read();
perimeter(l,w);
area(l,w);
}
//a method for calculating the perimeter
public static void perimeter( float length, float width){
if( length >= 0.0 && length <= 20.0 ){
if( width >= 0.0 && width <= 20.0 )
System.out.println
("The perimeter of the rectangle is " + (( length * 2) + ( width * 2 )));
}
}
//a method for calculating the area
public static void area( float length, float width){
if( length >= 0.0 && length <= 20.0 ){
if( width >= 0.0 && width <= 20.0 )
System.out.println
("The area of the rectangle is " + (length * width));
}
}
}
-
not sure exactley why your code dosen't work, but i'm pretty sure System.in.read() returns a String.....
as for the rest, i'm not used to working with io, without Buffers/BufferedReaders
Linux?!?!?!
What is that, something to eat?
"All things being equal, the simplest solution tends to be the best."
Acham's Razor
-
What nextwave said. You might want to try turning the input into a float before you try to assign it.
Code:
System.out.print("Enter the length: ");
l = Float.parseFloat(System.in.read());
-- Steven
-
wow i was actualy right
Linux?!?!?!
What is that, something to eat?
"All things being equal, the simplest solution tends to be the best."
Acham's Razor
-
There is a bit more to this problem.
Firstly, System.in.read() returns an int from 0 to 255 because it only reads in bytes.
The following code will help you more:
Code:
float l = 0.0F;
float w = 0.0F ;
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter the length: ");
l = Float.parseFloat(in.readLine());
System.out.print("Enter the width: ");
w = Float.parseFloat(in.readLine());
q4.perimeter(l,w);
q4.area(l,w);
The bufferedReader is a much friendlier way to get input from the user.. and readLine() *does* return a String so it had to be parsed into a Float object.
Also note the change to the last two lines of the block ..
since your methods perimeter() and area() are static methods, they could not be called just by their name, the name of the class has to be prefixed since they are bound to the class.
Hope this helps
Laziness is a virtue.
-
Forgive the mistake in my reply... the string was parsed from into a float primative, not a Float object.
Laziness is a virtue.
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