Reputation: 1
Whenever I compile and run my code, the output becomes a big mess and I would like if someone could show me how to make my output look good.
import java.util.*;
public class JavaApplication3 {
static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
int NumPerHamper;
int NumHampersMade;
int NumItemsLeftOver;
int NumAvalable;
double ValuePerHamper;
double ItemCost;
double ValueAllotedHamper;
double ValueItemsLeftOver;
String name="";
Date TheDate = new Date();
System.out.println("Enter the number of avaliable mac and cheese");
NumAvalable = sc.nextInt();
System.out.println("Enter the number of items per hamper");
NumPerHamper = sc.nextInt();
System.out.println("Price Of Item");
ItemCost = sc.nextDouble();
NumHampersMade = NumAvalable / NumPerHamper;
NumItemsLeftOver = NumAvalable % NumPerHamper;
ValuePerHamper = NumPerHamper * ItemCost;
ValueAllotedHamper = ValuePerHamper * NumHampersMade;
ValueItemsLeftOver = NumItemsLeftOver * ItemCost;
System.out.printf("\n");
System.out.printf(TheDate + "\n");
System.out.printf("Amount of Hampers Made: ", NumHampersMade,"\n");
System.out.printf("The Items Left Over is: ", NumItemsLeftOver,"\n");
System.out.printf("Each Value Of the Hampers are: $%.2f.", ValuePerHamper,"\n");
System.out.printf("The Price Of All The Hampers Is: $%.2f.", ValueAllotedHamper,"\n");
System.out.printf("The Value Of Mac And Cheese Is: $%.2f.", ValueItemsLeftOver,"\n");
}
}
Upvotes: 0
Views: 5810
Reputation: 585
You could use %n
or \n
. Either of these should be in the first parameter in the printf()
method like this.
System.out.printf("Each Value Of the Hampers are: $%.2f. %n", ValuePerHamper);
System.out.printf("The Price Of All The Hampers Is: $%.2f.%n", ValueAllotedHamper);
System.out.printf("The Value Of Mac And Cheese Is: $%.2f.%n", ValueItemsLeftOver);
And you are missing %d
for the first 2 lines to display the values.
System.out.printf("Amount of Hampers Made: %d %n", NumHampersMade);
System.out.printf("The Items Left Over is: %d %n", NumItemsLeftOver);
Upvotes: 1
Reputation: 31699
This isn't right:
System.out.printf("Each Value Of the Hampers are: $%.2f.", ValuePerHamper,"\n");
The first argument (the format string) is the string you actually print, after replacing "format specifiers", or placeholders. Format specifiers in that string, such as %.2f
, get substituted with the other arguments. So the second argument, ValuePerHamper
, is used for the first specifier, %.2f
; and the third argument, \n
, is used for the second specifier---um, except that there aren't any other specifiers, so it doesn't get used at all.
You want to put \n
in the format string:
System.out.printf("Each Value Of the Hampers are: $%.2f.\n", ValuePerHamper);
(printf
also allows you to use %n
instead of \n
, which is more portable since it will also work on Windows systems where going to a new line needs \r\n
instead of \n
.)
Upvotes: 1