13 Replies - 1604 Views - Last Post: 07 June 2015 - 09:05 AM Rate Topic: -----

#1 mandy978765  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 05-June 15

How do you put numbers that were inputed by the user into an array

Posted 05 June 2015 - 07:51 AM

Hello I am trying to write a java program that calculates how many gallons of paint are needed for a series of rooms. I started by asking the user how many rooms they needed to paint then I store that value and use it as in input to call a method named rooms. In my code I have two methods titled rooms(one is commented out because it is incorrect) and the other is not incorrect but it is not working the way I want it to. Looking at the second rooms method...I need it to be in a while loop as it should print
How wide is room #" + n + " in feet?
How long is room #" + n + " in feet?
How tall is room #" + n + " in feet?
for each n..(n number of rooms). What I am having problems with is then I need to store the width length and tall numbers entered by the user in an array however because it is in a loop the value that is stored ie response,response2, response 3 is being overwritten through every loop iteration. I am able to get the size of the array with the value n (that was calculated in method rooms) and that array is initialized in the method titled array however I do not know how to get the values from the while loop into method array. The first method titled rooms that is commented out was my attempt at combining the two methods however then I end up having the method call itself and was getting errors. Any help would be greatly appreciated.
[import java.util.Scanner;

public class Assignment3 {
	public static void main(String[] args)
	{

		Scanner input=new Scanner(System.in);
 		System.out.println("How many rooms do you need to paint: ");
		int value=input.nextInt();
		int calling=rooms(value);
		System.out.println("How many square feet can one gallon of paint cover?");
		int gallon=input.nextInt();

		//storing value of the number of rooms you need to paint
	}

	/*public static int rooms(int value)
	{
		int n=1;

		while (n<=value)
		{
			//create an array myArray[0]=5(n);
			Scanner input=new Scanner(System.in);
			int myArray[];
			myArray=new int[];
			for (int i=0;i< myArray.length; i=i+3)
			{
				myArray[i]=input.nextInt();
			System.out.println("How wide is room #" + n +  " in feet?");
			double respose=input.nextDouble();
			myArray[i]=input.nextInt();
			System.out.println("How long is room #" + n +  " in feet?");
			double respose2=input.nextDouble();
			myArray[i+1]=input.nextInt();
			System.out.println("How tall is room #" + n +  " in feet?");
			double respose3=input.nextDouble();
			myArray[i+2]=input.nextInt();
			System.out.println(respose3+ "response3");
			n=n+1;	
			System.out.println(n);
			//System.out.println(myArray[i]);
			}
		}
		return n;
		//System.out.println("Array values are "+ myArray[]);
	}
	//for(int n=1;n<=value;n++)
	//{

	//}*/


	




	public static void array(int calling)
	{
		int myArray[];
		myArray=new int[(calling-1)*3];//is this really integers?
		for (int i=1;i<myArray.length;i++)
		{

		}
	}
	public static int rooms(int value)
	{
		int n=1;
		while (n<=value)
		{
			
			Scanner input=new Scanner(System.in);
			System.out.println("How wide is room #" + n +  " in feet?");
			double respose=input.nextDouble();
			System.out.println("How long is room #" + n +  " in feet?");
			double respose2=input.nextDouble();
			System.out.println("How tall is room #" + n +  " in feet?");
			double respose3=input.nextDouble();
			System.out.println(respose3+ " response3");
			n=n+1;	
			System.out.println(n);
			//System.out.println(myArray[i]);
		}


		return n;
		//System.out.println("Array values are "+ myArray[]);
	}





}]

:code:

This post has been edited by modi123_1: 05 June 2015 - 07:53 AM
Reason for edit:: please use the 'code' button in the editor to add code tags


Is This A Good Question/Topic? 0
  • +

Replies To: How do you put numbers that were inputed by the user into an array

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 13567
  • View blog
  • Posts: 54,129
  • Joined: 12-June 08

Re: How do you put numbers that were inputed by the user into an array

Posted 05 June 2015 - 08:05 AM

Quote

owever I do not know how to get the values from the while loop into method array.


You would figure - if you are storing three values for each room then you could make a 2d array.. one dimension being 'n' (the number of rooms), and the other being '3'.. since you need three values you want to store..

then, in your loop as you go back to ask for values you know what row index you are on and can slam values into the dimensions.


