simple input/output program


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 6 of 6

Thread: simple input/output program

  1. #1
    Join Date
    Feb 2003
    Posts
    1

    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));
    }
    }



    }

  2. #2
    Join Date
    Feb 2003
    Posts
    15
    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

  3. #3
    Join Date
    Dec 2002
    Posts
    83
    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

  4. #4
    Join Date
    Feb 2003
    Posts
    15
    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

  5. #5
    Join Date
    Mar 2003
    Posts
    17
    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.

  6. #6
    Join Date
    Mar 2003
    Posts
    17
    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
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center