I've current got a file which I'm trying to read and extract data from. I'm using LineNumberReader, StringTokenizer and InputStreamReader to do this.

I've not had any problems for most of the file, however when I get to read the header1 (see below) section of the file I seem to be getting errors. I have written the following code (see below).
When I run it, it prints the first line correctly, altough the next lines mises the first number but prints the following numbers. I'm therefore not sure what I'm doing wrong, also how can I remove the commas?

Thanks in advance for your help.


<< File contents >>

SomeHeader = 334;

# can be random amount of values
header1 = 40.0, 120.0, 50.0, 120.0, 63.0, 120.0, 80.0,
120.0, 100.0, 120.0, 125.0, 120.0, 160.0, 120.0, 200.0,
120.0, 250.0, 118.0, 315.0, 117.0, 400.0, 116.0, 500.0
630.0, 114.0, 800.0, 113.0, 1000.0, 112.0, 1250.0,

header2 = "Blah"

private void extractHeader1()
 {
	StringTokenizer  st = null;
	String nextTokenStr = null;

          while ( (line = lnr.readLine()) != null)
         {
            st   = new StringTokenizer(line);

            if(st.hasMoreTokens())
            {
                if(st.nextToken().startsWith("header1"))
                {
                   do
                   {
		 // Need to count number of values for 
                                 // header1 and add values to array

		nextTokenStr    = st.nextToken("\t=");
		System.out.println(nextTokenStr);
			
		line = lnr.readLine();
                     	st   = new StringTokenizer(line);
	
	           } while( (!st.nextToken().startsWith("header2"))  
                                                             &&  (st.hasMoreTokens()) );
		   break;
 		}
	    }
	 }
 }

Sorry I seemed to have posted this three times by mistake.

Hey,
You have called st.nextToken() twice. See, highlighted part.
On the first call i.e. inside if statement it returns you the nextToken but stores nowhere. That is why you are missing 1st token of each line.


if(st.hasMoreTokens()) {
if(st.nextToken().startsWith("header1")) {
do {
// Need to count number of values for
// header1 and add values to array

nextTokenStr = st.nextToken("\t=");
System.out.println(nextTokenStr);
.
.
.
.


And please learn to indent your code.

Thanks for your help. If there a way of checking for the nextToken but not moving the pointer forward I can't figure out how to change the code?
I'm also getting an exception in the do while loop I now find.
Exception in thread "AWT-EventQueue-0" java.util.NoSuchElementException

Sorry

This article has been dead for over six months. Start a new discussion instead.