• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Populating a string array using a for loop

 
O Tosa
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
O Tosa
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15216
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
O Tosa
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot, using readLine worked!

About the using statement, that was a typo(i am used to the using statement from c#)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic