Help with multiple inputs

Obtaining and storing multiple inputs with JOptionPanes

Page 1 of 1

12 Replies - 579 Views - Last Post: 10 March 2013 - 09:40 PM Rate Topic: -----

#1 josephine11  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 01-September 12

Help with multiple inputs

Posted 08 March 2013 - 09:47 PM

I am trying to create a swing program that receives input of wall measurements in feet, calculates the amount of wallpaper rolls needed outputs this information along with a price. As you can see I have only created a little, I cannot seem to figure out how to add measurements of another room. I know I need to use an array, but I am not sure how I assign another rooms measurements to an array and then calculate the amount of rolls... any suggestions??


import javax.swing.JOptionPane;

public class wallarea
{
	// main method
	public static void main(String[]args)
	{
		double area ;
		String input;
		int number = 1;
		
		while (number == 1) // while loop
		{
			input = JOptionPane.showInputDialog("Enter the Lenght in feet of the wall");
			int l = Integer.parseInt(input);
			input = JOptionPane.showInputDialog("Enter the Height in feet of the wall");
			int h = Integer.parseInt(input);
			areaOfRoom(l,h);
			input = JOptionPane.showInputDialog("Please Type in 1 to add measurements of another wall or 2 to quit");
			number = Integer.parseInt(input);
		}
		System.exit (0);
	}
	
	// Method, to find Area of room to be covered
	public static void areaOfRoom(int lenght, int height)
	{
		double areaOfwall = (lenght*height);
		double areaOfwallTotal = ((areaOfwall *.10) + areaOfwall);
		double numberOfRolls = (areaOfwallTotal / 35);
		double roundNumRolls = Math.ceil(numberOfRolls);
		JOptionPane.showMessageDialog(null, "The number of wallpaper rolls needed is  " + roundNumRolls + "."); 
		
	}

} 


Is This A Good Question/Topic? 0
  • +

Replies To: Help with multiple inputs

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2204
  • View blog
  • Posts: 5,236
  • Joined: 10-September 10

Re: Help with multiple inputs

Posted 09 March 2013 - 01:53 AM

Please write better thread titles that describe the topic or the general area in which help is needed. Everyone needs "Help..."

Follow Java naming conventions: Class titles are capitalized.

Your program functions properly the way it is. You'll have to provide more details about what else you need it to do in order to get help.

This post has been edited by GregBrannon: 09 March 2013 - 02:02 AM

Was This Post Helpful? 0
  • +
  • -

#3 Takk  Icon User is offline

  • D.I.C Head

Reputation: 40
  • View blog
  • Posts: 111
  • Joined: 08-March 13

Re: Help with multiple inputs

Posted 09 March 2013 - 02:33 AM

Looking at your code, it looks like you're actually calculating the number of rolls needed 'per wall' instead of 'per room', from that assumption, every time you input a length and a height, you will get feedback telling you how many rolls you will need for that particular wall, you'll have to write that down and do the next wall.. with this method, you would generally do every wall, keep a list of the outputs, and add them all together..

to put together a little bit of psuedo for you to consider on how I would go about this, which may lead you to your answer, and likely a bit of brain pain..

areaDoor = (3*7) // Standard door area?
areaWindow = (3.5*4.5) //Standard window area?

roomLength = input(length of room)
roomWidth = input(width of room)
roomHeight = input(height of room)
numDoors = input(number of doors)
numWindows = input(number of windows)

wallArea = (roomLength * roomHeight * 2) + (roomWidth * roomHeight * 2)
wallArea = (wallArea - ((areaDoor * numDoors) + (areaWindow * numWindows)))
numRolls = (Your math with wallArea variable)



That will get your rolls to a 'per room' stand point and cut your measuring down a bit, then your loop can be the same, you will just need to declare an array before the loop, and each time you go through the loop, you can use the array[].add function to add the calculated value to your array. Once you receive number=2 you leave the loop, then you'll have to loop one more time through each element in your array and add each of those values together (you could save this last step if you dont use an array and just add each calculation to an integer variable as you go and output this value at the end) however without the array, you don't have the option of something such as

