0

I have a main class called CustomerTypeApp. It has a class variable private static float itemPrice = 0.0f. Within the main method is the following code:

String promptType = ("  To determine a discount for " + nf.format((float) itemPrice) + ", type in the... ...Select =>  ");
char customer = Validation.getChar(promptType);

I also have a class called Validation with a method getChar. The code is as follows:

public static char getChar(String prompt){
    String input;
    char choice;
    System.out.printf("  " + prompt);
    input = sc.nextLine();
    choice = input.charAt(0);
    return choice;
}

In the CustomerTypeApp class, the nf.format is obviously referring to NumberFormat. However, the professor has said we cannot use NumberFormat and instead have to use printf. The getChar method does use a printf statement. However, I am having trouble passing the value for the printf (variable?). Here's what I mean: I edited my code for CustomerTypeApp as follows:

String promptType = ("  To determine a discount for $%.2s, type in the... ...Select => ");

The problem is I have to pass in itemPrice where the $%.2s is. I tried tacking it onto the end of promptType as follows:

("  To determine...$%.2s...Select => ", itemPrice);

However, I get an error stating ")" expected and ";" expected. I also tried adding it in the getChar method as follows:

System.out.printf("  " + prompt, itemPrice);

However, for that I get an error stating Cannot find symbol.

Can anyone tell me what I'm doing wrong? Thanks!

3
  • 1
    String promptType = (" <- whats the point of the parenthesis? Is it supposed to be part of the String? Commented May 8, 2018 at 12:35
  • The professor included the parenthesis in the original code. I assume he was just keeping the string value together. It may not be necessary, but I left it on since he had put it on. In any event, if I'm trying to pass a value for the $%.2s, I would think I would need parens around the whole thing. Commented May 8, 2018 at 12:40
  • Cannot find symbol == where is the variable prompt declared (is it a variable?) Commented May 8, 2018 at 13:30

1 Answer 1

5

Use String.format("To determine...%f.2s...Select => ", itemPrice)

Sign up to request clarification or add additional context in comments.

2 Comments

That gives "To determine...0.000000.2s...Select => ". Did you mean To determine...%.2f...Select =>?
I caught that when I tested the code and fixed it. Thanks for mentioning it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.