This program compiles and runs perfectly if I remove '%%' from the 'toString()' method. However, if I display the percentage symbol, my program crashes right when the 'toString()' method is called (after asking for input). I failed to resolve this issue. Can anybody help me?
I am running this on VSCODE.
// FA2022_Investor_Souza.java
// Data type class
public class FA2022_Investor_Souza {
// Fields
private String name;
private int shareAmount;
private float sharePrice;
private float yearlyDividend;
// No-arg constructor
public FA2022_Investor_Souza()
{
name = "";
shareAmount = 0;
sharePrice = 0f;
yearlyDividend = 0f;
}
// Parameterized constructor
public FA2022_Investor_Souza(String n, int a, float p, float y)
{
name = n;
shareAmount = a;
sharePrice = p;
yearlyDividend = y;
}
// Method to calculate the savings money
public float savingsMoney()
{
float moneyInvested = sharePrice * shareAmount;
float interestAmount = moneyInvested * yearlyDividend * 0.01f;
float moneyReturned = moneyInvested + interestAmount;
return moneyReturned;
}
// Method to display the information
public String toString ()
{
float moneyInvested = sharePrice * shareAmount;
float interestAmount = moneyInvested * yearlyDividend * 0.01f;
return "-------------------------------------------------------\n" +
"FA2022_ShareInvestmentCalculator_Souza.java\n" +
"FALL 2022 semester - MATHEUS SOUZA\n" +
"-------------------------------------------------------\n" +
String.format("%-32.32s%15s\n", "Name of investor: ", name) +
String.format("%-32.32s%15d\n", "Number of shares: ", shareAmount) +
String.format("%-32.32s%15.2f\n", "Price of each share: ", sharePrice) +
String.format("%-32.32s%15.2f%%\n", "Percentage of yearly dividend: ", yearlyDividend) +
String.format("%-32.32s%15.2f\n", "Money Invested: ", moneyInvested) +
String.format("%-32.32s%15.2f\n", "Interest Amount: ", interestAmount) +
"-------------------------------------------------------\n" +
String.format("%-32.32s%15.2f\n", "Total money at the end of year: ", savingsMoney());
}
}
// FA2022_ShareInvestmentCalculator_Souza.java
// Driver class
// Importing Scanner class
import java.util.Scanner;
public class FA2022_ShareInvestmentCalculator_Souza {
public static void main (String [] args)
{
// Creating object for Scanner class
Scanner scanner = new Scanner(System.in);
// Asking for the user's info
System.out.printf("What is your name? ");
String name = scanner.nextLine();
System.out.printf("What is the number of shares? ");
int number = scanner.nextInt();
System.out.printf("What is the price of a share? ");
float price = scanner.nextFloat();
System.out.printf("What is the yearly dividend? ");
float dividend = scanner.nextFloat();
// Creating object for FA2022_Investor_Souza class using parameterized constructor
FA2022_Investor_Souza investor = new FA2022_Investor_Souza(name, number, price, dividend);
// Displaying the result by accessing the method toString()
System.out.printf(investor.toString());
}
}
Relating to this line of your code
String.format("%-32.32s%15.2f%%\n", "Percentage of yearly dividend: ", yearlyDividend)
You want to add another % as a string after the yearlyDividend, correct?
Then you need to tell in the format string that another string is following ( %s ). Then add the "%%" as string as another parameter, because %'s need to be escaped by another % to be shown as the % sign.
So the working code would look like this:
String.format("%-32.32s%15.2f%s\n", "Percentage of yearly dividend: ", yearlyDividend, "%%")
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.