I understand that the "cannot find symbol" message usually appears if you are missing a variable declaration or a library for a method, right? I have the following code, in which the error happens in the lines related to file reading. This are the error messages:
P3a.java:81: error: cannot find symbol
String linea = file.nextLine();
^
symbol: method nextLine()
location: variable file of type File
P3a.java:141: error: cannot find symbol
} while(file.hasNext() == true); //SEGUIRA LEYENDO FILAS DE COCHES MIENTRAS HAYA CONTENIDO EN EL FICHERO
^
symbol: method hasNext()
location: variable file of type File
P3a.java:143: error: cannot find symbol
file.close();
^
symbol: method close()
location: variable file of type File
3 errors
While the code on itself is as follows:
package practica3;
import java.util.Scanner;
import java.io.File;
import java.io.PrintWriter;
public class P3a {
public static void main(String[] args) {
String nombrefichero = args[0];
String plate3 = "6288LYM", manufacturer3="Ford"; int power3=110; float batteryCharge3 = 120.5F;
String plate4 = "07451JMR", manufacturer4 = "Seat"; int power4=60; float batteryCharge4 = 10.1F;
String plate5 = "3400JXK", manufacturer5 = "Peugeot"; int power5=70; float batteryCharge5 = 50F;
String plate6 = "0041LDR", manufacturer6 = "Seat"; int power6 = 20; float batteryCharge6 = 10.1F;
ElectricCar miElectricCar1 = new ElectricCar("1111KLS", "SEAT", 220, 30.5);
ElectricCar miElectricCar2 = new ElectricCar();
miElectricCar2.setPlate("2222LSX");
miElectricCar2.setManufacturer("FORD");
miElectricCar2.setElectricPower(220);
miElectricCar2.setBatteryCharge(30.5);
String s1 = miElectricCar1.toText();
String s2 = miElectricCar2.toText();
System.out.println(s1);
System.out.println(s2);
if (ElectricCar.isValidPlate("6288LYM") == true)
{if (ElectricCar.isValidelectricPower(110) == true)
{if (ElectricCar.isValidbatteryCharge(120.5) == true)
{ElectricCar miElectricCar3 = new ElectricCar("6288LYM", "Ford", 110, 120.5);} else{}
}else{}
}else{}
if (ElectricCar.isValidPlate("07451JMR") == true)
{if (ElectricCar.isValidelectricPower(60) == true)
{if (ElectricCar.isValidbatteryCharge(10.1) == true)
{ElectricCar miElectricCar3 = new ElectricCar("07451JMR", "Seat", 60, 10.1);} else{}
}else {}
}else{}
if (ElectricCar.isValidPlate("3400JXK") == true)
{if (ElectricCar.isValidelectricPower(70) == true)
{if (ElectricCar.isValidbatteryCharge(50) == true)
{ElectricCar miElectricCar3 = new ElectricCar("3400JXK", "Peugeot", 70, 50);} else{}
}else {}
}else{}
if (ElectricCar.isValidPlate("0041LDR") == true)
{if (ElectricCar.isValidelectricPower(20) == true)
{if (ElectricCar.isValidbatteryCharge(10.1) == true)
{ElectricCar miElectricCar3 = new ElectricCar("0041LDR", "Seat", 20, 10.1);} else{}
}else {}
}else{}
new P3a().readCityCarsFile(nombrefichero);
}
ElectricCar[] electricCityCars = new ElectricCar[25];
CombustionCar[] combustionCityCars = new CombustionCar[25];
HybridCar[] hybridCityCars = new HybridCar[25];
private static int cochesC = 0;
private static int cochesE = 0;
private static int cochesH = 0;
public void readCityCarsFile (String fichero) {
File file = new File(fichero);
do {
String linea = file.nextLine();
if (linea.startsWith("#")) {
continue;
}
else {
String[] infocar = linea.split(";");
if (infocar[0] == "C") {
if (CombustionCar.isValidPlate(infocar[1]) == true)
{
int mecPower = Integer.parseInt(infocar[3]);
if (CombustionCar.isValidmechanicalPower(mecPower) == true){
CombustionCar miCombustionCar = new CombustionCar(infocar[1], infocar[2], mecPower);
combustionCityCars[cochesC] = miCombustionCar;
cochesC++;} else{}
}else{}
} else{}
if (infocar[0] == "E") {
if (ElectricCar.isValidPlate(infocar[1]) == true)
{
int elecPower = Integer.parseInt(infocar[3]);
double batCharge = Double.parseDouble(infocar[4]);
if (ElectricCar.isValidelectricPower(elecPower) == true)
{if (ElectricCar.isValidbatteryCharge(batCharge) == true){
ElectricCar miElectricCar = new ElectricCar(infocar[1], infocar[2], elecPower, batCharge);
electricCityCars[cochesE] = miElectricCar;
cochesE++;}
else{}
}else {}
}else{}
} else{}
if (infocar[0] == "H") {
if (HybridCar.isValidPlate(infocar[1]) == true)
{
int mhpower = Integer.parseInt(infocar[3]);
int mepower = Integer.parseInt(infocar[4]);
double bathCharge = Double.parseDouble(infocar[5]);
if (HybridCar.isValidmechanicalPower(mhpower) == true)
{if (HybridCar.isValidelectricPower(mepower) == true)
{if (HybridCar.isValidbatteryCharge(bathCharge) == true){
HybridCar miHybridCar = new HybridCar(infocar[1], infocar[2], mhpower, mepower, bathCharge);
hybridCityCars[cochesH] = miHybridCar;
cochesH++;}
else{}
}else{}
}else{}
}else{}
}else {}
}
} while(file.hasNext() == true);
file.close();
}
}
What I have tried:
The overall structure of the code was copy pasted from a previous program that I wrote, so the file reading should work properly, but when it's inside of the method that is outside the main, the compiler doesn't know what to do with those 3 methods that show up in the error messages.