简体   繁体   中英

Why is my program not counting word hello when it reads from a text file in IntelliJ

I am new to java and coding in general and I was learning how to read from file. I have looked up at the process on how to add file into the src folder by just dragging it but my question persists on why my program is not counting a string "hello" when it reads the file successfully. According to what I think, it is the first element that shows us and I have used the IF statement to check out by storing it in a string element line. can someone explain this to me in a beginner friendly way.

public class Main {
    public static void main(String[] args) throws FileNotFoundException {
        File path = new File("C:\\Users\\fahad\\IdeaProjects\\testingFileIO\\src\com\\Fahad\\Readingfile.txt");
        Scanner fileread = new Scanner(path);
        String check = "hello";
        int count=0;

        boolean flag = false;
        while(fileread.hasNextLine()){
            String line = fileread.nextLine();
            System.out.println(line);
            if(line == check)
            {
                flag = true;
                count++;
            }
        }

        if(flag == true)
        {
            System.out.println("WE found it" + count);
        }
        else
        {
            Syste.out.println("No string was there");
        }
    }
}

output

hello world.
This is fahad Qazi
How are you doing ?
checking
1234567
No string was there

I am new to stackoverflow so sorry about this question but any help is appreciated

The majority of your code is alright. It can be better, but it's alright for a start. As to where you're going wrong is the line:

if (line == check)

You're comparing the complete line if it equals to the string "hello" . This returns false since the line in question is hello world .

A solution to this is to do a contains() operations. Below is an example of this

String myStr = "Hello world";
System.out.println(myStr.contains("Hello"));   // true
System.out.println(myStr.contains("world"));   // true
System.out.println(myStr.contains("hello"));   // false due to case sensitivity
System.out.println(myStr.contains("Hi"));      // false

So in your case you should check the following using contains:

if (line.contains(check)) // do the things you wanna do when the line contains 'hello'

To compare String values you don't have to use "==" .

You have to compare with the equals method from String class. For example:

String x="hello",y="world";
if(x.equals(y)){

}

Or you can use x.equalsIgnoreCase(y) if you want to compare two strings and ignore uppercase and lowercase.

Direct == operation on string will not work. You need to use contains(CharSequence s) method from String class in-order to check if the the required sequence of characters is present in the given string or not. You need to make use of this method in your code in order to achieve the required result.

Additionally, if you are using Java 8+, then solution can be simplified/re-written as:

long count = Files.lines(Paths.get(<your_file_path>))
.filter(line -> line.contains(check))
.count();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM