-2

I use toString and I print out the output like below. I want to align it like a timetable using toString, but I can't.

what I print

NAME: Poon

IC NO: 000912

TAXABLE INCOME 85000.0

STATUS :S

TAX AMOUNT: 17000.0

what I use public String toString(){ return person+"\nTAXABLE INCOME" + taxableIncome +"\nSTATUS :"+status+ "\nTAX AMOUNT: " + taxAmount ; }

Required output:

name    iCNO    taxableincome  taxableAmount

Poon    00654       6546546               465

the required output is somekind like this format I want to print

2 Answers2

0

You're looking for something like:

String.format("%20s     %05d   %10dd   %12d", name, icNumber, income, amount);

%20s means: Print a string, and if it's shorter than 20 characters, pad it out to the right by adding spaces. The formatter page explains it all. %05d means: Print a number, pad it out if it's less than 5 long, and pad it with zeroes instead of spaces. And so on.

rzwitserloot
  • 85,357
  • 5
  • 51
  • 72
0

In your Class, you first need to create Getters:

public void getName(){
return name;
}
public void geticNumber(){
return icNumber;
}
public void getincome(){
return income;
}
public void amount(){
return amount;
}

After that you can print out your desired text in your Main:

System.out.println("name     iCNO     taxableincome     taxableAmount);
System.out.printf("%s     %d     %d     %d",yourClass.getName(),yourClass.geticNumber(),yourClass.getincome(),yourClass.amount())

Alternatively you can edit your .toString in your Class like this:

String format = String.format("name     iCNO     taxableIncome     taxableAmount%n%s     %d     %d     %d",person,taxableIncome,status,taxAmount);