# Room dimensions User input help

Page 1 of 1

## 11 Replies - 944 Views - Last Post: 09 February 2013 - 09:14 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=311476&amp;s=f219290f3d8b1da18e9df8907a22473f&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 LawlessBaron

Reputation: 5
• Posts: 115
• Joined: 07-February 13

# Room dimensions User input help

Posted 07 February 2013 - 06:11 AM

Hi ok so i have just started Java Programming i have studied but i am still get a little lost and stuck with the code
I was wondering if anyone could help me with a code that would require user input for the value to be multiplied to get the final answer i am not asking of anyone to do it for me i am simply asking to be pointed in the write direction as to speak

So here is the code for the first project

/*
Name: Reece Baron
Date: 7 Feb 2013
ID: 130830521
Ver 0.0.5
*/
public class Room {
public static void main(String[] args) {
int length = 15; // Defines length of the Room
int width = 25; //Defines width of the Room
int area = length/width;
area = length * width; // Defines Multiplication between the two areas
System.out.println("The length of the room is 15"); //Prints Room length
System.out.println("The width of the room is 25"); //Prints Rooms width
System.out.println("Which gives you a total floor space of " + area + " feet"); // Displays the total of the two areas multiplied
}
}

the objective it to have it allow user input to define the length and width

Is This A Good Question/Topic? 0

## Replies To: Room dimensions User input help

### #2 Ryano121

• D.I.C Lover

Reputation: 1380
• Posts: 3,021
• Joined: 30-January 11

## Re: Room dimensions User input help

Posted 07 February 2013 - 06:19 AM

To get user input you can use the Scanner class e.g

System.out.println("Enter the first number");
//get user input for number

Once you have that you can do your arithmetic just as before.

### #3 LawlessBaron

Reputation: 5
• Posts: 115
• Joined: 07-February 13

## Re: Room dimensions User input help

Posted 07 February 2013 - 06:34 AM

Thanks mate all i must do now is get it to multiply it

### #4 ILoveJava

• D.I.C Regular

Reputation: 29
• Posts: 389
• Joined: 12-March 12

## Re: Room dimensions User input help

Posted 07 February 2013 - 06:41 AM

Just to help, in your prints, instead of stating the number, state the variable.
System.out.println("The length of the room is " + length "."); //Prints Room length
System.out.println("The width of the room is " + width "."); //Prints Rooms width

This post has been edited by ILoveJava: 07 February 2013 - 06:41 AM

### #5 LawlessBaron

Reputation: 5
• Posts: 115
• Joined: 07-February 13

## Re: Room dimensions User input help

Posted 07 February 2013 - 06:44 AM

ILoveJava, on 07 February 2013 - 06:41 AM, said:

Just to help, in your prints, instead of stating the number, state the variable.
System.out.println("The length of the room is " + length "."); //Prints Room length
System.out.println("The width of the room is " + width "."); //Prints Rooms width

Thankyou for that

### #6 rgfirefly24

• D.I.C Lover

Reputation: 380
• Posts: 1,891
• Joined: 07-April 08

## Re: Room dimensions User input help

Posted 07 February 2013 - 10:13 AM

LawlessBaron, on 07 February 2013 - 08:11 AM, said:

int area = length/width;
area = length * width; // Defines Multiplication between the two areas

Also, you already have the code to get it to multiply; however, you add in a division when declaring the area variable. This is not needed. I would change these lines to read int area = length * width. You would also do that after you've set the length and width variables from the user input.

### #7 LawlessBaron

Reputation: 5
• Posts: 115
• Joined: 07-February 13

## Re: Room dimensions User input help

Posted 08 February 2013 - 07:44 PM

Thankyou i got the code working

### #8 k3y

Reputation: 36
• Posts: 205
• Joined: 25-February 12

## Re: Room dimensions User input help

Posted 08 February 2013 - 08:10 PM

Hey LawlessBaron;
Glad to see that you got you code working. I just wanted to base an idea off of your initial code and show you some stuff that you could do with such a simple concept such as user input. Before I paste my code I want to warn you, I am still learning as well. With that said I will breakdown my code and then show you the code as a whole. I will also include handy links to official Oracle tutorials if you need any clarification. Please do not feel overwhelmed it will make sense as you progress on you programming journey =D.

package roomdimensions;

A Java package is a mechanism for organizing Java classes into namespaces similar to the modules of Modula. According to wikipedia, that's a fancy way of saying its a digital container that you can store code in that you could later import. However; just think of it as a container right now.
http://docs.oracle.c...kage/index.html

import java.util.InputMismatchException;
import java.util.Scanner;

These are called import statements. There are two primary ways of importing, explicit and wildcard. It is better practice to import explicitcly, as it is easier to keep track of what you will be using in your code. Also I learned this from jon.kiparsky =D
http://docs.oracle.c...ge/usepkgs.html

