0

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.

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I was going to implement an ArrayList but knowing the size of the array is not my problem, the problem I'm having is I don't know how many integers I am supposed to be storing, so I don't know when to stop storing them, and when to split them into two separate arrays

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.