Java will not use parseInt - 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)

Related

"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.

Problem in compilation: Symbol error in csv file split in 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

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

Cannot find symbol error bufferedreader

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.

Trying to get some words out of every line

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.

Categories