Why is my System.out.println not working? Also this is not the main class and when i try to access it from the main class i don't get the output

huangapple 未分类评论54阅读模式
英文:

Why is my System.out.println not working? Also this is not the main class and when i try to access it from the main class i don't get the output

问题

以下是翻译好的代码部分:

public class Init{
 
    private String clientName;
    private String clientNumber;
    private double balance;
 
    public Init(ASCIIDataFile file){
    
        clientNumber = file.readString();
        clientName = file.readString();
        balance = file.readDouble();
    } 

    public String getClientName(){
        System.out.println(clientName);  // `not working`  
        return clientName;  
    }
}
英文:

the code isn't giving the output

public class Init{

    private String clientName;
    private String clientNumber;
    private double balance;
 
    public Init(ASCIIDataFile file){
    
        clientNumber = file.readString();
        clientName = file.readString();
        balance = file.readDouble();
    } 




 public String getClientName(){
  System.out.println(clientName);  // `not working`  
    return clientName;  
  }
}

答案1

得分: -2

你的构造函数中缺少 this.,因此在 getClientName() 中的 clientName 为空。

你的构造函数应该像这样:

    public Init(ASCIIDataFile file){

        this.clientNumber = file.readString();
        this.clientName = file.readString();
        this.balance = file.readDouble();
    } 
英文:

You are missing this. in constructor therefore clientName in getClientName() is empty

Your constructor should be like this:

    public Init(ASCIIDataFile file){

        this.clientNumber = file.readString();
        this.clientName = file.readString();
        this.balance = file.readDouble();
    } 

huangapple
  • 本文由 发表于 2020年4月10日 17:08:39
  • 转载请务必保留本文链接:https://java.coder-hub.com/61137156.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定