-1

i have a file that consist like this

/1/a/a/a/Female/Single/a/a/January/1/1920/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a

and here is my StringTokenizer code:

public void retrieveRecords(){
        String holdStr="";
        try {
            fileRead=new FileReader(fileNew);
            read=new Scanner(fileRead);

            while(read.hasNext()){
                holdStr+=read.nextLine()+"\n";
            }
            read.close();

            StringTokenizer strToken=new StringTokenizer(holdStr, "/");

            while(strToken.hasMoreElements()){
                rows=new Vector();
                for(int i=0; i<column.length; i++){
                    rows.add(strToken.nextElement());
                }
                ViewTablePanel.tblModel.addRow(rows);
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

i actually don't have any idea what is the problem as well as i researched every reason why nosuchelementexception would happened but i am not doing all those reasons why it won't work. any suggestions would be open thank you so much :)

lels
  • 5
  • 2
  • In which line does the exception occur? Also, please read about posting a [Minimal, Complete and Verifiable example](http://stackoverflow.com/help/mcve). – Jezor Sep 09 '16 at 16:03
  • 5
    your while loop calls `hasMoreElements()` but then inside of it you have a for loop that calls `nextElement()` multiple times. Per the javadoc, `nextElement()` may throw NoSuchElementException if there are no more elements. – FatalError Sep 09 '16 at 16:04
  • Please read [How to create a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve). For instance, you could directly create a tokenizer from the string in your code snippet instead of reading from a file. That makes it reproducing much easier. What is `ViewTablePanel`, is it important to reproduce the problem? – Martin Nyolt Sep 09 '16 at 16:04
  • @Jezor in the `rows.add(strToken.nextElement())` – lels Sep 09 '16 at 16:04
  • hey guys thanks i solved the problem now. it's actually my file. but yes thank you so much you actually helped me figure out what's wrong thank you so much!!!!! :) – lels Sep 09 '16 at 16:11
  • @lels It's not your file, it's the code others pointed at. If you don't want that exception to occur again, you need to check if there are more elements in the internal loop. – Jezor Sep 09 '16 at 16:19
  • 1
    @Jezor oh it's in my file. some of the areas doesn't have any text for example like `/1/a/a/a/Female/Single////....` that's why it stop reading and returns the error because some of the areas are blank. but thank you so much anyway!!!! :) – lels Sep 21 '16 at 16:14

2 Answers2

1
    while(read.hasNext()){

          holdStr+=read.nextLine()+"\n";
     }

From the above, Scanner read does not have any tokenizer, hence, read.hasNext() throws NoSuchElementEXception read.hasNextLine() should work fine

Positive
  • 62
  • 5
1

Exception java.util.NoSuchElementException This exception is thrown to indicate that there are no more elements in an tokenizer.

    while(strToken.hasMoreElements()){
        rows=new Vector();
        for(int i=0; i<column.length; i++){
            rows.add(strToken.nextElement()); //Exception will throw on this line
        }
        ViewTablePanel.tblModel.addRow(rows);
    }

In your while loop you have condition for checking more elements using hasMoreElements(). But inside while loop you have for loop which calls nextElement() multiple times. If there is no elements in tokenizer then nextElement() will throw java.util.NoSuchElementException.

Solution :Add some condition in for loop for checking elements in tokenizer.

Abhijeet
  • 4,069
  • 1
  • 22
  • 38