Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9301099
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T22:56:16+00:00 2026-06-18T22:56:16+00:00

I am new to java and having an issue when trying to add a

  • 0

I am new to java and having an issue when trying to add a single element into a structure type array. I have my array setup as so: public apartment availableRoom[] = new apartment[1]; My main calls a method that initializes this as soon as the application launches:

availableRoom[0] = new apartment(150, 2, 200.00,null);
//this sets default values for room#, beds, price, and guest

My constructor takes the info like so

public apartment(int roomNum, int beds, double price, String guest )
    { 
        this.roomNumber = roomNum;
        this.roomBeds = beds;
        this.nightlyFee = price;
        this.roomGuest = guest;
    }

Where I am having issues is when I am trying to assign a guest to the room. I am trying it with availableRoom[i].roomGuest = name Name is entered by the user and i is set to 0 (I checked). No errors but when I go to print the information for the room it returns every value as 0 and the guest to null. Can anyone see what I am doing wrong? (FYI Apartment is a separate class from main)

Main

public class apartmentMain {


    static apartment action = new apartment();



    public static void main(String[] args) {

        action.createApt();
        action.addGuest();

apartment.java

public void createApt()
    {
       availableRoom[0] = new apartment(150, 2, 200.00,null);
}

public void addGuest()
{

    name = input.next();
    availableRoom[i].roomGuest = name;

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T22:56:17+00:00Added an answer on June 18, 2026 at 10:56 pm

    well, as you say

    No errors but when I go to print the information for the room it returns every value as 0 and the guest to null.

    I think , you are setting values in diffrent object and printing different one. If you just paste how you are printing its values it may help a lot.

    Things to Consider

    1. Since you are getting default values(that instance variables get if you don’t assign any), that means an actual object is there in your array, that is instantiated but not initialized.
    2. Take a close look which object you are printing and which one you are setting values in.
    3. It is quite possible that at the first index, a newly instantiated object is inserted somehow that replaced the original one.
    4. you are printing a different object than the original one ….a possibility.
    5. You may want to get rid of the “no argument constructor” to better get actual seen of the problem. Try it..its worth.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am having an issue with a String array and trying to add it
hello fellow java developers. I'm having a very strange issue. I'm trying to read
Trying to set up a new spring project and im having this issue where
I'm currently having an issue with my xpath expressions in java. I'm trying to
I am brand new to Java. I am having an issue compiling a basic
I'm new to regex in Java and am having trouble getting it to work
I'm having problems with a Java Regex Pattern Syntax expression. toCensor = toCensor.replaceAll((new StringBuilder(((?i)))).append(word).toString(),
I'm new to Android, and Java altogether for that matter and I'm having a
Good evening, I am having a weird issue with a method I am trying
I seem to be having an issue with Java and NetBeans when it comes

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.