This question already has answers here:
What does a "Cannot find symbol" or "Cannot resolve symbol" error mean?
(18 answers)
Closed 4 years ago.
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;
}
}
}
}
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");
Related
This question already has answers here:
How do I compare strings in Java?
(23 answers)
NoSuchElement exception when using Scanner
(2 answers)
Closed 1 year ago.
This is a silly game written in Python, I was wondering how to reproduce it in Java:
name = ''
while name != 'your name':
print('Please type your name.')
name = input()
print('Thank you!')
Basically, it requests to write the string "your name" to break the while loop, now, I've tried this code in Java but it throws an error:
import java.util.Scanner;
public class sillyGame {
public static void main(String [] args) {
String name = "";
while (name != "your name"){
System.out.println("Please type your name");
Scanner input = new Scanner(System.in);
name = input.next();
input.close();
}
System.out.println("thank you");
}
}
The error is: "Exception in thread "main" java.util.NoSuchElementException"
I really appreciate your help.
I am a beginner with java and programmin over all, So this the full code for a file reader program that counts words or displays text file content, I wanted to take user inputs for commands that I indicated using an if statement, but String printFileCommand = scan.nextLine(); is not working due to the error addressed below:
package com;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class FileReader {
public static void main(String[] args) throws FileNotFoundException {
Scanner scanTwo = new Scanner(System.in);
System.out.println("Please Enter Your File Path");
String filePath = scanTwo.nextLine();
scanTwo.close();
File fileInput = new File(filePath);
Scanner fileScanner = new Scanner(fileInput);
System.out.println(fileScanner.nextLine());
fileScanner.close();
System.out.println("Commands: PRINT.FILE --> Prints all file COUNT.WORDS --> Counts all words");
System.out.println("Type Command:");
Scanner scan = new Scanner(System.in);
String printFileCommand = scan.nextLine(); <----ERROR HERE
scan.close();
if (printFileCommand.contains("PRINT.FILE")) {
while (fileScanner.hasNextLine()) {
System.out.println(fileScanner.nextLine());
}
} else if (printFileCommand.contains("COUNT.WORDS")) {
int wordCount = 0;
while (fileScanner.hasNext()) {
String fileWords = fileScanner.next();
wordCount++;
// System.out.println(wordCount);
}
System.out.println(wordCount);
}
else {
System.out.println("COMMAND INVALID!");
}
}
}
```
**Terminal Output:**
PS C:\Users\DR\Desktop\FIRST REAL PROGRAMMING> c:; cd 'c:\Users\DR\Desktop\FIRST REAL PROGRAMMING'; & 'c:\Users\DR\.vscode\extensions\vscjava.vscode-java-debug-0.30.0\scripts\launcher.bat' 'C:\Program Files\AdoptOpenJDK\jdk-15.0.1.9-hotspot\bin\java.exe' '--enable-preview' '-XX:+ShowCodeDetailsInExceptionMessages' '-Dfile.encoding=UTF-8' '-cp' 'C:\Users\DR\AppData\Roaming\Code\User\workspaceStorage\458dc35931a3067a355426e5ceeeee32\redhat.java\jdt_ws\FIRST REAL PROGRAMMING_e263b9bc\bin' 'com.FileReader'
Please Enter Your File Path
E://texttwo.txt
This is my text file.
Commands: PRINT.FILE --> Prints all file COUNT.WORDS --> Counts all words
Type Command:
Exception in thread "main" java.util.NoSuchElementException: No line found
at java.base/java.util.Scanner.nextLine(Scanner.java:1651)
at com.FileReader.main(FileReader.java:21)
PS C:\Users\DR\Desktop\FIRST REAL PROGRAMMING>
So why is `String printFileCommand = scan.nextLine();` not working? I tried alot, but its not working...
It doesn't work because your stream for System.in is closed.
You can check it for example System.out.println(System.in.available()); and you will see:
Exception in thread "main" java.io.IOException: Stream closed
at java.io.BufferedInputStream.getInIfOpen(BufferedInputStream.java:159)
at java.io.BufferedInputStream.available(BufferedInputStream.java:410)
you closed it in line: scanTwo.close();
I'm still trying to understand Java myself, but I think you don't exactly need to create and use multiple Scanners to collect data. Since you are searching for strings for the file creations, you could technically do something like:
Scanner scanner = new Scanner(System.in);
String filePath = scanner.nextLine();
With some of the other scanners you can keep since you're specifically calling the fileInputs within the Scanner, but when asking the user for data, I suggest using only one scanner source, but having something like the last line of code I shared as a template for updating your code! If I misunderstood something you're more than welcome to let me know. Thanks!
Please check this question:
NoSuchElementException - class Scanner
Your code will work if you remove the code:
scanTwo.close();
Or removing better:
Scanner scan = new Scanner(System.in);
And use scanTwo for reading (but you don't have to close the scanner with scanTwo.close()).
But I recommend you to read those answers to understand how it works.
This question already has answers here:
What does a "Cannot find symbol" or "Cannot resolve symbol" error mean?
(18 answers)
Closed 2 years ago.
I have the following code:
import java.util.Random;
import java.util.Scanner;
import java.io.*;
public class Main
{
public static void main(String[] args)
{
try
{
FileInputStream in = null;
FileOutputStream outp = null;
in = new FileInputStream("file.txt");
Random rand = new Random();
int x = rand.nextInt(9);
int guess;
int count=0;
Scanner input = new Scanner(System.in);
int temp;
char znak;
int i=0;
while(i<1000)
{
temp=rand.nextInt(94)+32;
znak=(char)temp;
in.write((int)znak);
i++;
}
i=0;
in.close();
outp = new FileOutputStream("file.txt");
while(i<1000)
{
znak = (char)outp.read();
System.out.print(znak);
i++;
}
}
catch(Exception e)
{
System.out.print("Input error");
return;
}
}
}
I apologise for pasting it all in instead of only the relevant parts, but I'm still new to Java and can't pinpoint where exactly the problem lies. When I attempt to compile it, I get the following errors:
Main.java:31: error: cannot find symbol
in.write((int)znak);
^
symbol: method write(int)
location: variable in of type FileInputStream
Main.java:40: error: cannot find symbol
znak = (char)outp.read();
^
symbol: method read()
location: variable outp of type FileOutputStream
What could be causing the problem here? From what I've gathered, these are normally returned when I try to use an undefined variable, but I do define them before.
InputStreams aren't writable, and OutputStreams aren't readable. You have the order of operations you want to do backwards.
I just copied and pasted this code straight from my Uni provided lecture notes:
import java.util.*;
public class Echo {
public static void main (String[] args) {
Scanner console = new Scanner(System.in);
System.out.println("Input a line of text");
String message = console.nextLine();
System.out.println("Your input was: "
+ message);
it keeps giving me the error: Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The constructor Scanner(InputStream) is undefined
at Scanner.main(Scanner.java:4)
i think it is reffering to the (System.in); section of code, but I do not know how to fix it.
You named your file Scanner.java, but you should have named it Echo.java. Java requires that file names and public class names be the same.
The specific error: javac thought you were defining a Scanner class, which was conflicting with java.util.Scanner. Had you fixed that, it would have complained about the class/filename mismatch.
import java.util.Scanner;
import java.util.Scanner;
public class Echo {
public static void main (String[] args) {
Scanner console = new Scanner(System.in);
System.out.println("Input a line of text");
String message = console.nextLine();
System.out.println("Your input was: "
+ message);
I have been trying to get 2,3,4 words of a file and this is the code so far. But I am getting some error messages. Can anybody help me please? This is the code:
import java.util.Scanner;
import java.io.File;
import java.io.PrintWriter;
import java.io.FileNotFoundException;
class PrintLines{
public static void main(String[] args) throws FileNotFoundException {
Scanner me = new Scanner(System.in);
System.out.print("File Name: ");
String s = me.next();
File inFile = new File(s);
Scanner in = new Scanner(inFile);
while(in.hasNextLine()){
String[] split=in.split(" ");
System.out.println(split[2]+split[3]+split[4]);
}
in.close();
}
}
But this is the error messages I am getting:
PrintLines.java:18: cannot find symbol
symbol : method split(java.lang.String)
location: class java.util.Scanner
String[] split=in.split(" ");
^
1 error
You are calling split on the Scanner itself; you should be calling it on the nextLine which returns the next line as a String:
String[] split = in.nextLine().split(" ");
If you read the docs then Scanner doesn't have a "split" method, so what you're getting is a compiler error telling you that you're calling a non-existent method.
Try swapping
String[] split=in.split(" ");
for:
String[] split=in.nextLine().split(" ");
The connection between the two methods is hinted at if you read the JavaDoc for hasNextLine(), where the nextLine() method is the next one documented.