Problem in compilation: Symbol error in csv file split in java - java

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

Related

How to delete String from text file?

I'm working on creating simple address book using text file but my code is throwing too much errors in deleting a String method.It shows IO Exceptions at mostly places and when the IO Exceptions are resolved then compiling error cannot find symbol occurs at 5 places in some identifiers. Here is my code:
public void DeletePerson(){
try {
File file = new File("AddressBook.txt");
File temp = File.createTempFile("file", ".txt", file.getParentFile());
BufferedReader reader = new BufferedReader(new InputStreamReader(new
FileInputStream(file), Charset));
PrintWriter writer = new PrintWriter(new OutputStreamWriter(new
FileOutputStream(temp), Charset));
//More code ...
} finally {
if (writer != null) {
System.out.println("Closing PrintWriter");
writer.close();
} else {
System.out.println("PrintWriter not open");
}
file.delete();
temp.renameTo(file);
}
}
Output:
C:\java\AddressBook>javac AddressBook.java
AddressBook.java:50: error: cannot find symbol
if (writer != null) {
^
symbol: variable writer
location: class AddressBook
AddressBook.java:52: error: cannot find symbol
writer.close();
^
symbol: variable writer
location: class AddressBook
AddressBook.java:57: error: cannot find symbol
file.delete();
^
symbol: variable file
location: class AddressBook
AddressBook.java:58: error: cannot find symbol
temp.renameTo(file);
^
symbol: variable file
location: class AddressBook
AddressBook.java:58: error: cannot find symbol
temp.renameTo(file);
^
symbol: variable temp
location: class AddressBook
I'm creating address-book and finding problem in deleting person's name method. Firstly i have to take input from user i.e. name of the person then i have to check the text-file(read the file) and find the matched word and then delete it from addressbook.
I have also made other methods to delete the name but they didn't work thoroughly.
Kindly check the code and resolve the problem please.
Your File and PrintWriterObject inside the try block
You can try to use it.
File file = new File("AddressBook.txt");
PrintWriter writer = new PrintWriter(new FileWriter(file,Charset.forName("UTF-8")));//use "throws IOException" in your method.
try{
//some java code
}finally{
if (writer != null) {
System.out.println("Closing PrintWriter");
writer.close();
} else {
System.out.println("PrintWriter not open");
}
file.delete();
}

"Cannot find symbol" in Java file operations [duplicate]

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.

Unable to read MySQL log file [duplicate]

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");

Java will not use parseInt

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)

Class issue java

I wrote a flesch reading program that uses another class. I was under the impression that simply having the two classes saved in the same folder would enable one to access the other but I am getting errors. Any ideas.
The error I am getting is:
Flesch.java:36: cannot find symbol
symbol : method getSyllableCt()
location: class Flesch
sllyablesCt = getSyllableCt();
flesh is here:
public class Flesch{
public static void main(String args[])throws Exception{
int syllablesCt,
wordCt,
sentenceCt;
double flesch;
String listStr;
StringBuffer sb = new StringBuffer();
String inputFile = JOptionPane.showInputDialog("What file do you want to sort?");
BufferedReader inFile = new BufferedReader(new FileReader(inputFile));
sb.append(inFile.readLine());
//listStr = inFile.readLine();
while (inFile.readLine() != null){
sb.append(inFile.readLine());
//listStr = inFile.readLine();
}
Sentence sentence = new Sentence(sb);
wordCt = getWordCt();
sentenceCt = getSentenceCt();
System.out.println("The sentence count is" + sentenceCt);
System.out.println("The word count is" + wordCt());
Word word = new Word(getWords());
sllyablesCt = getSyllableCt();
System.out.println("The syllable count is" + syllablesCt);
flesch = (.39 * wordCt / sentenceCt) + (11.8 * syllablesCt / wordCt) - 15.59;
System.out.println("The Flesch Readability of this document is" + flesch);
inFile.close();
}
}
If the methods live in another class they need to either be (a) referenced as static methods, or (b) called on an instance of the class.
// Static method
int syllableCount = TheOtherClassName.getSyllableCt();
// Instance method
TheOtherClassName otherClass = new TheOtherClassName();
int syllableCount = otherClass.getSyllableCt();
However it's not clear where the methods in question live, or how they're getting their data.
if the method is in another class, you need to be make the class static.
ClassName.getSyllableCt();
sllyablesCt = getSyllableCt();
Your code has a typo. That variable doesn't exist. Change it to
syllablesCt = getSyllableCt();

Categories