The Artima Developer Community
Sponsored Link

Java Answers Forum
Accessing values of Strings

6 replies on 1 page. Most recent reply: Mar 8, 2007 10:50 PM by Matthias Neumair

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 6 replies on 1 page
Brian Read

Posts: 3
Nickname: breader
Registered: Mar, 2007

Accessing values of Strings Posted: Mar 6, 2007 12:06 PM
Reply to this message Reply
Advertisement
I'm a C/C++ programmer and am now trying to migrate into Java. I'd appreciate any help here!

How do I use the value (or the name) of a string to initialize a variable in java?

I've done the following:

String str1 = "t";
//want to create objects "final Text"
for(int i = 0; i==80; i++)
{
//Concatenate the value of i into str1
str1 += i;
final Text str1 = new Text(s, SWT.SINGLE | SWT.BORDER);
str1 = "t";
}

That "final Text str1" is causing me all kinds of headaches. I was trying *str1, &str1, trying to access the value by using str1.ToString(), etc..

I'd like to name my object Text as t1, t2, t3, t4, etc..

Any help is greatly appreciated!


Vincent O'Sullivan

Posts: 724
Nickname: vincent
Registered: Nov, 2002

Re: Accessing values of Strings Posted: Mar 7, 2007 2:49 AM
Reply to this message Reply
Attempting to declare two variables with the same name "str1" is one problem.

Assigning an undeclared variable to it is a second problem.

I assume you're hitting such very basic problems because your using a text editor rather than an IDE.

VOS

Brian Read

Posts: 3
Nickname: breader
Registered: Mar, 2007

Re: Accessing values of Strings Posted: Mar 7, 2007 8:28 PM
Reply to this message Reply
Thanks for the response.

That second declaration was meant to use the value of str1 to initialize my object Text. I wasn't trying to declare the same name twice. I just put str1 in that spot to demonstrate.

Any thoughts on how I can get the value as the name of that Text object?

Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: Accessing values of Strings Posted: Mar 8, 2007 2:53 AM
Reply to this message Reply
I just have to ask: Why would you want to do that?

During runtime the names of the variables don't matter at all. They're just imoprtant for the programmer during the implementation.

If you don't know the name of your variable, how do you want to access it?

Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: Accessing values of Strings Posted: Mar 8, 2007 2:55 AM
Reply to this message Reply
Ok, ther ARE ways to access a variable through it's name, but in this case you just create 80 variables. Why don't you use an array?

Brian Read

Posts: 3
Nickname: breader
Registered: Mar, 2007

Re: Accessing values of Strings Posted: Mar 8, 2007 8:07 PM
Reply to this message Reply
That's a good point...

An array of Text objects though?

Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: Accessing values of Strings Posted: Mar 8, 2007 10:50 PM
Reply to this message Reply
Yes, that was my idea.

Flat View: This topic has 6 replies on 1 page
Topic: Accessing values of Strings Previous Topic   Next Topic Topic: for loop

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use