Reputation:
I have been having trouble with this assignment given to me.
You will write a program that will accept a list of 5 grocery items along with their prices. The program should calculate the total cost of the items on a line-by-line basis and format the output so it looks clean and even.
Instructions:
Here is a sample run:
Enter item #1: 2.59
Enter item #2: 5.25
Enter item #3: 6.34
Enter item #4: 4.50
Enter item #5: 8.72
Set all your width specifications to 10 and all your precision to 2, where appropriate. Instead of using any regular spaces to align your columns, only use the printf method. Use the println
method only for changing lines.
You may do all your work in the main()
method of your program.
Here is my code so far, whenever I try to run this in BlueJ
, it won't actually run the code:
import java.util.Scanner;
public class P4_Icel_Murad_GroceryList {
public P4_Icel_Murad_GroceryList() {
Scanner in = new Scanner(System.in);
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
//String a = in.nextLine();
System.out.printf("Enter item #1: ", in.nextDouble());
//System.out.printf("Enter item #2: " + in.nextLine() + "%.2f");
//System.out.printf("Enter item #3: " + in.nextLine() + "%.2f");
//System.out.printf("Enter item #4: " + in.nextLine() + "%.2f");
}
}
Upvotes: 2
Views: 1819
Reputation:
Never Mind, I was able to answer my own question: the result is
import java.util.Scanner;
public class P4_Icel_Murad_GroceryList
{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
//String a = in.nextLine();
System.out.printf("Enter item #1: ");
double Num1 = in.nextDouble();
System.out.printf("Enter item #2: ");
double Num2 = in.nextDouble();
System.out.printf("Enter item #3: ");
double Num3 = in.nextDouble();
System.out.printf("Enter item #4: ");
double Num4 = in.nextDouble();
System.out.printf("Enter item #5: ");
double Num5 = in.nextDouble();
System.out.println();
System.out.printf("%-5s", "Item:");
System.out.printf("%10s", "Cost:");
System.out.printf("%10s", "Total:\n");
System.out.printf("%5s", "#1");
System.out.printf("%10.2f", Num1);
System.out.printf("%8s",Num1);
System.out.println();
System.out.printf("%5s", "#2");
System.out.printf("%10.2f", Num2);
System.out.printf("%8s",Num1 + Num2);
System.out.println();
System.out.printf("%5s", "#3");
System.out.printf("%10.2f", Num3);
System.out.printf("%8s",Num1 + Num2 + Num3);
System.out.println();
System.out.printf("%5s", "#4");
System.out.printf("%10.2f", Num4);
System.out.printf("%8s",Num1 + Num2 + Num3 + Num4);
System.out.println();
System.out.printf("%5s", "#5");
System.out.printf("%10.2f", Num5);
System.out.printf("%8.2f",Num1 + Num2 + Num3 + Num4 + Num5);
}
}
Upvotes: 0
Reputation: 2773
As this is an assignment, I won't provide the actual code for you. However, some things to consider.
public P4_Icel_Murad_GroceryList(){
Scanner in = new Scanner(System.in);
}
This code seems a little useless... what are you planning on doing with this declared Scanner?
Also, in.nextDouble()
returns the double the user typed in. What are you doing with this value? You don't want to just print it, you want to store it somehow so you can retrieve it later to calculate the sum of all entries. Perhaps 5 different variables... but, if you haven't learned about them yet, arrays
seem like the perfect candiate for this problem. Some pseudo-code
Declare scanner
Prompt user for 5 values
Read next 5 doubles -- in.nextDouble()
Total all values, and print to screen
Upvotes: 2