aspose file tools*
The moose likes Beginning Java and the fly likes Populating a string array using a for loop Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Populating a string array using a for loop" Watch "Populating a string array using a for loop" New topic
Author

Populating a string array using a for loop

O Tosa
Greenhorn

Joined: Apr 16, 2008
Posts: 3
Hi,
I have the following code below but i need to populate a string array dynamically(size and content are defined by the user of the program) but i'm having difficulty with the assignment statement. i.e

Snippet..
int[] numbers = new int[size];
for(int i=0; i<numbers.length; i++){
numbers = readInt("? ");
}
for(int i=0; i<numbers.length; i++){
names = readString("? "); // portion fails
}



Does anyone know how to populate a string array dynamically using a for loop?
[ May 19, 2008: Message edited by: O Tosa ]
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3455
    
  12
In what way does it fail ? The code looks fine assuming that the readString method returns a String.
[ May 19, 2008: Message edited by: Joanne Neal ]

Joanne
O Tosa
Greenhorn

Joined: Apr 16, 2008
Posts: 3
The readString method does not really exist. for example, we have methods that read in the input of the user into number variables..
for acm we have readInt(), readDouble(), etc
for java we have nextInt(), nextDouble(), etc.

but there is no provision for reading in a string value to a variable.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14114
    
  16

Those readInt() etc. methods are implemented in one of the superclasses. The class ConsoleProgram is not a class from the standard Java API, so I can't tell you how to use it.

A quick Google search for "ConsoleProgram" found the API documentation for acm.program.ConsoleProgram - that seems to be the class you're using. Note that according to that documentation there is indeed no readString() method, but there is a readLine() method. You could try using that.

Note about the following line in your program:

using acm.program.*;

This is not valid Java code. There is no "using" statement in Java. You probably meant:

import acm.program.*;
[ May 19, 2008: Message edited by: Jesper Young ]

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
O Tosa
Greenhorn

Joined: Apr 16, 2008
Posts: 3
Thanks a lot, using readLine worked!

About the using statement, that was a typo(i am used to the using statement from c#)
 
jQuery in Action, 2nd edition
 
subject: Populating a string array using a for loop