Sajha.com Archives
Java Problem

   I have a text file which has words in a 11-Apr-03 Biruwa
     Hello! You want to find "equals" and 11-Apr-03 eNepal
       it's fairly simple. just replace if (a 11-Apr-03 salakjith
         Thanks for u'r inputs! 11-Apr-03 Biruwa


Username Post
Biruwa Posted on 11-Apr-03 05:15 PM

I have a text file which has words in a single column. I want to find out how many "equals" are written in that file. The following program compiles perfectly but cannot find out the word "equals".

What's wrong with it?

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

public class test1 {
public static void main (String[] args) throws IOException {
String line,line1, account;
int e = 0;
StringTokenizer tokens;
//open local files
BufferedReader tempFile = new BufferedReader(new FileReader("temp.dump"));
System.out.println();

line = tempFile.readLine();

// outer loop - each round takes care of an account
while (line != null) {
// break the input line to its parts (called "tokens") delimited by spaces
tokens = new StringTokenizer(line);
// put each token in a different variable
account = tokens.nextToken();
System.out.println("out"+e);
if (account=="equal"){
e++;
System.out.println("in"+e);
}
//
line = tempFile.readLine();
}
tempFile.close();

System.out.println();
} // method main
} // class numbers
eNepal Posted on 11-Apr-03 06:35 PM

Hello!

You want to find "equals" and you are writing "if (account=="equal"){ "
I wonder whether you have "equal" in your file or "equals"?

can you publish your data file, here?

Did you check that your program correctly reads the file at first? Why don't you put things insdie "try" loop to sort out file opening error.

The first thing beginners should do is to print out whatever you read from a file.
You write after every steps, that you are not sure whether program enters in that loop or passes out of the loop. Your first job will be to confirm whether the program is going inside a particular loop, you hope it to pass through. The only method to confirm it is to assign an identification code and print it out before and after the loop or the statement.

.Net Nepal
salakjith Posted on 11-Apr-03 07:02 PM

it's fairly simple. just replace
if (account=="equal"){

with:

if (account.equals ("equal") ){

Your variable "account" is a String instance, and hence you cannot use the == operator. It only works for primitive types like int, char etc.
Biruwa Posted on 11-Apr-03 08:35 PM

Thanks for u'r inputs!