class RoomDimensions {

This is called a class header (used when declaring a class), it can have accessmodifiers ahead of it however; you don't always have to.
http://docs.oracle.c.../classdecl.html

private static double length, width;
private static Scanner console = new Scanner(System.in);
private static boolean acceptableUserInput = false;

These are called variables, to be more particular. These are called private class variables. Meaning that they can only be used in this class (RoomDimensions).
http://docs.oracle.c.../variables.html

public static void main(String[] args) {
do{
try{
System.out.println("length: ");
length = getNumberFromUser();
System.out.println("width: ");
width = getNumberFromUser();
acceptableUserInput = true;
}catch(InputMismatchException e){
console.next();
continue;
}
}while(!(acceptableUserInput));

This is called a looping mechanism, to be more specific a do...while loop. I wont go into the specifics but this loop will continue forever unless the user inputs a valid number. If the user does input an invalid type (not double) the loop will restart and ask for input again. However, within this loop there are method calls (a way to communicate to other pieces of code that are reusable). I will provide links to each idea this code has:
http://docs.oracle.c...olts/while.html
http://docs.oracle.c.../arguments.html
http://docs.oracle.c...ial/exceptions/

printUserInput(length, width);

This is a clear example of a method call. This will pass the length and width variables to the method printUserInput which will print the users input (weird huh =D). There are method calls in the try/catch (try and pick them out =D).

if(length == width){
System.out.println("area: " + getAreaOfSquare(length));
}
else{
System.out.println("area: " + getAreaOfRectangle(length, width));
}

This is called a decision structure, a.k.a an if...else statement. This says if (you meet this condition) then do this, else (otherwise) do this. (once again referencing a method call =D).
http://docs.oracle.c...ndbolts/if.html

private static void printUserInput(double x, double y){
System.out.println("length: " + x + " width: " + y);
}
private static double getAreaOfSquare(double x){
return Math.pow(x, 2);
}
private static double getAreaOfRectangle(double x, double y){
return x * y;
}
private static double getNumberFromUser(){
double tempVariable = console.nextDouble();
return tempVariable;
}

These are called methods, within each method a certain action is performed. I won't go into the specifics but the most important thing to know is that these can be used a number of times. Making it alot easier then constantly writing something. For example you could print something by hand 100 times or you could create a method to call, that will do the same thing and won't waste as much time.
http://docs.oracle.c...vaOO/index.html

That was the general breakdown, don't be discourage like anything it takes time to grasp the concepts. Here is my code in whole
package roomdimensions;
import java.util.InputMismatchException;
import java.util.Scanner;
class RoomDimensions {
private static double length, width;
private static Scanner console = new Scanner(System.in);
private static boolean acceptableUserInput = false;
public static void main(String[] args) {
do{
try{
System.out.println("length: ");
length = getNumberFromUser();
System.out.println("width: ");
width = getNumberFromUser();
acceptableUserInput = true;
}catch(InputMismatchException e){
console.next();
continue;
}
}while(!(acceptableUserInput));
printUserInput(length, width);
if(length == width){
System.out.println("area: " + getAreaOfSquare(length));
}
else{
System.out.println("area: " + getAreaOfRectangle(length, width));
}
}
private static void printUserInput(double x, double y){
System.out.println("length: " + x + " width: " + y);
}
private static double getAreaOfSquare(double x){
return Math.pow(x, 2);
}
private static double getAreaOfRectangle(double x, double y){
return x * y;
}
private static double getNumberFromUser(){
double tempVariable = console.nextDouble();
return tempVariable;
}
}

Once again I am learning as well so, some of my code may not be 100% perfect by industry standards, I just wanted to give you some insight into the potential that you could do. Also D.I.C has a ton of awesome tutorials, here is one that I refer to from now and again: http://www.dreaminco...ogramming-java/

### #9 burakaltr

• D.I.C Regular

Reputation: 91
• Posts: 280
• Joined: 07-November 10

## Re: Room dimensions User input help

Posted 08 February 2013 - 08:18 PM

An Object Oriented Approach

Spoiler

This post has been edited by burakaltr: 08 February 2013 - 08:18 PM

### #10 LawlessBaron

Reputation: 5
• Posts: 115
• Joined: 07-February 13

## Re: Room dimensions User input help

Posted 09 February 2013 - 12:45 AM

Awesome thanks guys, yea they are much more complicated then the one i came up with thanks for sharing those guys

### #11 k3y

Reputation: 36
• Posts: 205
• Joined: 25-February 12

## Re: Room dimensions User input help

Posted 09 February 2013 - 06:33 AM

LawlessBaron, on 09 February 2013 - 02:45 AM, said:

Awesome thanks guys, yea they are much more complicated then the one i came up with thanks for sharing those guys />

Once you grasp the concepts it gets easier. It just takes practice. If you follow through the oracle series: http://docs.oracle.c...rted/index.html , it will make sense. However; seeming how dreamincode is amazing you can always post questions with your test code and we'll help you out.

### #12 LawlessBaron

Reputation: 5
• Posts: 115
• Joined: 07-February 13

## Re: Room dimensions User input help

Posted 09 February 2013 - 09:14 PM

k3y, on 09 February 2013 - 06:33 AM, said:

LawlessBaron, on 09 February 2013 - 02:45 AM, said:

Awesome thanks guys, yea they are much more complicated then the one i came up with thanks for sharing those guys />/>

Once you grasp the concepts it gets easier. It just takes practice. If you follow through the oracle series: http://docs.oracle.c...rted/index.html , it will make sense. However; seeming how dreamincode is amazing you can always post questions with your test code and we'll help you out. />