for each in array
output("Room 1: " + array[item] + " rolls.")
total = total + array[item]
next item
output("Total: " + total + " rolls.")



given you may not be covering every room in the same paper ;) hope this helps with the actual question without making you think of too much additional stuff along the way.
Was This Post Helpful? 1
  • +
  • -

#4 josephine11  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 01-September 12

Re: Help with multiple inputs

Posted 09 March 2013 - 08:48 PM

View PostGregBrannon, on 09 March 2013 - 01:53 AM, said:

Please write better thread titles that describe the topic or the general area in which help is needed. Everyone needs "Help..."

Follow Java naming conventions: Class titles are capitalized.

Your program functions properly the way it is. You'll have to provide more details about what else you need it to do in order to get help.

Hi GregBrannon, you are right, in fact I wanted to change the title, but I wasn't sure how to. I am trying to create a program that calculates the number of rolls of wallpaper needed. Once the measurements of each wall in the room has been entered it should calculate the number of rolls.

Thanks
J

View PostTakk, on 09 March 2013 - 02:33 AM, said:

Looking at your code, it looks like you're actually calculating the number of rolls needed 'per wall' instead of 'per room', from that assumption, every time you input a length and a height, you will get feedback telling you how many rolls you will need for that particular wall, you'll have to write that down and do the next wall.. with this method, you would generally do every wall, keep a list of the outputs, and add them all together..

to put together a little bit of psuedo for you to consider on how I would go about this, which may lead you to your answer, and likely a bit of brain pain..

areaDoor = (3*7) // Standard door area?
areaWindow = (3.5*4.5) //Standard window area?

roomLength = input(length of room)
roomWidth = input(width of room)
roomHeight = input(height of room)
numDoors = input(number of doors)
numWindows = input(number of windows)

wallArea = (roomLength * roomHeight * 2) + (roomWidth * roomHeight * 2)
wallArea = (wallArea - ((areaDoor * numDoors) + (areaWindow * numWindows)))
numRolls = (Your math with wallArea variable)



That will get your rolls to a 'per room' stand point and cut your measuring down a bit, then your loop can be the same, you will just need to declare an array before the loop, and each time you go through the loop, you can use the array[].add function to add the calculated value to your array. Once you receive number=2 you leave the loop, then you'll have to loop one more time through each element in your array and add each of those values together (you could save this last step if you dont use an array and just add each calculation to an integer variable as you go and output this value at the end) however without the array, you don't have the option of something such as

for each in array
output("Room 1: " + array[item] + " rolls.")
total = total + array[item]
next item
output("Total: " + total + " rolls.")



given you may not be covering every room in the same paper ;)/> hope this helps with the actual question without making you think of too much additional stuff along the way.


Ok, this is so helpful, and I can now see a little clearer... I shall study and play around with the code a little more... If I get stuck again...can I ask???
Was This Post Helpful? 0
  • +
  • -

#5 josephine11  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 01-September 12

Re: Help with multiple inputs

Posted 09 March 2013 - 10:27 PM

Ok, I have a question. Let's say once the program receives the input of the length, width etc. these parameters are then saved into parallel arrays, from which I than do my calculations, could this be possible? If so could you give me an example of a parallel array within a calculation?
Was This Post Helpful? 0
  • +
  • -

#6 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2204
  • View blog
  • Posts: 5,236
  • Joined: 10-September 10

Re: Help with multiple inputs

Posted 10 March 2013 - 04:00 AM

Of course, what you describe is possible, but is that the assignment, or did you decide on that approach yourself? Friends don't let friends program with parallel arrays, unless they're being forced to by an assignment that was likely crafted to demonstrate the evils of parallel arrays.
Was This Post Helpful? 2
  • +
  • -

#7 josephine11  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 01-September 12

Re: Help with multiple inputs

Posted 10 March 2013 - 10:02 AM

Oh, it's like that parallel arrays! >.< The actual assignment does not specify how I reach the output. All I am being asked to do is make a program that estimates how many rolls of wallpaper will be needed for a room and then calculate the cost.

I have reworked the code a little, this time I have separated into methods a little more.

