Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

initialising String variables in an array  RSS feed

 
Daniel .J.Hyslop
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all,

I have tried to set up an array to initialise String variables in a for loop .The String variables after being initialised with their values print out fine but when I pass them to an objects(VehicleRecord) constuctor no values are passed . I have then declared and initialised a string , reinitialised it with JOptionPane and passed this to the constuctor and it does pass the value . Could someone please explain what I am obviously missing here , her`s the code :

thanks danny

[ September 15, 2005: Message edited by: Daniel .J.Hyslop ]
[ September 15, 2005: Message edited by: Daniel .J.Hyslop ]
 
Jeanne Boyarsky
author & internet detective
Sheriff
Posts: 37242
519
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daniel,
Strings are special immutable objects. If you assigned a value to b it would be set. But when you set abc[1], it sets the array value and does not carry back to the original variable (b.)
 
Mark Spritzler
ranger
Sheriff
Posts: 17309
11
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes you are just replacing the objects in the Array, b c d and e are all still null.


Meaning abc[0] is set to a
abc[1] is set to b.

But when you are calling the JOptionPane in the loop and setting

abc[i] = String returned by JOptionPane you are not setting b c d or e to the String returned by JOptionPane

so setting abc[1] to "Hello" is not the same as

b = "Hello"

because you are setting abc[1] and not b in that line of code.

Mark
 
Daniel .J.Hyslop
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks guys ,
understood , I have just changed my code sligthly to see how your explanation works I changed the string initialisation
instead of a = "ett"
b = null etc....
abc [i] = JOptionPane . etc

changed it to
a = "ett";
b = ""; etc
abc[i] += JOptionPane etc
This did work concanating the strings with values returned from the JOptionPane , the ouput was

ettFord
capri
 
Mark Spritzler
ranger
Sheriff
Posts: 17309
11
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you understand our points, however, I am still confused as to your approach to the code and why you are using both variables a, b, etc and an array.

Your Code



could be be written

[CODE
String[] abc = new String[5];
abc[1] = "ett";

for(int i = 0 ; i < abc.length ; i++) {
//set initialised strings with JOptionPane
abc[i] = JOptionPane.showInputDialog("please enter value");
}
[/CODE]

then just change this line

//initialise other object with values
VehicleRecord v = new VehicleRecord( a , b, c, d , e);

to

VehicleRecord v = new VehicleRecord(abc[0], abc[1], abc[2], abc[3], abc[4]);

or better yet, if you can change the VehicleRecord's constructor to take an
array parameter instead, and inside the constructore loop through the array
setting the instance variables of VehicleRecords one by one.

Mark
 
Daniel .J.Hyslop
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi mark ,
You are correct to say the code that I have written is a bit overweight , and it is. I was still having a bit of difficulty in understanding the concept of strings as immutable objects as apposed to StringBuffer ,which I have used in the actual test program for my tutor assignment.Strings cannot be changed after they are created and initialised . I was testing the behaviour of strings in different circumstances of usage to see what happens . The immutable fact of Strings is not really where my problem lies ,I still have a little doubt as to what effect it has on code and what the outcome will be when used in certain areas.Hopefully this will come with practice, thanks for the advice , as usual all ranchers come in with very good solutions to beginners problems, which I am still.
thanks
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!