I am writing a program that reads a text file passed as an argument to the main method and extracts all the unique words from the file and prints them in the console one per line. I am having trouble passing the tokens to a string array while each line is being read from the scanner:
There's a couple things I see that are wrong or could be written in a more efficient manner:
1)tokens is initialized to 100. This an obvious constraint, I thought about using something like a dynamic array like arrayList or vector but ultimately decided to use simple string array and simply expand the array (i.e. create a new array double the size of the original array, by writing some type of conditional statement that will determine if the tokens is filled up with max elements but scanner still has more lines.
2)I am not sure if simply passing input.hasNextLine()
as the test statement in the for loop makes sense. I basically want to loop as long as input has reached EOF
3) I want the regex expression in split to catch all punctuation, whitespaces, and digits, I'm not 100% sure if it's written correctly
4) The line in question is tokens[index] = token[index]
, I'm not sure this correct. I want the tokens from each line being to be added to tokens.
public static void main(String[] arg) throws FileNotFoundException {
File textFile = new File(arg[0]);
String[] tokens = new String[100];
try {
Scanner input = new Scanner(textFile);
for (int index = 0; input.hasNextLine(); index++) {
String[] token = input.nextLine().split("[.,;']+\\d +\\s");
tokens[index] = token[index];
}
for (String token : tokens) {
System.out.println(token);
}
input.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
tokens[index] = tokens[index]
isn't right. You're assigning a variable to itself. – Hovercraft Full Of Eels 39 mins ago99
right? You split your line into tokens, then try to access the array usingindex
which is99
- see a problem here? – Scary Wombat 37 mins ago