Results 1 to 2 of 2
  1. #1
    alan_ogz83 is offline Member
    Join Date
    Jul 2014
    Rep Power

    Default For loop problem.

    Hey hi to all,

    I have a problem with my application.
    It supposed to store 4 different Room objects but when I entered one only it stores tat object variables into all my Array elements.
    I just need it to store any number of objects as long as it is less than 4.
    Could you guys help me on that please?

    Java 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;
    		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)

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Northern Virginia, United States
    Rep Power

    Default Re: For loop problem.

    The problem is that you enter the information, and then iterate over the loop like this:

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

    That stores the same thing in each of the four array locations. You need to incorporate your input prompts, inside the loop.

    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Need help with Loop Problem
    By jayfaxva in forum New To Java
    Replies: 2
    Last Post: 11-21-2011, 03:33 PM
  2. Replies: 2
    Last Post: 11-07-2011, 02:25 AM
  3. simple line problem / for loop problem
    By helpisontheway in forum New To Java
    Replies: 1
    Last Post: 11-17-2009, 06:12 AM
  4. Help with a loop-like problem
    By Jnoobs in forum New To Java
    Replies: 1
    Last Post: 10-14-2009, 01:15 AM
  5. Loop Problem
    By jralexander in forum New To Java
    Replies: 4
    Last Post: 12-02-2008, 07:08 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts