简体   繁体   中英

Why does my for loop adds an object simultaneously?

I have this application which has an array element of 4. I'm required to add each single Room object into the array when input by the user and he/she can input up to 4 objects. But when i only input one Room object, it shows tat all the elements in the array got added by the object. May i know why and can someone please help? Thanks!

import java.util.Arrays;
import java.util.Scanner;
import javax.swing.JOptionPane;

class TestRoom {

public static void main(String [] args)
{
    String[] roomsInHouse = new String[4];
    int answer;

    do{
    String nameOfRoom = JOptionPane.showInputDialog(null, "Enter the name of the room");
    String dimsOfRoom = JOptionPane.showInputDialog(null, "Enter the dimension of the room in feet L X W X H");

    Scanner userInput = new Scanner(dimsOfRoom);
    double l = userInput.nextDouble();
    double w = userInput.nextDouble();
    double h = userInput.nextDouble();

    Room roomDetails = new Room(nameOfRoom, l, w, h);

    String n = Double.toString(l);
    String o = Double.toString(w);
    String p = Double.toString(h);

    for(int i = 0; i < roomsInHouse.length; i++)
    {
        roomsInHouse[i] = nameOfRoom + dimsOfRoom;
    }   

    answer = JOptionPane.showConfirmDialog(null, "Input another room?", "Area/Volume calculator", JOptionPane.YES_NO_CANCEL_OPTION);

    } while(answer == JOptionPane.YES_OPTION);

    for(String j: roomsInHouse)
    {
        System.out.println(j);
    }
}

}

Ok I have just changed my coding and thanks to you guys' contributions, I understand what went wrong. Cheers!

int counter = 0;

roomsInHouse[counter++] = nameOfRoom + dimsOfRoom;  

answer = JOptionPane.showConfirmDialog(null, "Input another room?", "Area/Volume calculator", JOptionPane.YES_NO_CANCEL_OPTION);

} while(answer == JOptionPane.YES_OPTION && counter < 4);

If I get you question right, you are saying that your array gets full when the user inputs just 1 room. If that's the case I think that.

 for(int i = 0; i < roomsInHouse.length; i++)
    {
    roomsInHouse[i] = nameOfRoom + dimsOfRoom;
    }  

Is your problem, because it is looping through all the array.

Right here you are filling up the whole array with a single room. roomsInHouse.length will always return 4

for(int i = 0; i < roomsInHouse.length; i++)
{
    roomsInHouse[i] = nameOfRoom + dimsOfRoom;
}

You can change your code accordingly

String[] roomsInHouse = new String[4];
int n = 0;
int answer;

and then later:

roomsInHouse[n++] = nameOfRoom + dimsOfRoom;

After that just check if n < 4 , which means that more rooms can be added.

The mistake is in your for loop with in the do-while loop......

As u are iterating "roomsInHouse.lenght" times for every interation of do-while loop.....the array gets rewritten for every interation....

Do the following correction to your code....

import java.util.Arrays;
import java.util.Scanner;
import javax.swing.JOptionPane;

class TestRoom {

public static void main(String [] args)
{
String[] roomsInHouse = new String[4];
int answer;
int counter=0;
do{
String nameOfRoom = JOptionPane.showInputDialog(null, "Enter the name of the room");
String dimsOfRoom = JOptionPane.showInputDialog(null, "Enter the dimension of the room in feet L X W X H");

Scanner userInput = new Scanner(dimsOfRoom);
double l = userInput.nextDouble();
double w = userInput.nextDouble();
double h = userInput.nextDouble();

Room roomDetails = new Room(nameOfRoom, l, w, h);

String n = Double.toString(l);
String o = Double.toString(w);
String p = Double.toString(h);


roomsInHouse[counter] = nameOfRoom + dimsOfRoom;

counter++;
answer = JOptionPane.showConfirmDialog(null, "Input another room?", "Area/Volume calculator", JOptionPane.YES_NO_CANCEL_OPTION);

} while(answer == JOptionPane.YES_OPTION);

for(String j: roomsInHouse)
{
    System.out.println(j);
}

}

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM