KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
Possible Duplicate: Declaring an array of unknown size I'm working in Java and I am trying to input a sentence into a string array. I am tokenizing it and determining the word count. However, I need to add each word into a string array in order to determine if there are duplicates or not. I am not sure how to initialize my array if I don't know the word count until later in the program. //Declares variables Scanner scan = new Scanner (System.in); int withoutdup = 0, wordCount = 0; String line, word; StringTokenizer tokenizer; List<String> sentence = ArrayList<String>; //Asks user for input System.out.println ("Please enter text. Enter DONE to finish."); line = scan.next(); //Tokenizes the string and counts the number of character and words while (!line.equals("DONE")) { tokenizer = new StringTokenizer (line); while (tokenizer.hasMoreTokens()) { word = tokenizer.nextToken(); wordCount++; sentence += word; } line = scan.next(); }
Tags (comma-separated)
Save Edits
Cancel