Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

1. ## Scanner to JOptionPane?

Hi all,

I'm having trouble converting from Scanner to JOptionPane.

Here is the question:

Write a program that computes tax and tip on a restaurant bill. The program should ask the user to enter the charge for a meal. The tax should be 6.75 percent of the meal charge. The tip should be 20 percent of the total after adding the tax. Display the meal charge, tax amount, tip amount, and total bill on the screen.

Here is the code I created using Scanner:

import java.util.Scanner;

public class RestaurantBill {

public static void main( String [ ] args ) {

//Declare variables
Scanner scanner = new Scanner( System.in );
double mealCost; // cost of meal
double tax; // tax of meal = 0.0675 x cost of meal
double totalCost; // total cost = meal cost + tax
double tip; // tip = 0.2 x total cost
double totalBill; // total of the bill = total cost + tip

// Prompt user to enter the cost of the meal
System.out.println( "Please enter the cost of the meal: ");
mealCost = scanner.nextDouble();

// Calculate the tax by multiplying 0.0675 x the cost of the meal.
tax = 0.0675 * mealCost;

// Calculate the total cost of the meal by adding the meal cost + tax.
totalCost = mealCost + tax;

// Calculate the tip by multipling 0.2 x the total cost.
tip = 0.2 * totalCost;

// Calculate the total bill by adding the total cost and tip.
totalBill = totalCost + tip;

// Display meal cost
System.out.println("Cost of meal: \$" + mealCost );
// Display tax amount
System.out.println("Tax: \$" + tax );
// Display tip amount
System.out.println("Tip: \$" + tip );
// Display total bill
System.out.println("Total Bill: \$" + totalBill );
}

}

I would like to have the pop up window ask the meal cost, and then be able to have 4 more pop up windows that display the meal cost, tax amount, tip amount, and total bill. Thanks so much!

2. ## Re: Scanner to JOptionPane?

What have you tried?