error adding single element into structure array java -
i new java , having issue when trying add together single element construction type array. have array setup so: public flat availableroom[] = new apartment[1];
main calls method initializes application launches:
availableroom[0] = new apartment(150, 2, 200.00,null); //this sets default values room#, beds, price, , invitee
my constructor takes info so
public apartment(int roomnum, int beds, double price, string invitee ) { this.roomnumber = roomnum; this.roombeds = beds; this.nightlyfee = price; this.roomguest = guest; }
where having issues when trying assign invitee room. trying availableroom[i].roomguest = name
name entered user , set 0 (i checked). no errors when go print info room returns every value 0 , invitee null. can see doing wrong? (fyi flat separate class main)
main
public class apartmentmain { static flat 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; }
well,
no errors when go print info room returns every value 0 , invitee null.
i think , setting values in diffrent object , printing different one. if paste how printing values may help lot.
things consider
since getting default values(that instance variables if don't assign any), means actual object there in array, instantiated not initialized. take close object printing , 1 setting values in. it quite possible @ first index, newly instantiated object inserted somehow replaced original one. you printing different object original 1 ....a possibility. you may want rid of "no argument constructor" improve actual seen of problem. seek it..its worth. java arrays
No comments:
Post a Comment