By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,360 Members | 2,210 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,360 IT Pros & Developers. It's quick & easy.

Help with program (sorry im a noob)

P: 6
I can't figure out why 'display' in the main method wont show in my output. Any help will be appreciated.
Expand|Select|Wrap|Line Numbers
  1.    import javax.swing.JOptionPane;
  2.     public class lab006 {
  3.  
  4.  
  5.  
  6.        public static void main  (String [ ] args) {
  7.          int numRoom;
  8.          numRoom = number();
  9.          String display="";
  10.          int numberOfRooms = 0;
  11.          int length=0;
  12.          int width=0;
  13.          String name="";
  14.          int area=0;
  15.          int totalArea=0;
  16.  
  17.          for(int i = 0; i < numRoom; i++) {
  18.  
  19.             name = JOptionPane.showInputDialog("Enter name of room");
  20.  
  21.             do {
  22.                width =Integer.parseInt(JOptionPane.showInputDialog("The width of " + name + " --in feet")); 
  23.             } while(width <= 0 || width >= 30);     
  24.             do {
  25.                length =Integer.parseInt(JOptionPane.showInputDialog("The length of " + name + " --in feet")); 
  26.             } while(length <= 0 || length >= 30);     
  27.             area=length*width;
  28.             totalArea+=area;
  29.  
  30.             display+= name + area + "\n\n\n";
  31.          }
  32.          finalOutput(display, totalArea);
  33.  
  34.       }
  35.  
  36.  
  37.        public static int number () {
  38.          int numberOfRooms=0;
  39.          do {
  40.             numberOfRooms = Integer.parseInt(JOptionPane.showInputDialog("How many rooms in the house?"));//display message box to get the number of rooms
  41.          } while (numberOfRooms >= 10 || numberOfRooms <= 0);
  42.          return numberOfRooms;
  43.       }
  44.  
  45.  
  46.        public static void finalOutput(String output, int totalArea)
  47.       {
  48.  
  49.          output="Room          |        Area\n";
  50.  
  51.          output += "Total Square Footage: " + totalArea  + " square feet";
  52.  
  53.          JOptionPane.showMessageDialog(null, output);
  54.  
  55.       }
  56.    }
Apr 23 '07 #1
Share this Question
Share on Google+
5 Replies


Expert 10K+
P: 11,448
You carefully build up the display string here:
Expand|Select|Wrap|Line Numbers
  1.  
  2.             display+= name + area + "\n\n\n";
Finally you pass it to yout finalOutput method where you do this:
Expand|Select|Wrap|Line Numbers
  1.        public static void finalOutput(String output, int totalArea)
  2.       {
  3.  
  4.          output="Room          |        Area\n";
This means that you simply discard the previous content of the string. Get it?

kind regards,

Jos
Apr 24 '07 #2

10K+
P: 13,264
I can't figure out why 'display' in the main method wont show in my output. Any help will be appreciated.
Expand|Select|Wrap|Line Numbers
  1. import javax.swing.JOptionPane;
  2. public class lab006 {
  3.  
  4.  
  5.  
  6. public static void main (String [ ] args) {
  7. int numRoom;
  8. numRoom = number();
  9. String display="";
  10. int numberOfRooms = 0;
  11. int length=0;
  12. int width=0;
  13. String name="";
  14. int area=0;
  15. int totalArea=0;
  16.  
  17. for(int i = 0; i < numRoom; i++) {
  18.  
  19. name = JOptionPane.showInputDialog("Enter name of room");
  20.  
  21. do {
  22. width =Integer.parseInt(JOptionPane.showInputDialog("The width of " + name + " --in feet")); 
  23. } while(width <= 0 || width >= 30); 
  24. do {
  25. length =Integer.parseInt(JOptionPane.showInputDialog("The length of " + name + " --in feet")); 
  26. } while(length <= 0 || length >= 30); 
  27. area=length*width;
  28. totalArea+=area;
  29.  
  30. display+= name + area + "\n\n\n";
  31. }
  32. finalOutput(display, totalArea);
  33.  
  34. }
  35.  
  36.  
  37. public static int number () {
  38. int numberOfRooms=0;
  39. do {
  40. numberOfRooms = Integer.parseInt(JOptionPane.showInputDialog("How many rooms in the house?"));//display message box to get the number of rooms
  41. } while (numberOfRooms >= 10 || numberOfRooms <= 0);
  42. return numberOfRooms;
  43. }
  44.  
  45.  
  46. public static void finalOutput(String output, int totalArea)
  47. {
  48.  
  49. output="Room | Area\n";
  50.  
  51. output += "Total Square Footage: " + totalArea + " square feet";
  52.  
  53. JOptionPane.showMessageDialog(null, output);
  54.  
  55. }
  56. }
1.)Please use code tags when posting code
2.)
Expand|Select|Wrap|Line Numbers
  1. output="Room | Area\n";
  2.  
sets the variable output to ="Room | Area\n". Any value that was previously in output is thrown away.
Apr 24 '07 #3

P: 6
I get what you guys are saying but how would I go about fixing it.
Apr 24 '07 #4

Expert 10K+
P: 11,448
I get what you guys are saying but how would I go about fixing it.
How about changing this line:
Expand|Select|Wrap|Line Numbers
  1. output="Room          |        Area\n";
to this:
Expand|Select|Wrap|Line Numbers
  1. output="Room          |        Area\n"+output;
kind regards,

Jos
Apr 25 '07 #5

P: 6
Thanks Got IT!
Apr 26 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.