В моем методе toString все мои переменные печатались нормально, и весь мой код работал без проблем.
Мне нужно было отредактировать, чтобы изменить десятичный формат, чтобы вместо моих чисел, печатающих 1E9 или что-то еще (более длинное число с плавающей запятой), он фактически печатал версию ненаучной записи (123 456 789,00).
Итак, в моем методе toString я инициализировал объект DecimalFormat и внедрил его в свои значения.
Раньше я не получал такой ошибки относительно «типа 'void' здесь не разрешен», однако после реализации десятичного формата он теперь дает мне ошибку для всех 3 моих других переменных, которые я пытаюсь напечатать.
Почему мой десятичный формат влияет на другие переменные?
import java.text.DecimalFormat;
public class Project {
String projName; //Name of a project
int projNumber; //Number of a project
String projLocation; //Location of a project
Budget projBudget; //Budget of a project
public Project(double amount) {
this.projName = "?";
this.projNumber = 0;
this.projLocation = "?";
Budget thisBudget = new Budget(amount);
this.projBudget = thisBudget;
}
public String getName(){
return projName;
}
public int getNumber() {
return projNumber;
}
public String getLocation() {
return projLocation;
}
public Budget getBudget() {
return projBudget;
}
public void setName(String aName) {
projName = aName;
}
public void setNumber(int aNumber) {
projNumber = aNumber;
}
public void setLocation(String aLocation) {
projLocation = aLocation;
}
public boolean addExpenditure(double amount) {
return projBudget.addSpending(amount);
}
public String toString() {
String format = "###,###.##";
DecimalFormat decimalFormat = new DecimalFormat(format);
return "\nProject Name:\t\t" + getName() + "\nProject Number:\t\t" + getNumber() + "\nProject Location:\t\t" + getLocation() + "\nBudget:\nInitial Funding\t$" + decimalFormat.applyPattern(String.valueOf(projBudget.initialFunding)) + "\nSpending\t\t$" + decimalFormat.applyPattern(String.valueOf(projBudget.spending)) + "\nCurrent Balance\t$" + decimalFormat.applyPattern(String.valueOf(projBudget.currentBalance)) +"\n\n";
}
}
Budget thisBudget = new Budget(amount); this.projBudget = thisBudget;
можно заменить наthis.projBudget = new Budget(amount);
. - person MC Emperor   schedule 22.01.2016