Help with program (sorry im a noob)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • baden
    New Member
    • Apr 2007
    • 6

    Help with program (sorry im a noob)

    I can't figure out why 'display' in the main method wont show in my output. Any help will be appreciated.
    Code:
       import javax.swing.JOptionPane;
        public class lab006 {
       
         
             	
           public static void main  (String [ ] args) {
             int numRoom;
             numRoom = number();
             String display="";
             int numberOfRooms = 0;
             int length=0;
             int width=0;
             String name="";
             int area=0;
             int totalArea=0;
          
             for(int i = 0; i < numRoom; i++) {
               
                name = JOptionPane.showInputDialog("Enter name of room");
               
                do {
                   width =Integer.parseInt(JOptionPane.showInputDialog("The width of " + name + " --in feet")); 
                } while(width <= 0 || width >= 30);     
                do {
                   length =Integer.parseInt(JOptionPane.showInputDialog("The length of " + name + " --in feet")); 
                } while(length <= 0 || length >= 30);     
                area=length*width;
                totalArea+=area;
             
                display+= name + area + "\n\n\n";
             }
             finalOutput(display, totalArea);
          
          }
       
       
           public static int number () {
             int numberOfRooms=0;
             do {
                numberOfRooms = Integer.parseInt(JOptionPane.showInputDialog("How many rooms in the house?"));//display message box to get the number of rooms
             } while (numberOfRooms >= 10 || numberOfRooms <= 0);
             return numberOfRooms;
          }
       
            
           public static void finalOutput(String output, int totalArea)
          {
          
             output="Room          |        Area\n";
          
             output += "Total Square Footage: " + totalArea  + " square feet";
          
             JOptionPane.showMessageDialog(null, output);
          
          }
       }
    Last edited by JosAH; Apr 24 '07, 06:09 AM. Reason: added [code] tags
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    You carefully build up the display string here:
    Code:
             
                display+= name + area + "\n\n\n";
    Finally you pass it to yout finalOutput method where you do this:
    Code:
           public static void finalOutput(String output, int totalArea)
          {
          
             output="Room          |        Area\n";
    This means that you simply discard the previous content of the string. Get it?

    kind regards,

    Jos

    Comment

    • r035198x
      MVP
      • Sep 2006
      • 13225

      #3
      Originally posted by baden
      I can't figure out why 'display' in the main method wont show in my output. Any help will be appreciated.
      Code:
      import javax.swing.JOptionPane;
      public class lab006 {
       
       
       
      public static void main (String [ ] args) {
      int numRoom;
      numRoom = number();
      String display="";
      int numberOfRooms = 0;
      int length=0;
      int width=0;
      String name="";
      int area=0;
      int totalArea=0;
       
      for(int i = 0; i < numRoom; i++) {
       
      name = JOptionPane.showInputDialog("Enter name of room");
       
      do {
      width =Integer.parseInt(JOptionPane.showInputDialog("The width of " + name + " --in feet")); 
      } while(width <= 0 || width >= 30); 
      do {
      length =Integer.parseInt(JOptionPane.showInputDialog("The length of " + name + " --in feet")); 
      } while(length <= 0 || length >= 30); 
      area=length*width;
      totalArea+=area;
       
      display+= name + area + "\n\n\n";
      }
      finalOutput(display, totalArea);
       
      }
       
       
      public static int number () {
      int numberOfRooms=0;
      do {
      numberOfRooms = Integer.parseInt(JOptionPane.showInputDialog("How many rooms in the house?"));//display message box to get the number of rooms
      } while (numberOfRooms >= 10 || numberOfRooms <= 0);
      return numberOfRooms;
      }
       
       
      public static void finalOutput(String output, int totalArea)
      {
       
      output="Room | Area\n";
       
      output += "Total Square Footage: " + totalArea + " square feet";
       
      JOptionPane.showMessageDialog(null, output);
       
      }
      }
      1.)Please use code tags when posting code
      2.)
      Code:
      output="Room | Area\n";
      sets the variable output to ="Room | Area\n". Any value that was previously in output is thrown away.

      Comment

      • baden
        New Member
        • Apr 2007
        • 6

        #4
        I get what you guys are saying but how would I go about fixing it.

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Originally posted by baden
          I get what you guys are saying but how would I go about fixing it.
          How about changing this line:
          Code:
          output="Room          |        Area\n";
          to this:
          Code:
          output="Room          |        Area\n"+output;
          kind regards,

          Jos

          Comment

          • baden
            New Member
            • Apr 2007
            • 6

            #6
            Thanks Got IT!

            Comment

            Working...