Win a copy of Rust Web Development this week in the Other Languages forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

initialising String variables in an array

 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
author & internet detective
Posts: 40913
840
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.)
 
ranger
Posts: 17346
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 17346
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic