Muldawg2020 Muldawg2020 - 2 months ago 13
Java Question

JAVA Scanner syntax error or variable...?

so here's my code:

package postage3;

import java.util.Scanner;

public class PostageUtil
{
/**
* Returns the cost of postage for a letter of the given weight.
* @param weight
* given weight in ounces
* @return
* cost of postage for the weight
*/
public static void main(String[] args)
{

Scanner scanner = new Scanner(System.in);
System.out.print("Enter weight of postage: ");
double weight = scanner.nextDouble();
double cost = 0.47;

if (weight > 1) {
cost = cost + Math.ceil(weight - 1) * 0.21;
}
if (weight > 3.5) {
cost = cost + 0.47;
}
System.out.println("the cost of the package with a weight of " + weight + " ounces is: " cost + ".");
}
}


My issue is that the word "cost" in the system.out statement (at the very bottom line) apparently is causing a syntax error and needs to be deleted. Why is that a syntax error, and how can I fix it so it prints the cost?

Answer

you missing + . The code below should work fine.

System.out.println("the cost of the package with a weight of " + weight + " ounces is: " + cost + ".")