Reputation: 33
So I have the following code
StringTokenizer st = new StringTokenizer(sb.toString());
while (st.hasMoreElements())
{
String next = st.nextElement().toString();
int nextInt = Integer.parseInt(next);
agents = Integer.parseInt(next);
pass = Integer.parseInt(next);
TSA[] agentArr = new TSA[agents];
for(int i; i < agents; i++)
{
agentArr[i] = new TSA(next,next,nextInt,nextInt,nextInt,nextInt,nextInt,nextInt,nextInt,nextInt,nextInt);
}
}
Now this works for what I am doing because I was expecting only to have a fixed number of nextInt's that I would be reading from a file. However I realized I would have to read an unknown amount, from two separate lines of unknown length. I cannot figure out how to do this using StringTokenizer, ideally I could check when a new line occured and take the next token as an array but am unable to do this. Any ideas? Also I know that String Tokenizer is deprecated and is not the best way. I am open to using other ways, but still could not get it to work.
Upvotes: 0
Views: 561
Reputation: 100279
Java arrays are fixed size, but you can use ArrayList
class. It stores the data in the array and once it's filled it creates bigger array and copies the data there. Thus you should not care about size, you can just add elements. If you want to convert it back to simple array after finishing, just use toArray()
method.
Upvotes: 1