0

I'm trying to read certain text file and when I find certain word, I should do another criteria,

in my code (will follow), I get an error, "Type mismatch: cannot convert from int to String" So, the suggested solution by Eclipse is to make the variable (key) integer instead of String what is the wrong thing here ?

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

public class JNAL {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        File file = new File("C:/20180918.jrn");
        FileInputStream fis = null;

        try {
            fis = new FileInputStream(file);

            System.out.println("Total file size to read (in bytes) : "
                    + fis.available());

            int content;
            String key;
            /*
            while ((content = fis.read()) != -1) {
                // convert to char and display it
                System.out.print((char) content);
            }
            */
            while ((key = fis.read()) == "Cash") {
                // convert to char and display it
                System.out.print((String) key);
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fis != null)
                    fis.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }   
    }
}

3 Answers 3

2

Try this

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;

public class JavaTextFileToString {


        public static void main(String[] args) throws Exception {
            File file = new File("C:/20180918.jrn");

            BufferedReader br=new BufferedReader(new FileReader(file));
            String line=null;
            while((line=br.readLine())!=null){
                if(line.equals("Cash")) {
                    System.out.println(line);
                }

            }

            br.close();
        }
    }
Sign up to request clarification or add additional context in comments.

8 Comments

The contains() method is Java method to check if String contains another substring, so it can not be used for checking equality for strings.
@Hülya you are right contains method should not be used but I added this because I thought each line in a file (.jrn) may contain other words as well. if the OP wants to check that the line in the file has just "Cash", then you are right the correct if condition should be if(line.equals("Cash")) { //do something}
@MONGWeee I am not sure what errors you are getting. Could you paste the screenshot or something. It might be because of compile time errors in your other java classes and since this is not using any other class this should run fine. Are you seeing the output when the line in the file has "Cash" in it. Also can u post your journal (.jrn) file here.
This one is working, But, it reads the whole line. I need only to read a certain word from that line and put it in the string variable.
Ignore my first comment, I deleted it
|
2

You have multiple issues in this code, but just to address your question:

(key = fis.read()) == "Cash"

"Cash" is of type String. You can't compare "primitive" int to "Object" String, so eclipse suggesting change primitive int to Object type String .

The key thing is, even that is not enough. While comparing objects you shouldn't use == instead use equals.

Comments

0

Try this:

BufferedReader bf = new BufferedReader(new InputStreamReader(fis));

while ((key = bf.readLine()).equals("Cash")) 

== operator compares the reference of an object so you should use equals() method to compare String

1 Comment

It won't work as you shouldn't compare object using ==

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.