Say they asked for two rooms. your array would be:
     h   w   L
[0]->[], [], []
[1]->[], [], []



Your loop would go through each room..

tempRoom = 0
maxNumRooms = 2

WHILE tempRoom < maxNumRooms THEN
  GET height
  GET width
  GET length

  array[tempRoom][0] GETS height
  array[tempRoom][1] GETS width
  array[tempRoom][2] GETS length
  
  tempRoom increments by 1
NEXT




Walking through it:

tempRoom = 0
maxNumRooms = 2

WHILE 0 < 2 THEN
  height = 20
  width = 21
  length = 22

  [0][0] -> 20
  [0][1] -> 21
  [0][2] -> 22

  tempRoom  -> 1
NEXT 


WHILE 1 < 2 THEN
  height = 30
  width = 31
  length = 32

  [1][0] -> 30
  [1][1] -> 31
  [1][2] -> 32

  tempRoom  -> 2
NEXT 


2 is not less than 2 so the loop ends.



Leaving:
     h      w     L
[0]->[20], [21], [22]
[1]->[30], [31], [32]


Was This Post Helpful? 0
  • +
  • -

#3 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3465
  • View blog
  • Posts: 15,680
  • Joined: 20-September 08

Re: How do you put numbers that were inputed by the user into an array

Posted 05 June 2015 - 08:13 AM

Quote

What I am having problems with is then I need to store the width length and tall numbers entered by the user in an array

Why?
Was This Post Helpful? 0
  • +
  • -

#4 mandy978765  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 05-June 15

Re: How do you put numbers that were inputed by the user into an array

Posted 05 June 2015 - 08:14 AM

I Thought about using a multi dimensional array as well but unfortunately I believe am only supposed to use a 1D array
Was This Post Helpful? 0
  • +
  • -

#5 jon.kiparsky  Icon User is offline

  • Chinga la migra
  • member icon


Reputation: 10733
  • View blog
  • Posts: 18,362
  • Joined: 19-March 11

Re: How do you put numbers that were inputed by the user into an array

Posted 05 June 2015 - 08:15 AM

I'm not entirely sure I'm understanding your issue correctly, but here's a rough sketch that might help: (pseudocode, you can write the java)


function get_room_dimensions(number_of_rooms):
  room_dimensions = array(length=number_of_rooms * 3)
  index = 0
  for n in number_of_rooms:
    room_dimensions[index] = input ("Enter length of room")
    index +=1
    room_dimensions[index] = input ("Enter width of room")    
    index +=1
    room_dimensions[index] = input ("Enter height of room")
    index +=1
  return room_dimensions



This will return an array whose elements n*3..n*3+2 contain the length, width, and height for room #n.
I don't particularly like this way of doing it. I'd rather write Java, which means I would want to model a Room with an object, but if you want to store the dimensions in an array, this is one way to do it.

(can you think of any other ways to do it? I can think of at least two)
Was This Post Helpful? 0
  • +
  • -

#6 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 13567
  • View blog
  • Posts: 54,129
  • Joined: 12-June 08

Re: How do you put numbers that were inputed by the user into an array

Posted 05 June 2015 - 08:15 AM

Okay then just store the resulting math for the total gallons of paint for the room in the array.. forget storing h, w, l.
Was This Post Helpful? 0
  • +
  • -

#7 mandy978765  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 05-June 15

Re: How do you put numbers that were inputed by the user into an array

Posted 05 June 2015 - 08:30 AM

View Postmodi123_1, on 05 June 2015 - 08:15 AM, said:

Okay then just store the resulting math for the total gallons of paint for the room in the array.. forget storing h, w, l.


Not sure if I am able to do that either bc it says "You must use an array to store information about the different rooms"


Start by asking the user how many rooms they need to paint
For each room, ask the user for a width, length and height in feet
After collecting the dimensions for each room, ask the user how many square feet can be covered by one gallon of paint.
Next, for each room, inform the user how much area needs to be painted and how many gallons of paint that will require.
Assume that all 4 walls need to be painted, and that doors and windows are not a factor
Finally, tell the user the total area that needs to be painted, the number of gallons of paint that needs to be purchased (in whole gallon increments) if all the rooms are the same color, and the number of gallons that need to be purchased if each room is a different color.
If all of the rooms use the same color, then any left-over portion of a gallon from one room can be used on another room.
If all of the rooms use different colors, then any left-over portion of a gallon is wasted and the next room start over with a fresh gallon.
The order given above is a requirement to steer everyone towards using certain concepts we want you to get practice with.