However, my output is putting in the calculations, its giving me just 0 values.. :(
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
import java.text.DecimalFormat;
 

public class WallpaperEstimatorProject extends JFrame implements ActionListener
{//begin public class WallpaperEstimatorProject extends JFrame implements ActionListener

	//construct components
	JTextPane displayPane = new JTextPane();
	DecimalFormat format = new DecimalFormat("######.##");
	
	//declare and intialize data
	boolean firstRun = true;
	int arrayLength;
	double [] totalSquareFootage = new double[50];
	String strNumberWalls; 
	int numberWalls; 
	String strWallLength; 
	double wallLength;
	String strWallHeight; 
	double wallHeight; 
	String strRollCost; 
	double rollCost;
	int wallNumberCounter = 1; 
	double totalArea = 0;
	double totalWallpaperCharge =0;
		//construct instance
		public WallpaperEstimatorProject()
		{
			super("Wallpaper Estimator");
	
		}

		public Container createContentPane()
		{// begin public Container createContentPane()
			
			//construct and populate main panel
			JPanel mainPanel = new JPanel();
			mainPanel.setLayout (new FlowLayout());
			displayPane = addTextToTextPane();
			JScrollPane scrollPane = new JScrollPane(displayPane);
			scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
			scrollPane.setWheelScrollingEnabled(true);
			scrollPane.setPreferredSize(new Dimension(190, 300));
			mainPanel.add(scrollPane);
			Container c = getContentPane();
			c.setLayout(new FlowLayout());
			c.add(mainPanel);
		
		return c;
		} //end public Container createContentPane()
		
		public JTextPane addTextToTextPane()
		{//Begin public JTextPane addTextToTextPane() 
		
			Document doc = displayPane.getDocument();
			
			try
			{
				//The following lines generate JTextPane attributes on-the-fly
				//to simplify adding text to the JTextPane
				StyleContext style = StyleContext.getDefaultStyleContext();
				AttributeSet set = style.addAttribute(SimpleAttributeSet.EMPTY,StyleConstants.ALIGN_LEFT,style);
				displayPane.setParagraphAttributes(set,false);
				
				//clear previous text
				doc.remove(0,doc.getLength());
		
				for(int lcv = 0; lcv < arrayLength; lcv++)
				{
					doc.insertString(doc.getLength(), String.valueOf(totalSquareFootage[lcv])+ "\n" ,set);}
				
					if(firstRun)
						System.out.println("Program Started.");
					else
					{
						double calculate = calculate();
						doc.insertString(doc.getLength(),"Number of wallpaper rolls needed: " + String.valueOf(format.format(calculate))+ "\n",set);
						doc.insertString(doc.getLength(),"Total cost of wallpaper: " +(calculate() * rollCost)+ "\n",set);}
						firstRun = false;
					}
			catch(BadLocationException ble)
			{
				System.err.println("Couldn't insert measurements.");
			}
			return displayPane;
		}//end public JTextPane addTextToTextPane() 
		
		public JMenuBar createMenuBar()
		{ // begin public JMenuBar createMenuBar()
		 
			// Create an instance of the menu
			JMenuBar mnuBar = new JMenuBar();
			setJMenuBar(mnuBar);
			
			//Construct and add the File menu
			JMenu mnuFile = new JMenu("File");
				mnuFile.setMnemonic(KeyEvent.VK_F);
				mnuFile.setDisplayedMnemonicIndex(0);
				mnuBar.add(mnuFile);
				
			//Construct and add the menu command to add grades
			JMenuItem mnuFileNewGrades = new JMenuItem("Enter New Measurements");
				mnuFileNewGrades.setMnemonic(KeyEvent.VK_W);
				mnuFileNewGrades.setDisplayedMnemonicIndex(0);
				mnuFile.add(mnuFileNewGrades);
				mnuFileNewGrades.setActionCommand("NewMeasurements");
				mnuFileNewGrades.addActionListener(this);
			
			//Construct and add the Exit Command
			JMenuItem mnuFileExit = new JMenuItem("Exit");
				mnuFileExit.setMnemonic(KeyEvent.VK_X);
				mnuFileExit.setDisplayedMnemonicIndex(1);
				mnuFile.add(mnuFileExit);
				mnuFileExit.setActionCommand("Exit");
				mnuFileExit.addActionListener(this);
			
			return mnuBar;
		} // end public JMenuBar createMenuBar()
		
		public void actionPerformed(ActionEvent e)
		{ //begin public void actionPerformed(ActionEvent e)
		
			String arg = e.getActionCommand();
			
			if(arg == "NewMeasurements")
			{//begin if new measurement is clicked
			
				//Resets the array so no values get accidentally calculated
				for(int lcv = 0 ; lcv < totalSquareFootage.length; lcv++)
				totalSquareFootage[lcv] = 0.0;
			
				//receives number of walls to calculate
				strNumberWalls = JOptionPane.showInputDialog("Please enter the number of walls to wallpaper." ); 
				numberWalls = Integer.parseInt(strNumberWalls); 
			
				//receives measurement and price inputs until counter reaches specified number of walls
				while (wallNumberCounter<= numberWalls) 
				{ 
					 
					strWallLength = JOptionPane.showInputDialog("Please enter length in square feet for the room # " + wallNumberCounter ); 
						wallLength = Double.parseDouble (strWallLength); 
					strWallHeight = JOptionPane.showInputDialog("Please enter height in square feet for the room # " + wallNumberCounter );
						wallHeight = Double.parseDouble (strWallHeight);
					totalSquareFootage[wallNumberCounter] = (wallLength * wallHeight); //calculate wall's area and holds result to the array
					strRollCost = JOptionPane.showInputDialog("Please enter the cost per roll of the wallpaper you have selected." ); 
						rollCost = Double.parseDouble(strRollCost); 
					wallNumberCounter = wallNumberCounter + 1;//increment wallNumberCounter 	
				} 
				
			addTextToTextPane();
			}//end if NewMeasurement is clicked
		
			//if exit is clicked 
			if(arg == "Exit")
			{
				System.out.println("Ciao Ciao.");
				System.exit(0);
			}
		} // End public void actionPerformed(ActionEvent e)
		
		public double calculate()
		{
			//sum the values of the array
			double sum = 0, calculate;
			int totalCount = 0;
		
				for(int lcv = 0 ; lcv < arrayLength; lcv++)
				{
					if(totalSquareFootage[lcv] > 0)
						sum += totalSquareFootage[lcv];
				}

			// calculate rolls	
			double totalWallpaper = ((totalArea *.10)+ totalArea);
			double numberOfRolls = (totalWallpaper / 35);
			double totalNumberOfRolls = Math.ceil(numberOfRolls);
		
			calculate = totalNumberOfRolls;
		return calculate;
		}
		
		//main method executes at run time
		public static void main(String[] args)
		{ //begin public static void main(String[] args)
			WallpaperEstimatorProject m = new WallpaperEstimatorProject();
			m.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			m.setJMenuBar( m.createMenuBar());
			m.setContentPane( m.createContentPane());
			m.setSize(200, 375);
			m.setVisible(true);
			JOptionPane.showMessageDialog(null, "To calculate, Hit File >> Enter New Measurements.\n" +
			"To end input, enter -1 in the grade value box.",
			"Howdy", JOptionPane .INFORMATION_MESSAGE);
		} //end public static void main(String[] args)

}//end public class WallpaperEstimatorProject extends JFrame implements ActionListener


Was This Post Helpful? 0
  • +
  • -

#8 Takk  Icon User is offline

  • D.I.C Head

Reputation: 40
  • View blog
  • Posts: 111
  • Joined: 08-March 13

Re: Help with multiple inputs

Posted 10 March 2013 - 07:54 PM

Way to bulk the code up overnight hah, just one suggestion I noticed while going through this, I would make sure your variable names are not the same as method names, for sake of saving yourself some hair pulling if you miss a () after them.

That being said, in your calculate function above, when you're looping to arrayLength, arrayLength is 0 because it has not been assigned a value, you're looking for totalSquareFootage.length here, and below your math in the same function, you'll need to assign totalArea = sum; before you continue your calculations. You'll have to test a few cases here as I only did one wall to find the issue, there may still be bugs elsewhere.

As a general suggestion, and I apologize if I'm incorrect given I dont know java specifically... I wouldn't necessarily always put values in global variables then call a function with no parameters, it makes debugging a bit more stressful than it has to be on you. My suggestion would be something along the lines of

overAllArea += calculate(height, length, cost)

hope this helps.. keep at it.
Was This Post Helpful? 0
  • +
  • -

#9 josephine11  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 01-September 12

Re: Help with multiple inputs

Posted 10 March 2013 - 08:20 PM

Ok, I am not sure if it's my accumulative late nights, but I am having a hard time visualizing what you are trying to explain. Could you put into an example for me? I'm pretty sure you mean here
for(int lcv = 0; lcv < arrayLength; lcv++)
				{
					doc.insertString(doc.getLength(), String.valueOf(totalSquareFootage[lcv])+ "\n" ,set);}
				
					if(firstRun)
						System.out.println("Program Started.");
					else
					{
						double calculate = calculate();
						doc.insertString(doc.getLength(),"Number of wallpaper rolls needed: " + String.valueOf(format.format(calculate))+ "\n",set);
						doc.insertString(doc.getLength(),"Total cost of wallpaper: $" +(calculate() * rollCost)+ "\n",set);}
						firstRun = false;
					}



