java - Incorrectly passing token to string array -


i writing program reads text file passed argument main method , extracts unique words file , prints them in console 1 per line. having trouble passing tokens string array while each line being read scanner:

there's couple things see wrong or written in more efficient manner:

1)tokens initialized 100. obvious constraint, thought using dynamic array arraylist or vector decided use simple string array , expand array (i.e. create new array double size of original array, writing type of conditional statement determine if tokens filled max elements scanner still has more lines.

2)i not sure if passing input.hasnextline() test statement in loop makes sense. want loop long input has reached eof

3) want regex expression in split catch punctuation, whitespaces, , digits, i'm not 100% sure if it's written correctly

4) line in question tokens[index] = token[index], i'm not sure correct. want tokens each line being added 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);          (int index = 0; input.hasnextline(); index++) {             string[] token = input.nextline().split("[.,;']+\\d +\\s");             tokens[index] = token[index];         }         (string token : tokens) {             system.out.println(token);         }         input.close();     } catch (filenotfoundexception e) {         e.printstacktrace();     } } 

there several errors in code, i'll try cover of them:

  1. change tokens arraylist, there no reason not to
  2. you need 2 iterations: a) lines in file , b) tokens in line
  3. the regex specific of have between tokens (punctuations + 1 digit + spaces + other space)

    public static void main(string[] arg) throws filenotfoundexception {     file textfile = new file(arg[0]);     arraylist<string> tokens = new arraylist<string>();      try {         scanner input = new scanner(textfile);          while (input.hasnextline()) {             string[] linetokens = input.nextline().split("[,;:\"\\.\\s]+");             (string token : linetokens) {                 tokens.add(token);             }         }         (string token : tokens) {             system.out.println(token);         }         input.close();     } catch (filenotfoundexception e) {         e.printstacktrace();     } } 

the regex can improved depends on data anyway can't know cases need handle.


Comments

Popular posts from this blog

tcpdump - How to check if server received packet (acknowledged) -