Hints and Warnings

You must use an array to store information about the different rooms. Furthermore, your array must be appropriately sized based on how many rooms the user said they have.
Before you start coding, spend some time planning out your program. Don't just dive in and start pounding out unplanned code. This program will benefit from appropriate use of methods.
Your main method should mostly be concerned with directing the flow of the program, while the details and any bulky or repeated work is handled by other methods.
For instance, collecting all of the room dimensions is a task that probably has a nice name you can give it, and it would be nice to get that clutter out of your main method...
Think hard about what information is needed by a method to do it's job. Make sure those pieces of information are method parameters
Think hard about what information is needed by anyone calling a method. Make sure that information gets passed back as the return value
Make sure you store/pass enough information to get the job done, but don't store/pass information that isn't necessary. If you find yourself needing to pass a lot of information between methods, perhaps you should rethink where you decided to draw the line between what goes in which method.
Don't forget that Math.ceil(), Math.floor(), Math.round() and type casting are all tools that you can use. You may or may not need them depending on how you tackle this program.
Was This Post Helpful? 0
  • +
  • -

#8 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3465
  • View blog
  • Posts: 15,680
  • Joined: 20-September 08

Re: How do you put numbers that were inputed by the user into an array

Posted 05 June 2015 - 08:39 AM

Quote

Why?

OK, so the answer to that is: it's in the spec

Quote

if all the rooms are the same color, and the number of gallons that need to be purchased if each room is a different color.


.. and yet they don't ask for input about the color. afaics, that's a problem
Was This Post Helpful? 0
  • +
  • -

#9 mandy978765  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 05-June 15

Re: How do you put numbers that were inputed by the user into an array

Posted 05 June 2015 - 08:39 AM

Sample Run

How many rooms do you need to paint?
3
How wide is room #1 in feet?
8.4
How long is room #1 in feet?
10.7
How tall is room #1 in feet?
8
How wide is room #2 in feet?
15.7
How long is room #2 in feet?
9.3
How tall is room #2 in feet?
8.5
How wide is room #3 in feet?
6.2
How long is room #3 in feet?
18
How tall is room #3 in feet?
6
How many square feet can 1 gallon of paint cover?
250
Room 1 has 305.6 square feet of wall area to paint, which at
250.0 square feet per gallon, will require
1.2224000000000002 gallons of paint.

Room 2 has 425.0 square feet of wall area to paint, which at
250.0 square feet per gallon, will require
1.7 gallons of paint.

Room 3 has 290.4 square feet of wall area to paint, which at
250.0 square feet per gallon, will require
1.1616 gallons of paint.

This is a total of 1021.0 square feet, which will require
5 gallons of paint if all of the rooms are the same color, or
6 gallons of paint if every room is a different color.
Was This Post Helpful? 0
  • +
  • -

#10 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3465
  • View blog
  • Posts: 15,680
  • Joined: 20-September 08

Re: How do you put numbers that were inputed by the user into an array

Posted 05 June 2015 - 08:44 AM

OK, then 'same color' is a kind of binary with 'all different colors' and not required to be connected with input. In that case, there is NO problem

So we know that an array IS required and we can construct a 2D one, as has been suggested. So what's the problem with that?

This post has been edited by g00se: 05 June 2015 - 08:45 AM
Reason for edit:: Clarification

Was This Post Helpful? 0
  • +
  • -

#11 mandy978765  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 05-June 15

Re: How do you put numbers that were inputed by the user into an array

Posted 06 June 2015 - 07:29 PM

Hello,
I am trying to write a program that asks the user how many rooms that they need to paint and then asks the user the width, length and height in feet of each room. After collecting the dimensions for each room, I ask the user how many square feet can be covered by one gallon of paint.
Next, for each room, I am supposed to inform the user how much area needs to be painted and how many gallons of paint that will require. I calculated the area of the first room in the method titled calcTotalRoomArea. Originally I had included the for loop that is now in the main in that method, however then the main does not recognize the [][] items that I would need to list inside the array as they are out of the scope of the main. I then moved the for loop up to the main and tried to combine the one area I got from that method and the other areas I will be getting from the loop in a single array. I am not sure if I did that right of if I am actually overwriting the first element in my array. I am also now getting the following error messages after moving the loop up to the main that I do not understand. I am newer to Java code and would really appreciate the help.

