Compe
Compe

Reputation: 55

Decimal Format Causes 'void type not allowed here' error

In my toString method all of my variables were printing fine and all of my code worked with no problems.

I needed to edit to change the decimal format so that instead of my numbers printing 1E9 or whatever (a longer float number) it would actually print the non-scientific notation version (123,456,789.00).

So in my toString method I initialized a DecimalFormat object and implemented it into my values.

Before I was getting no such error regarding "'void' type not allowed here" however after implementing the decimal format it is now giving me the error for all 3 of my other variables I am trying to print.

Why is my decimalformat affecting the other variables?

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";
}
}

Upvotes: 0

Views: 415

Answers (1)

Rahmat Waisi
Rahmat Waisi

Reputation: 1330

that because decimalFormat.applyPattern() output type is void

you can use decimalFormat.format( value ) method and it`s output is String so you can use it in your toString method without any trouble.

Upvotes: 1

Related Questions