1

I am trying to create an array that reads string tokens from standard input, and places them in an array, and then prints the words out, until it reaches a specific word. For example, let's say I wanted my array to read a series of words until it reached the word "okay" from std in, print out each word, and then terminate before printing out "okay". The length of this array will be unknown, so I am confused on how to do this.

     String s  = sc.next();
     String[] copy = new String[???];

     for( int i = 0; i < copy.length; i++ ){
           copy[i] = sc.next();     
     }
1
  • Maybe consider using an ArrayList instead, then. Commented Jun 10, 2015 at 2:06

3 Answers 3

2

Something like:

 String s  = sc.next();
 ArrayList<String> copy = new ArrayList<String>();
 while(!s.equals("okay")){
      copy.add(s);
      s = sc.next();
 }

 for (String n : copy){
      System.out.println(n);
 }
Sign up to request clarification or add additional context in comments.

4 Comments

Is there any way to do this without using ArrayList
@sartar not if the length is unknown. You can ask for the length ahead of time, and allocate an array of the predetermined length.
I understand that, however i am only trying to read strings. a user would enter "one two okay" and my program will print out "one two" and terminate. I know how to allocate the size with an int beforehand.
@sartar For this to work as you want, you will have to use a dynamic data structure. Whether this is an ArrayList or a primitive array coupled with a method that expands this array when necessary, it has to scale with the number of inputs.
0

If you don't want to use any list at all, then this becomes impossible. This is simply because array size needs to be defined at the time the array object is created.

So with this constraint you can have a large integer and declare an array of that size.

Final int MY_LARGE_CONST = 3000; ... String[] tokens = new String[MY_LARGE_CONST]...

This is wasteful since it takes more memory and will fail if you have more tokens than the constant.

Alternaely if you are ok with lists and not ok with iterating over that for actual processing, then u can put the tokens in an ArrayList and once they are all collected, call the toArray method on the ArrayList object.

4 Comments

is there any way to count the tokens on the input line? Or parse the number of tokens
For that, either you have to parse the input line twice or parse once and put the tokens in a list or other dynamic data structure.
Worth noting is that it is possible to "expand" a primitive array. By expand I mean creating a new array of larger size, and then copying the values from the original one.
@martin mj, that sounds like my own ArrayList implementation :-)
0

It's my code Without using ArrayList.

import java.util.Scanner;
import java.util.StringTokenizer;
public class Sample {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System. in );
        String line = sc.nextLine();
        StringTokenizer st = new StringTokenizer(line);
        int len = st.countTokens();
        String[] array = new String[len];
        for (int idx = 0; idx < len; idx++) {
            array[idx] = st.nextToken();
        }
        for (String str: array) {
            System.out.println(str);
        }
    }
}

Comments

Your Answer

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