Morning People

I seem to be having a pretty big issue with a buffer I have that is reading a text file and acting upon the results of the file,

As of now, the buffer reads each token in the file, and stores each token into a variable, this is all working as it should,

It takes the first 8 tokens and stores them then should move onto the next part of the code, but unknown to me it doesn't, it seems to try to read them again and again?

Here is my entire code,

import java.util.*;
import comp100.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.BorderLayout;
import java.awt.Color;
import java.io.*;

public class CartoonAnimator{

    public void animateFromFile(){
	JFrame frame = new JFrame("Cartoon Animation");
	frame.setSize(700, 400);
	DrawingCanvas canvas = new DrawingCanvas();
	frame.getContentPane().add(canvas, BorderLayout.CENTER);
	    File f = new File(FileChooser.open());
		Scanner scan = new Scanner(f);

    	int startX1 = 0;
    	int startY1 = 0;
    	int startX2 = 0;
    	int startY2 = 0;
	    String strName1 ;
	    String strColour1;
	    String strName2;
	    String strColour2;
	    String strStartX1;
	    String strStartY1;
	    String strStartX2;
	    String strStartY2;
	    BufferedReader br = new BufferedReader (new FileReader (f));
	    String curLine = br.readLine();
	    	StringTokenizer Tok = new StringTokenizer (curLine);
	    	strName1 = Tok.nextToken();
	    	strColour1 = Tok.nextToken();
	    	strStartX1 = Tok.nextToken();
	    	strStartY1 = Tok.nextToken();
	    	curLine = br.readLine();
	    	strName2 = Tok.nextToken();
	    	strColour2 = Tok.nextToken();
	    	strStartX2 = Tok.nextToken();
	    	strStartY2 = Tok.nextToken();
	    	startX1 = Integer.parseInt(strStartX1);
	    	startY1 = Integer.parseInt(strStartY1);
	    	startX2 = Integer.parseInt(strStartX2);
	    	startY2 = Integer.parseInt(strStartY2);	
	    	if (!strName1.equals(" ") && !strColour1.equals(" ") && startX1 != 0 && startY1 != 0){	
	    	CartoonFigure cf1 = new CartoonFigure(canvas, strColour1, startX1, startY1);

	    	if (!strName2.equals(" ") && !strColour2.equals(" ") && startX2 != 0 && startY2 != 0){	
		    	CartoonFigure cf2 = new CartoonFigure(canvas, strColour2, startX2, startY2);
	    	curLine = br.readLine();
	while (curLine != null){
		StringTokenizer Tok2 = new StringTokenizer (curLine);
		strName1 = Tok2.nextToken();
		if (Tok2.equals("frown")){
			CartoonFigure cf2 = new CartoonFigure(canvas, "frown", startX2, startY2);
		if (Tok2.equals("smile")){
			CartoonFigure cf2 = new CartoonFigure(canvas, "smile", startX2, startY2);
		if (Tok2.equals("talk")){
			CartoonFigure cf2 = new CartoonFigure(canvas, "talk", startX2, startY2);
		if (Tok2.equals("left")){
			CartoonFigure cf2 = new CartoonFigure(canvas, "left", startX2, startY2);
	    if (Tok2.equals("right")){
	    	CartoonFigure cf2 = new CartoonFigure(canvas, "right", startX2, startY2);
	    if (Tok2.equals("move")){
	    	CartoonFigure cf2 = new CartoonFigure(canvas, "move", startX2, startY2);


	catch(IOException e){System.out.println("File reading failed: "+e);}

    // Main
    public static void main(String[] arguments){
	CartoonAnimator ob = new CartoonAnimator();


What I want it to be doing, is as I said before, read the first 8 tokens in a file, store them into the giving variable's, act on those, then move onto the next line of the text file, But When I run it, it does the first part fine, then I get this error

Sample of text file...

Jim blue 100 150
Jan yellow 400 150
Jim frown
Jim talk hello there,
Exception in thread "main" java.util.NoSuchElementException
	at java.util.StringTokenizer.nextToken(Unknown Source)
	at CartoonAnimator.animateFromFile(CartoonAnimator.java:57)
	at CartoonAnimator.main(CartoonAnimator.java:106)

And that's at

strName2 = Tok.nextToken();

I'm unsure why its going back to that point? I just noticed while trying to debug my code, I cant print anything to the console, No matter where I put a print message, it doesn't seem to run it,

I am still learning the basics and hope I have just missed something simple, Please help!

P.S sorry for the long post... Thought its better to give to much information rather than not enough ;)

6 Years
Discussion Span
Last Post by BestJewSinceJC

Print out the value that "f.exists()" returns after you create the file. So do


Also, why are you opening two methods of reading from the file? You created a Scanner and a BufferedReader on the same file. Are you sure that doesn't cause a problem? I wouldn't know because I've never had cause to try it. But you need to be much more thorough in your methods of testing for errors and not assume that the root of the problem is any particular thing (unless, of course, you happen to already have evidence or knowledge supporting a particular cause)...

For example, have you tried calling countTokens() on the StringTokenizer you created to see how many tokens it has? Also, for lines 49-54, don't you need to construct another StringTokenizer since you reassigned the 'curLine' variable? Strings in java are immutable, so the info that you originally passed to StringTokenizer isn't going to get "updated". IMO, [I've never used StringTokenizer, so take with a grain of salt] you need to create another StringTokenizer every time after you say "curLine = br.readLine()".

Edited by BestJewSinceJC: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.