THIS IS MY CODE
import java.IO.*;
class jed {
public static void main (String args[]){
BufferedReader datain = new BufferedReader(new InputStreamReader(System.in));
String name =" ";
System.out.print("What is your name?:");
try{
name = datain.readline();
} catch(IOException e) {
System.out.print("Error");
}
System.out.print("Your name is" + name); } }
THIS IS THE ERROR
D:\>javac jed.java jed.java:1: error: package java.IO does not exist
import java.IO.*; ^ jed.java:4: error: cannot find symbol
BufferedReader datain = new BufferedReader(new
InputStreamReader(System.in)); ^ symbol: class BufferedReader
location: class jed jed.java:4: error: cannot find symbol
BufferedReader datain = new BufferedReader(new
InputStreamReader(System.in)); ^ symbol: class BufferedReader
location: class jed jed.java:4: error: cannot find symbol
BufferedReader datain = new BufferedReader(new
InputStreamReader(System.in));^ symbol: class InputStreamReader
location: class jed jed.java:10: error: cannot
find symbol catch(IOException e){^ symbol: class IOException location: class jed 5 errors
I will appreciate any help I can get to fix this issue. Thank you
Java is case sensitive.
Your import is wrong. Change
import java.IO.*;
to
import java.io.*;
In defence of the compiler it does actually tell you the problem clearly:
error: package java.IO does not exist import java.IO.*;
Java is case sensitive. You should import java.io.*, instead of java.IO.*.
You have to change .readline()
to .readLine(), because java is case sensitive.
Related
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();
}
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 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
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)
The runtime error I get is:
----jGRASP exec: javac -g Programmmm1.java
Programmmm1.java:95: error: cannot find symbol
StringTokenizer token = new StringTokenizer(infile.nextLine());
^
symbol: class StringTokenizer
location: class Programmmm1
Programmmm1.java:95: error: cannot find symbol
StringTokenizer token = new StringTokenizer(infile.nextLine());
and this is my actual segment, why is it saying that it cannot find symbol? Does it have something to do with my Main?
public static void Display()throws IOException, FileNotFoundException
{
Scanner infile = new Scanner(new FileReader("G:\\DataFile.txt"));
StringTokenizer token = new StringTokenizer(infile.nextLine());
StringElement str = new StringElement();
while(token.hasMoreTokens())
{
str.setString(token.nextToken());
stringList.insert(str);
}
stringList.print();
}
You need to add this in your import statement
import java.util.StringTokenizer;
Update
public static void Display()throws IOException, FileNotFoundException
{
Scanner infile = new Scanner(new FileReader("G:\\DataFile.txt"));
StringTokenizer token = null;
String line=null;
while((line=infile.nextLine())!=null && !"".equals(line.trim()))
{
token = new StringTokenizer(line);
StringElement str = new StringElement();
while(token.hasMoreTokens())
{
str.setString(token.nextToken());
stringList.insert(str);
}
}
stringList.print();
}