• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Referencing string variables through an array

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class Week()
{
Pattern number=Pattern.compile("\\d+\\.?\\d*");
Matcher m;

String sunday;
String monday;
String tuesday;
String wednesday;
String thursday;
String friday;
String saturday;

String []days=new String[7];
}

In one of my methods, I am querying a database to get the hour values for each of the days (i.e. sunday="r8.0d2.0", monday="r6.0d5.0"). I would like to handle it in the following way where days[i] would match the following:
//days[0] correspond to sunday, days[1] to monday, days[2] to tuesday, etc.


for(int i=0;i<7;i++)
{
m=number.matcher(days[i]);
while(m.find())
{
if(i%2==0)
{
double=m.group();
}
else
{
regular=m.group();
}
}
}

I cannot find a way to get days[0] to point at the String object sunday. When sunday changes, days[0] retains the original value of sunday, not the changed one.

I hope I provided enough detail to outline my problem/lack of understanding. Thanks in advance for all that are willing to help!!

JD
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would just use the elements of the array instead of creating the variable Sunday.
 
JD Thompson
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Keith for your reply!

My code is pretty lengthy and there are special rules that apply to Sundays and Saturdays versus the monday thru friday. Being able to reference the day is a help for my simple mind.

I guess a better question would be the following:

String b=new String("hi");
String c="bye";

Object [] a=new String[2];
a[0]=b;
a[1]=c;
System.out.println(a[0].toString());
b="bye";
System.out.println(a[0].toString());

Both lines will output "hi". How do I change the relationships so that the second line of output will be "bye"?

Thanks!!

JD
 
Keith Lynn
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't think you're going to be able to get the change unless you actually use the array reference.

One thing you might look into is using something like a HashMap. It stores keys and values, and when you want to change the value associated with a key, simply add the key and value to the map again, and the original will be replaced.
 
JD Thompson
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Keith, thanks once again for your reply. I will study up on HashMaps and see how to apply.

Take care!

JD
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the problem here is that String objects are immutable. When you assign a new value to the String variable, you are creating a brand new object reference. The array points to the original object reference.

Are you using Java 5? If so, you could make the days of the week a typesafe enum. You could then even encode day-of-week specific behavior on the enumerated values themselves. That seems like it would be more fun.

Cheers,
Rick Goldstein
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic