-2

Hey I am trying to read any "root" string in the log file name "SHIKHAR.log" but the compiler keeps on showing.

dam.java:12: error: cannot find symbol
       System.out.println("I found in file " +SHIKHAR.getName());
                                              ^
  symbol:   variable SHIKHAR
  location: class dam
1 error

I am not sure what's wrong with it I have tried appending the extensions and try it otherwise aswell but it doesn't work.

import java.util.Scanner;
import java.io.*;
class dam 
{
public static void main (String arg[])
{
final Scanner scanner = new Scanner("SHIKHAR");
while (scanner.hasNextLine()) {
   final String lineFromFile = scanner.nextLine();
   if(lineFromFile.contains("root")) { 
       // a match!
       System.out.println("I found in file " +SHIKHAR.getName());
       break;
   }
}
}
}

1 Answers1

0

You are using undefined variable name SHIKHAR.

System.out.println("I found in file " +SHIKHAR.getName());

As per your assumption SHIKHAR is file but here in your code its just a string literal.

You can define a variable as

final File SHIKHAR = new File("SHIKHAR");
final Scanner scanner = new Scanner(SHIKHAR);

BTW, as per your current code will simply read string and not from file named SHIKHAR.

final Scanner scanner = new Scanner("SHIKHAR");