and here
public double calculate()
		{
			//sum the values of the array
			double sum = 0, calculate;
			int totalCount = 0;
		
				for(int lcv = 0 ; lcv < arrayLength; lcv++)
				{
					if(totalSquareFootage[lcv] > 0)
						sum += totalSquareFootage[lcv];
				}

			// calculate rolls	
			double totalWallpaper = ((totalArea *.10)+ totalArea);
			double numberOfRolls = (totalWallpaper / 35);
			double totalNumberOfRolls = Math.ceil(numberOfRolls);
		
			calculate = totalNumberOfRolls;
			
		return calculate;
		}


Was This Post Helpful? 0
  • +
  • -

#10 josephine11  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 01-September 12

Re: Help with multiple inputs

Posted 10 March 2013 - 08:48 PM

Ok, I think I have figured what you are trying to tell me. Now the code works and outputs the correct price, but still does not output the number of rolls needed for the job, any suggestions?

Ok, I think I have figured what you are trying to tell me. Now the code works and outputs the correct price, but still does not output the number of rolls needed for the job, any suggestions?
Was This Post Helpful? 0
  • +
  • -

#11 Takk  Icon User is offline

  • D.I.C Head

Reputation: 40
  • View blog
  • Posts: 111
  • Joined: 08-March 13

Re: Help with multiple inputs

Posted 10 March 2013 - 08:53 PM

I actually didn't even catch your first instance there, which may lead to some other issues when you work with more than one room and need to be approached separately, but what I did was..

Line 7: for(int lcv = 0 ; lcv < totalSquareFootage.length; lcv++)

arrayLength was =0, so it was never adding anything to the sum variable

Line 12: totalArea = sum;

My inputs were rooms=1, height=10, length=10, cost=1
the output was rolls: 4, cost: 4

My inputs were rooms=1, height=15, length=25, cost=1
the output was rolls: 12, cost: 12

I was admittedly a bit lost in the flow of everything, but was able to get two correct results for a single room, I'm pretty sure if you get the top instance resolved also, you should have what you were after.

After looking back at the code, arrayLength is never set while your rooms are being calculated, despite my changes you may be able to update arrayLength correctly and have everything fall together but I haven't done any checking past that. Hope this helps some.
Was This Post Helpful? 1
  • +
  • -

#12 josephine11  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 01-September 12

Re: Help with multiple inputs

Posted 10 March 2013 - 09:03 PM

Yes It working with those corrections. My assignment was very vague, I was perhaps making it more complicated than it actually is. Thanks for your help... I can definitely see Java a little better :)
Was This Post Helpful? 0
  • +
  • -

#13 Takk  Icon User is offline

  • D.I.C Head

Reputation: 40
  • View blog
  • Posts: 111
  • Joined: 08-March 13

Re: Help with multiple inputs

Posted 10 March 2013 - 09:40 PM

Great :) glad to help
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1