1

I'm trying to import data from a file to array using string tokenizer.

Data format in file is

AA,BB,CC
AA,BB,CC

But i keep getting error

Exception in thread "main" java.util.NoSuchElementException
    at java.util.StringTokenizer.nextToken(StringTokenizer.java:349)
    at java.util.StringTokenizer.nextElement(StringTokenizer.java:407)
    at main.main(main.java:36)

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.StringTokenizer;


public class main {
public static void main(String[] args) throws FileNotFoundException {

    Scanner input;
    String line;
    int x=0;
    String [] cName = new String [100];
    String [] cMascot = new String [100];
    String [] cAlias = new String [100];





         input = new Scanner(new File("test.txt"));

         while(input.hasNextLine()) {

             line = input.nextLine();
             StringTokenizer strings = new StringTokenizer(line,",");

             while (strings.hasMoreElements()) {
                 cName[x] = strings.nextElement().toString();
                 cMascot[x] = strings.nextElement().toString();
                 cAlias[x] = strings.nextElement().toString();
                 x++;
             }

         }


}

}

So any help would be appreciated. I cant use array list so that out of the context

5
  • 2
    If all lines have this format, why not just use .split()? Commented May 25, 2013 at 13:06
  • 1
    Shouldn't you check for hasMoreElements() before calling to each nextElement() ? Commented May 25, 2013 at 13:08
  • @fge I'm new to java so could tell me how would i go about it? Commented May 25, 2013 at 13:09
  • @the new idiot i do check for more elements "strings.hasMoreElements()" Commented May 25, 2013 at 13:11
  • You can always try debugging... Commented May 25, 2013 at 13:14

3 Answers 3

2

you can't call .nextElement() many times in while statement; befor each of them .hasNextLine() must be called

Sign up to request clarification or add additional context in comments.

2 Comments

Yea. I got that now.. Thanks :)
Did you mean hasMoreElements()?
1

I suggest you use readLine and split ...

public static void main(String[] args) throws FileNotFoundException {

    String line;
    int x=0;
    String [] cName = new String [100];
    String [] cMascot = new String [100];
    String [] cAlias = new String [100];

    try (BufferedReader input = new BufferedReader(new FileStreamReader("test.txt"))) {

         while ((line = input.readLine())!=null) {

             cName[x] = line.split(",")[0];
             cMascot[x] = line.split(",")[1];
             cAlias[x] = line.split(",")[2];
             x++;
         }
    }

}

5 Comments

Quick question: String [] cName = new String [100]; say i dont know if need i need 100 array size, whats the best way to make it dynamic, so if i have 105elements, i can still save them in array
@user2291480 if you need dynamic array then use ArrayList.
Any way without ArrayList?
Hey how do i cast "year[z] = line.split(",")[0];" to int?
@user2291480 Try Integer.parseInt(line.split(",")[0])
0

You can have below code useful too :

public static void main(String[] args) throws FileNotFoundException {

    Scanner input;
    String line;

    String cMascot = null;
    String cAlias = null;
    String cName = null;

    input = new Scanner(new File("test.txt"));
    while (input.hasNextLine()) {
        line = input.nextLine();
        StringTokenizer strings = new StringTokenizer(line, ",");

        while (strings.hasMoreElements()) {
            cName = strings.nextToken();
            cMascot = strings.nextToken();
            cAlias = strings.nextToken();
            System.out.println("cName :" + cName);
            System.out.println("cMascot :" + cMascot);
            System.out.println("cAlias :" + cAlias);
        }
    }

}

1 Comment

Hey, thansk for that but the Strings have to be arrays.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.