Link to home
Start Free TrialLog in
Avatar of aej1973
aej1973

asked on

How to input a value for an integer variable

Hi,
I want to have the user input length and width so that my program calculates area and perimeter of a rectangle. How do I write the input statements? Here is my code:

class Rectangle {
private int length;
private int width;

public Rectangle(int l, int w) {
length = l;
width = w;
}

public int calculateArea() {
return length * width;
}

public int calculatePerimeter() {
return (2 * length + 2 * width);
}

public static void main(String args[]) {
    int l,w,area,perimeter;
    System.out.println("Input length:");
    l = input.nextInt(); //????
    System.out.println("Input width:");
    w = input.nextInt();//????
    Rectangle rect = new Rectangle(l,w);
    area = rect.calculateArea();
    perimeter = rect.calculatePerimeter();
    System.out.println("The area of the rectangle is: ");
 
System.out.println(area);
System.out.println("The perimeter of the rectangle is: ");
System.out.println(perimeter);

}
}

Thank you.

Avatar of kawas
kawas
Flag of United States of America image


quoting: http://javaalmanac.com/egs/java.io/ReadFromStdIn.html

e34. Reading Text from Standard Input

    try {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        String str = "";
        while (str != null) {
            System.out.print("> prompt ");
            str = in.readLine();
            process(str);
        }
    } catch (IOException e) {
    }
the method process(str) would be the place that you would do something like:

try {
len = Integer.parseInt(str) ;
} catch (Exception e) {
// bad input
}
Avatar of aej1973
aej1973

ASKER

Do I have to include or import any files to use the above code?
java.io.BufferedReader
java.io.InputStreamReader
Avatar of aej1973

ASKER

This is my code. Where am I going wrong?

import java.io.BufferedReader;
import java.io.InputStreamReader;

class Rectangle {
private int length;
private int width;
public Rectangle(int l, int w) {
length = l;
width = w;
}
public int calculateArea() {
return length * width;
}
public int calculatePerimeter() {
return (2 * length + 2 * width);
}
public static void main(String args[]) {
    int l,w,area,perimeter;
    System.out.println("Input length:");
    try {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        String str = "";
        while (str != null) {
            System.out.print("> prompt ");
            str = in.readLine();
            process(str);
        }
    } catch (IOException e) {
    }

    System.out.println("Input width:");
    Rectangle rect = new Rectangle(l,w);
        area = rect.calculateArea();
        perimeter = rect.calculatePerimeter();
        System.out.println("The area of the rectangle is: ");
       
System.out.println(area);
System.out.println("The perimeter of the rectangle is: ");
System.out.println(perimeter);

}
}

ASKER CERTIFIED SOLUTION
Avatar of marchent
marchent
Flag of Bangladesh image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of aej1973

ASKER

Thank you.