1

Trying to read a file separated by commas into an array and not sure how to make party into a string to work with tokenizer

for(int i = 0; i < s.length; i++) {
         String str = scan.nextLine();
         StringTokenizer st = new StringTokenizer(str, ",");
        //String [] tokens = str.split(",");
         String name = st.nextToken();
         String abbreviation = st.nextToken();
         long population = Long.parseLong(st.nextToken());
         String govName = st.nextToken();
         char party = st.nextToken();
         int ageWhenElected = Integer.parseInt(st.nextToken());

         s[i] = new State(name, abbreviation, population, govName ,party, ageWhenElected);
Avoras
  • 19
  • 4
  • 1
    I would suggest you to just stick to `.split` as it is easier to use. – Karen Apr 04 '18 at 03:20
  • Possible duplicate of [How to convert/parse from String to char in java?](https://stackoverflow.com/questions/7853502/how-to-convert-parse-from-string-to-char-in-java) – luksch Apr 04 '18 at 03:22
  • 1
    One thing to be careful of is your delimiter. A comma seperated file often contains white space too, so your might want to use something like `new StringTokenizer( str, "\\s*,\\s*" )`. Same goes for `split()` if you switch to that. – markspace Apr 04 '18 at 03:26

2 Answers2

0

You can join your array of string with comma as separator

StringUtils.join(strArray, ",");
Hendri
  • 59
  • 6
0

Try

char party = st.nextToken().charAt(0);
luksch
  • 11,497
  • 6
  • 38
  • 53
  • if my answer is helpful you can show your thanks by upvoting. If my answer resolves your issue please accept it – luksch Apr 04 '18 at 03:27