Exception in thread "main" java.lang.Error: Unresolved compilation problems:
Type mismatch: cannot convert from double to int
roomArea cannot be resolved to a variable
width cannot be resolved to a variable
height cannot be resolved to a variable
length cannot be resolved to a variable
height cannot be resolved to a variable
roomArea cannot be resolved to a variable
i cannot be resolved to a variable
roomArea cannot be resolved to a variable

at assignment3_3.main(assignment3_3.java:11)

import java.util.Scanner;

public class project3_4 {
	public static void main(String[] args, int roomArea)
	{
		Scanner input=new Scanner(System.in);
		System.out.println("How many rooms do you need to paint: ");  //changing number of rooms to a double 
		int numberOfRooms=input.nextInt();
		double[][]roomData=new double[3][numberOfRooms];
		double calling=getRoomDimensions(numberOfRooms,roomData);
		double calling2=calcTotalRoomArea(roomData);   //returning room data for the first room
		double[][]ArrayRoomArea=new double[(int)roomArea][i];
		 //int width=0;
		//int length=1;
		//int height=2;
		
		for(int i=1;i<numberOfRooms;i++){
			roomArea=(int) ((roomData[0][i]*roomData[2][i]*2)+(roomData[1][i]*roomData[2][i]*2));
			ArrayRoomArea[(int)roomArea][i]=input.nextInt();
		}	
			ArrayRoomArea[(int)calling2][0]=input.nextInt();   //is this overriting the array
			
		double[][] arrayRoomArea=new double[numberOfRooms][(int)roomArea];
		System.out.println("How many square feet can one gallon of paint cover?");
		int gallon=input.nextInt();
		//double paintNeeded=gallonsOfPaintNeeded(calling2,gallon);
		//printMethod(calling2, gallon, paintNeeded,numberOfRooms, arrayRoomArea);
		

	}

	public static double getRoomDimensions(int numberOfRooms,double[][]roomData)
	{

		int n=0;
		while (n<numberOfRooms)
		{
			Scanner input=new Scanner(System.in);
			System.out.println("How wide is room #" + (n+1) +  " in feet?");	
			roomData[0][n]=input.nextDouble();
			System.out.println("How long is room #" + (n+1) +  " in feet?");
			roomData[1][n]=input.nextDouble();
			System.out.println("How tall is room #" + (n+1) +  " in feet?");
			roomData[2][n]=input.nextDouble();
			n=n+1;	
		}
		return roomData[3][n];    //RETURNING THE RIGHT INFORMATION FOR ROOM DATA?
	} 


		public static double calcTotalRoomArea(double [][]roomData){
			int i=0;
			double roomArea=0;
			int width=0;
			int length=1;
			int height=2;
			Scanner input=new Scanner(System.in);
				roomArea=(roomData[width][i]*roomData[height][i]*2)+(roomData[length][i]*roomData[height][i]*2);
			return roomArea;

Was This Post Helpful? 0
  • +
  • -

#12 horace  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 768
  • View blog
  • Posts: 3,832
  • Joined: 25-October 06

Re: How do you put numbers that were inputed by the user into an array

Posted 06 June 2015 - 10:39 PM

for a start in line 12
               double[][]ArrayRoomArea=new double[(int)roomArea][i];
 

you have not defined i and given it a value
Was This Post Helpful? 0
  • +
  • -

#13 macosxnerd101  Icon User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12189
  • View blog
  • Posts: 45,251
  • Joined: 27-December 08

Re: How do you put numbers that were inputed by the user into an array

Posted 07 June 2015 - 08:46 AM

Duplicate threads merged. Please do not duplicate post.
Was This Post Helpful? 0
  • +
  • -

#14 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3465
  • View blog
  • Posts: 15,680
  • Joined: 20-September 08

Re: How do you put numbers that were inputed by the user into an array

Posted 07 June 2015 - 09:05 AM

Quote

ArrayRoomArea=

Java variables begin lower case and are in camel case
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1