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.
Related
I got an error while trying to run my program from the command line.
My source code is:
public class Split {
public static void main(String[] args) {
String name = args[0]; // base file name
int n = Integer.parseInt(args[1]); // number of fields
String delimiter = args[2]; // delimiter (comma)
// create one output stream for each of the N fields
Out[] out = new Out[n];
for (int i = 0; i < n; i++) {
out[i] = new Out(name + i);
}
// read in the input and divide by field
In in = new In(name + ".csv");
while (in.hasNextLine()) {
String line = in.readLine();
String[] fields = line.split(delimiter);
for (int i = 0; i < n; i++) {
out[i].println(fields[i]);
}
}
}
}
The error I got:
C:\Users\zunayeed\Desktop\jav>javac Split.java Split.java:8: error:
cannot find symbol
Out[] out = new Out[n];
^ symbol: class Out location: class Split Split.java:8: error: cannot find symbol
Out[] out = new Out[n];
^ symbol: class Out location: class Split Split.java:10: error: cannot find symbol
out[i] = new Out(name + i);
^ symbol: class Out location: class Split Split.java:14: error: cannot find symbol
In in = new In(name + ".csv");
^ symbol: class In location: class Split Split.java:14: error: cannot find symbol
In in = new In(name + ".csv");
^ symbol: class In location: class Split 5 errors
Can anyone suggest how I can fix this error?
According to your code and the error message, the reason you are getting the error is because the compiler can not find the 'In' class and the 'Out' class.
When you are compiling your program, you are just compiling the 'Split.java' file. In order to compile the other class files that Split.java requires, you must explicitly tell the compiler to compile those other classes as well. If they are located in the same folder as Split.java, then all you have to do to compile them is to run this in the command line:
javac In.java Out.java Split.java
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");
This question already has answers here:
Why does a Try/Catch block create new variable scope?
(5 answers)
Closed 7 years ago.
I am trying to output the split contents of a .txt file.
Here is my code so far:
import java.io.FileReader;
import java.io.BufferedReader;
public class PassFail {
public static void main(String[] args) {
String path = "C:\\new_java\\Final_Project\\src\\student.txt";
try {
FileReader file = new FileReader(path);
BufferedReader reader = new BufferedReader(file);
String line = reader.readLine();
reader.close();
String[] values = line.split(" ");
int nums[] = new int[values.length];
for (int x = 0; x < values.length; x++) {
nums[x] = Integer.parseInt(values[x]);
}
} catch (Exception e) {
System.out.println("Error:" + e);
}
System.out.println(nums[1]);
}
}
Question: Why am I getting the error "nums cannot be resolved to a variable" when trying to output num[2]? Furthermore, how can I fix this? To my knowledge I have already declared nums[] as a an int data type just before the for loop.
Because nums is defined in the try block, and you try to access it outside the try.
Also, it's nicer if you use an IDE like IntelliJ or Eclipse. This will help you format the code, and more easily spot errors like this.
I'm currently trying to read in a formatted file for a maze game I am creating. Whenever I try to compile, however, it states that it cannot find symbols and is pointing to the parseInts inside the try block. I imported java.lang.Integer. Does anyone understand why Java is refusing to do so?
private Room[][] readRooms(String filepath) throws IOException
{
int numberOfRooms;
int numRows;
int numCols;
Room[][] grid;
try
{
FileReader fr = new FileReader(filepath);
BufferedReader br = new BufferedReader(fr);
String next = null;
numberOfRooms = parseInt(br.readLine());
numRows = parseInt(br.readLine());
numCols = parseInt(br.readLine());
grid = new Room[numRows][numCols];
while((next = br.readLine()) != null)
{
if (next.equals("***"))
{
}
}
br.close();
}
catch(IOException ex)
{
System.out.println("File potentially malformed.");
System.err.println(ex);
}
return grid;
}
Note: The string "***" is a separator in the text file.
Specific javac output:
javac "Maze.java" (in directory: C:\Users\Blaise\Programming\csc300\Maze)
Maze.java:40: error: cannot find symbol
numberOfRooms = parseInt(br.readLine());
^
symbol: method parseInt(String)
location: class Maze
Maze.java:41: error: cannot find symbol
numRows = parseInt(br.readLine());
^
symbol: method parseInt(String)
location: class Maze
Maze.java:42: error: cannot find symbol
numCols = parseInt(br.readLine());
^
symbol: method parseInt(String)
location: class Maze
3 errors
Compilation failed.
parseInt is a static method declared in the Integer class. To invoke it you need to either qualify the method name with the class name as follows:
Integer.parseInt(yourString);
Or add the following static import statement at the top of your class:
import static java.lang.Integer.parseInt;
You should use Integer.parseInt(String). It is a static method inside Integer class
You should use the static Integer.parseInt(String) or
import static java.lang.Integer.parseInt;
You should just use Integer.parseInt(String). But if you are trying to import it you should statically import it like this:
import static java.lang.Integer.parseInt;
You do not need to import java.lang.Integer, because java.lang package is always imported. Use static method Integer.parseInt(str)
im trying to make a class that will read in text from a file and put it into an array, and im coming across an error that we have not covered in class yet...
here is my code
import java.util.Scanner;
import java.io.*;
class Card
{
File inputFile = new File("Cards.txt");
Scanner textScan = new Scanner(inputFile);
{
String[] cards = new String[51];
for (int i = 0; i < cards.length; i++)
{
while (textScan.hasNextLine())
cards[i] = textScan.nextLine();
}
}
}
and this is the error:
Card.java:7: error: unreported exception FileNotFoundException; must be caught or declared to be thrown
Scanner textScan = new Scanner(inputFile);
^
1 error
thanks in advance, also im pretty new to java as you can probably tell from what im working on, so if you could dumb down your responses as much as possible that would be greatly appreciated :)
Constructor of Scanner declares to throw FileNotFoundException so it must be handled (either by catching or by rethrowing)
You must catch the exception using a Try-Catch statement like this
try{
Scanner textScan = new Scanner(inputFile);
}catch(FileNotFoundException e) {
System.out.println("Error!");
}
It's like telling Java what to do if you can't find the file 'inputFile' is referenced to.