• 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
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
Bartenders:
  • Himai Minh
  • Carey Brown
  • Frits Walraven

FOR loop in Return type

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Just wondering if it's possible to use a for loop in a Return type.
Similar to:
public static String createTableUsers() {
return "CREATE TABLE USERS (" +
"PRI VARCHAR (15) PRIMARY KEY, " +

for(int i = 0; i < listy.length; i++){
"" + listy[i] + " VARCHAR (30) UNIQUE NULL";
}

"USERID VARCHAR (15) UNIQUE NULL, ";
}
* I get an error with this but hopefully there is a way to do something similar to this.
Thanks in advance.
Yoo-Jin Lee.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, this simply isn't possible - you can't embed a loop inside another statement. It's not difficult to work around this however:
<code><pre> public static String createTableUsers() {
String list = "";
for(int i = 0; i < listy.length; i++){
list += "" + listy[i] + " VARCHAR (30) UNIQUE NULL";
}
return "CREATE TABLE USERS (" +
"PRI VARCHAR (15) PRIMARY KEY, " +
list.toString() +
"USERID VARCHAR (15) UNIQUE NULL, ";
}
</pre></code>
Or better yet:
<code><pre> public static String createTableUsers() {
StringBuffer buffer = new StringBuffer(
"CREATE TABLE USERS (" +
"PRI VARCHAR (15) PRIMARY KEY, ");
for (int i = 0; i < listy.length; i++){
buffer.append(listy[i]);
buffer.append(" VARCHAR (30) UNIQUE NULL");
}
buffer.append("USERID VARCHAR (15) UNIQUE NULL, ");
return buffer.toString();
}
</pre></code>
I suspect there should be some additional spaces or newlines in there too, but that's the basic idea.

[This message has been edited by Jim Yingst (edited June 08, 2000).]
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanx Jim for your response, it was great.
Except for one thing. I get the following error when I run the second code:
Can't make a static reference to a nonstatic variable listy;
buffer.append(listy[i]);
I'm not too sure how to correct this....
Thanx again.
Yoo-Jin Lee

[This message has been edited by Yoo-Jin, Lee (edited June 09, 2000).]
[This message has been edited by Yoo-Jin, Lee (edited June 09, 2000).]
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That is a problem in all the versions of the code listed, if listy isn't static. You don't show the declaration of listy - evidently it's not a static variable, while createTableUsers is a static method. The simplest solutions are to either make both static, or make both non-static. If these solutions create problems elsewhere in your code, you need to determine which methods should really be static, and which should not. (Note that you can call a static variable or method from a non-static method, but you can't do the reverse unless you call through an instance of the appropriate class.)
Also, I'm assuming that listy is an array of Strings. If it's an array of some other object, you'd need
<code><pre> buffer.append(listy[i].toString());</pre></code>
instead. Or if it's an array of primitives, use something like
<code><pre> buffer.append(Integer.toString(listy[i]));</pre></code>
The append method isn't as flexible as + for converting things to strings - it needs a little help.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I just wanted to thank you for your help.
It was extremely quick and very insightful.
You are the best.
Yoo-Jin Lee.
 
reply
    Bookmark Topic Watch Topic
  • New Topic