Hi

I am trying to tokenize a string from a file.
The file contains two lines that have different info. I need to tokenize the lines separately and compare the results of each line.
Now, I think my problem is identifying the separate lines to perform operations separately

/*
PROG: ride
*/

import java.io.*;
import java.util.*;

class ride {
  public static void main (String [] args) throws IOException {
    // Use BufferedReader rather than RandomAccessFile; it's much faster
    BufferedReader f = new BufferedReader(new FileReader("ride.in"));
                                                  // input file name goes above
    PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("ride.out")));
    // Use StringTokenizer vs. readLine/split -- lots faster


    StringTokenizer st = new StringTokenizer(f.readLine());
						  // Get line, break into tokens
	while (st.hasMoreTokens()){

    	int i1 = Integer.parseInt(st.nextToken());  // first value
    	int i2 = Integer.parseInt(st.nextToken());  // second value
	int i3 = Integer.parseInt(st.nextToken());  // third value
	int i4 = Integer.parseInt(st.nextToken());  // fourth value
	int i5 = Integer.parseInt(st.nextToken());  // fifth value
	int i6 = Integer.parseInt(st.nextToken());  // sixth value

    int i7 = (i1 - 64)*(i2 - 64)*(i3 - 64)*(i4 - 64)*(i5 - 64)*(i6 - 64);                      						     // product values

	int product = i7%47;


	}

    StringTokenizer st2 = new StringTokenizer(f.readLine());
						  // Get line, break into tokens
	while (st2.hasMoreTokens()){

    	int i8 = Integer.parseInt(st.nextToken());  // first value
    	int i9 = Integer.parseInt(st.nextToken());  // second value
	int i10 = Integer.parseInt(st.nextToken());  // third value
	int i11 = Integer.parseInt(st.nextToken());  // fourth value
	int i12 = Integer.parseInt(st.nextToken());  // fifth value
	int i13 = Integer.parseInt(st.nextToken());  // sixth value

    int i14 = (i8 - 64)*(i9 - 64)*(i10 - 64)*(i11 - 64)*(i12 - 64)*(i13 - 64);                      						     // product values

	int product2 = i14%47;

	}


	if(product == product2) {

	out.println("GO");					//Ready to go

	}

	else {

	out.println("STAY");					//Next time guys!!

	}

   
    out.close();                                  // close the output file
    System.exit(0);                               // don't omit this!
  }
}

Any help would be highly appreciated.

Recommended Answers

All 6 Replies

You can store the results in to two separate maps. Then iterate over each map and compare each object in the two maps.

Thanks but the source is strictly one file with two lines of words.
Or can I use another method that is not stringtokenizer to read the lines??

Does it compile?

Nope :(

That is why:

while () {
  
  [B]int product[/B] = ...
}

while () {
  
  [B]int product2[/B] = ...
}
if ([B]product[/B]==[B]product2[/B]) {
..
}

You declare the variables inside the while loops and then you try to use them outside. You cannot use a variable outside from the block it was declared.
Declare them outside and give them value inside the loop.

And when you posted your code, you didn't explain anything about your problem. Posting your code doesn't mean anything. Shouldn't you mention the "little" detail that the code doesn't compile?

Thanks.. Will try that out then post. Next time will remember to say that!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.