Click here to Skip to main content
15,867,330 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

Java
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];
     //Atributos de varios coches
     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);//COMAS

     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);
     
     
 } //FIN DEL MAIN
    //ARRAYS DE COCHES
    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;
    //METODO
    
    public void readCityCarsFile (String fichero) {

	File file = new File(fichero);

	
	do {
	    String linea = file.nextLine();
	    if (linea.startsWith("#")) { //COMPROBAMOS SI UNA LINEA EMPIEZA POR #, SI ES EL CASO, LA SALTAMOS
		continue;
	    }
	    else {
	        
		String[] infocar = linea.split(";"); //SI NO ES UN COMENTARIO, SE DIVIDE A PARTIR DE :
		
	        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); //SEGUIRA LEYENDO FILAS DE COCHES MIENTRAS HAYA CONTENIDO EN EL FICHERO
	
	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.
Posted
Updated 6-Mar-22 2:43am
v2

At a guess - and I'm not going to try and sort out that mess of code to work out your bracketing - it's the previous method that causes this: get rid of every single line like this:
Java
else{}
And sort out your indentation and style os it's both consistent and readable: at present, it is neither, and that's probably "hiding" an extra or missing "{" or "}".
If you are going to use the java standard bracketing placement, then stick with it:
Java
public static foo(){
   if (...) {
      if (...) {
         ...
         }
      }
   ...
   }
Don't mix in your own nastiness:
Java
public static foo(){
   if (...) 
   {if (...) 
    {...
    } else{}
   } else{}
   ...
   }
It makes it very difficult to work out what should be goin on, let alone what actually is ...
 
Share this answer
 
Comments
Maciej Los 6-Mar-22 8:49am    
Very good suggestion!
A simple reading of the documentation at File (Java Platform SE 7 )[^] shows you that the File class does not contain any of those three methods.
 
Share this answer
 
v2
Comments
Maciej Los 6-Mar-22 8:48am    
5ed!
If OP wants to read file, he have to use BufferReader or Scanner class.
Richard MacCutchan 6-Mar-22 8:55am    
Thanks, I was hoping my suggestion might prompt him/her to make real use of the documentation.
Maciej Los 6-Mar-22 9:01am    
👍
OriginalGriff 6-Mar-22 9:09am    
Ever the optimist, I see! :laugh:
_Asif_ 7-Mar-22 3:32am    
+5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900