Reputation: 50
Hello my network of software wizards. I have a questions as to why I can not get my class to compile. I keep getting error: cannot find symbol. All these classes are within the same package blocklaw. I have tried every variation of import statement but can not seem to remove this error. The directory structure is blocklaw --> src --> all_classes_here. I tried changing the package declaration blocklaw.src as well and still no help. I will share my code here,
package blocklaw;
import java.security.MessageDigest;
/*
* returns a string value of the true fact
*/
public class TrueFact implements Hashable {
private String trueFact;
private String hashFact;
private String source;
private String hashSource;
private int bytesLength;
private String hashedBytesLength;
private String hashedHash;
public TrueFact(String trueFact, String source){
this.trueFact = trueFact;
this.source = new Source(source).getSource();
byte[] bytes = trueFact.getBytes("UTF-8");
this.bytesLength = bytes.length;
setHashCode();
}
public String getTrueFact(){
return this.trueFact;
}
public String getHashFact(){
return this.hashFact;
}
public String getSource(){
return this.source;
}
public int getbytesLength(){
return this.bytesLength;
}
public String getHashedBytesLength(){
return this.hashedBytesLength;
}
public void setHashCode(){
this.hashFact = hash(this.trueFact);
this.hashSource = hash(this.source);
this.hashedBytesLength = hash(String.valueOf(this.bytesLength));
this.hashedHash = hash(this.hashFact+this.hashSource+this.hashedBytesLength);
}
public String getHashSource(){
return this.hashSource;
}
public String hash(String input){
try {
MessageDigest digest = MessageDigest.getInstance("SHA-256");
//Applies sha256 to our input,
byte[] hash = digest.digest(input.getBytes("UTF-8"));
StringBuffer hexString = new StringBuffer(); // This will contain hash as hexidecimal
for (int i = 0; i < hash.length; i++) {
String hex = Integer.toHexString(0xff & hash[i]);
if(hex.length() == 1) hexString.append('0');
hexString.append(hex);
}
return hexString.toString();
}
catch(Exception e) {
throw new RuntimeException(e);
}
}
}
It is my understanding that all classes within the same package do not need to be import explicitly. Why then is my error such that it can not find the symbol?
The following is the interface for Hashable...
package blocklaw;
public interface Hashable{
public String hash();
}
Upvotes: 0
Views: 965
Reputation: 950
So As discussed these are the steps to be followed to correct the answer.
Compile Source.java
And the String input parameter to the hash function in Hashable.java
Compile Hashable.java
Now go one folder level up.
Add necessary try catch blocks in TrueFact.java
Build blocklaw/TrueFact.java
This will compile the TrueFact.java.
Upvotes: 1
Reputation: 44
In an interface all the methods are public and abstract by nature. so no need to explicitly mention public keyword in your public String hash(); method instead you can just say String hash(); Also make sure your hash class is in the same directory and same package.
Upvotes: 0