A java class can't find an other in the same p

2019-01-26 02:42发布

问题:

I am implementing a java program in ubuntu without an IDE that converts a currency to €, i have 2 classes ConvertiEuro and Valuta both in the same directory (package) called finanza, the class ConvertiEuro uses the class Valuta, when i try to compile Valuta.java it compiles correctly but when i compile ConvertiEuro.java I get an error saying "ConvertiEuro.java:3: error: cannot find symbol" I don't know why here is the code

  package finanza;

   public class Valuta {

     private String nomeValuta;
     private double totValuta;



    public Valuta(String nomeVal, double totVal) {

             nomeValuta = nomeVal;
             totValuta = totVal;
   }

       public String getNomeValuta() {

          return nomeValuta;    
       }

        public double getTotValuta() {
          return totValuta;     
   }

}

package finanza;

 import finanza.Valuta;

 public class ConvertiEuro {


private int valuteGestibili;
private int cont = 0;
private Valuta [] valutas;



public ConvertiEuro(int valuteGest) {

    this.valuteGestibili = valuteGest;
    this.valutas = new Valuta [this.valuteGestibili];


}
public boolean impostaValuta(Valuta val){


    if(cont<valuteGestibili) {
        this.valutas[cont] = val;
        cont ++;
        return true; 

    }
    else {
        return false;
    }   

}

}

and this how I compile: javac ConvertiEuro.java

回答1:

I strongly suspect the problem is in how you're compiling.

Both ConvertiEuro.java and Valuta.java should be in a directory called finanza, and you should ideally compile from the parent directory, so that all the compiler knows where to find other code in the same package. It would expect to find a source file in a finanza directory under the one you're currently in, for a package called finanza.

It's simplest just to compile all the files at the same time though:

javac finanza/*.java

... or better yet, use an IDE which will manage this sort of things for you.