Solved

How to input a value for an integer variable

Posted on 2006-11-12
7
175 Views
Last Modified: 2010-03-31
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
Comment
Question by:aej1973
  • 3
  • 3
7 Comments
 
LVL 13

Expert Comment

by:kawas
ID: 17927365

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) {
    }
0
 
LVL 13

Expert Comment

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

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

Author Comment

by:aej1973
ID: 17927425
Do I have to include or import any files to use the above code?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 13

Expert Comment

by:kawas
ID: 17927430
java.io.BufferedReader
java.io.InputStreamReader
0
 

Author Comment

by:aej1973
ID: 17927449
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);

}
}

0
 
LVL 13

Accepted Solution

by:
marchent earned 75 total points
ID: 17928243
import java.io.BufferedReader;
import java.io.InputStreamReader;
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 {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("Input Length : ");
        l = Integer.parseInt(in.readLine());
        System.out.print("Input width : ");
        w = Integer.parseInt(in.readLine());
        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

by:aej1973
ID: 17929412
Thank you.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
@SBGen Method 3 38
Securing Jmx Console and web console 2 66
fibonacci ten numbers 4 30
hibernate insert example 13 15
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

28 Experts available now in Live!

Get 1:1 Help Now