0

Here' the Class :

public class Rectangle { 

private double lenght ;
private double width ;
public String name ;

public Rectangle (double len, double w, String n)

{
    lenght = len;
     width = w;
     name = n;
}

 public double getLenght()
 {
    return  lenght ;
 }

 public double getWidth ()
 {
     return width ;
 }

 public String getName() 
 {
     return name ;
 }

 public double getArea ()
 {
     return lenght * width  ;
 }

}

And here' s my constructor:

public class roomConstructor {

public static void main(String[] args) {


    double roomLenght, roomWidht,  totalArea;
    String roomName;

    Rectangle kitchen, bedroom, den;

    Scanner keyboard = new Scanner (System.in);
    System.out.println("What is the name of your kitchen? " );
    roomName = keyboard.nextLine();
    System.out.println("What is the lenght of the kitchen ? " );
    roomLenght = keyboard.nextDouble();
    System.out.println("What is the widht of the kitchen ? ");
    roomWidht =keyboard.nextDouble();
    kitchen = new Rectangle(roomLenght, roomWidht, roomName );

    System.out.println("What is the name of your bedroom? " );
    roomName = keyboard.nextLine();
    keyboard.nextLine();
    System.out.println("What is the lenght of the bedroom ? ");
    roomLenght = keyboard.nextDouble();
    System.out.println("What is your bedroom withd ? ");
    roomWidht = keyboard.nextDouble();
    bedroom = new Rectangle(roomLenght,roomWidht, roomName );

    System.out.println("What is the name of your den? " );
    roomName = keyboard.nextLine();
    keyboard.nextLine();
    System.out.println(" What is the lenght of your den ? ");
    roomLenght =keyboard.nextDouble();
    System.out.println("What is the widht of your den ? ");
    roomWidht =keyboard.nextDouble();
    den = new Rectangle ( roomLenght, roomWidht,roomName);

    totalArea = kitchen.getArea() + bedroom.getArea() + den.getArea();

    System.out.println(" The total area of your room is : " + totalArea);
    System.out.println(" The name of your kitchen is : " + kitchen.getName());
    System.out.println(" The name of your kitchen is : " + bedroom.getName());
    System.out.println("The name of your bedroom is : " + den.getName());
    System.out.println("The lenght of the kitchen is : " + bedroom.getLenght());


}

}

When I try to get the bedroom or den name it's missing the all name answer I get is the kitchen then there are 2 blank answers 5lmissing name for bedroom and den) What should I do please ? Thanks for your help

Andy Thomas
  • 78,842
  • 10
  • 93
  • 142
Freddy53
  • 1
  • 4
  • Just a note: your code is full of typos. It's `width` and `length`. Not `widht` and `lenght`. You're alwo abusing the term "constructor", and not respecting the Java naming conventions. – JB Nizet Sep 01 '15 at 16:59
  • I look at the other question and did not fully understood with my question is so duplicate. I am still having the same issue. Thanks for the BIG help in learning java. God the world is soooooooooo rude bad. – Freddy53 Sep 01 '15 at 17:30

1 Answers1

1

Keyboard.nextDouble() does not consume the newLine character. Call Keyboard.nextLine() after each call to Keyboard.nextDouble().

This is throwing off your data storage in the constructors because you're mismatching input when not consuming the \n character

andrewdleach
  • 2,380
  • 2
  • 14
  • 23
  • I have used keyboard.nexLine () because I got 2 questions on the same time :What is the name of your bedroom? " What is the lenght of the bedroom ? so that fixed the issue – Freddy53 Sep 01 '15 at 17:00
  • 1
    `Double.parseDouble(keyboard.nextLine())` is a better option. – Bruno Caceiro Sep 01 '15 at 17:00
  • Andrew thanks it was helpful but I still get the bedroom name missing . Here's the new code :System.out.println("What is the name of your bedroom? " ); roomName = keyboard.nextLine(); keyboard.nextLine(); System.out.println("What is the lenght of the bedroom ? "); roomLenght = keyboard.nextDouble(); System.out.println("What is your bedroom withd ? "); roomWidht = keyboard.nextDouble(); keyboard.nextLine(); bedroom = new Rectangle(roomLenght,roomWidht, roomName ) – Freddy53 Sep 01 '15 at 17:17
  • @Freddy53 you only need to consume the newline character if you invoke any other method than keyboard.nextLine() – andrewdleach Sep 01 '15 at 17:19
  • Understood Andrew but in this example if I don't add keyboard.nextLine() under roomName = keyboard.nextLine(); the program will ask both questions on the same times : What is the name of your bedroom?What is the lenght of the bedroom ? – Freddy53 Sep 01 '15 at 17:24
  • Thanks Andrew I finally got it working . Thanks for your help again , I was about to give up Java , spent the entire day trying to figure what was wrong with the code. – Freddy53 Sep 01 '15 at 18:11
  • you got it @Freddy53 – andrewdleach Sep 01 '15 at 18:12