Go Premium for a chance to win a PS4. Enter to Win

x
Solved

# How to input a value for an integer variable

Posted on 2006-11-12
Medium Priority
188 Views
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.

0
Question by:aej1973
• 3
• 3

LVL 13

Expert Comment

ID: 17927365

e34. Reading Text from Standard Input

try {
String str = "";
while (str != null) {
System.out.print("> prompt ");
process(str);
}
} catch (IOException e) {
}
0

LVL 13

Expert Comment

ID: 17927372
the method process(str) would be the place that you would do something like:

try {
len = Integer.parseInt(str) ;
} catch (Exception e) {
}
0

Author Comment

ID: 17927425
Do I have to include or import any files to use the above code?
0

LVL 13

Expert Comment

ID: 17927430
0

Author Comment

ID: 17927449
This is my code. Where am I going wrong?

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 {
String str = "";
while (str != null) {
System.out.print("> prompt ");
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);

}
}

0

LVL 13

Accepted Solution

marchent earned 300 total points
ID: 17928243
import java.io.IOException;

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;
try {
System.out.print("Input Length : ");
System.out.print("Input width : ");
Rectangle rect = new Rectangle(l,w);
area = rect.calculateArea();
perimeter = rect.calculatePerimeter();
System.out.println("The area of the Rectangle is : "+area);
System.out.println("The perimeter of the Rectangle is : "+perimeter);

} catch (Exception e) {
System.out.println(e);
}
}
}
0

Author Comment

ID: 17929412
Thank you.
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used byâ€¦
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
###### Suggested Courses
Course of the Month10 days, 21 hours left to enroll