I get a tip of $8.0 when I want $8.4. What am I missing here?

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Tip extends JFrame
	implements ActionListener {

	private JButton button;

	public static void main(String[] args) {
		Tip frame = new Tip();
		frame.setSize(400, 300);
		frame.createGUI();
		frame.setVisible(true);

}

	private void createGUI() {
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		Container window = getContentPane();
		window.setLayout(new FlowLayout() );

		button = new JButton("Compute Tip");
		window.add(button);
		button.addActionListener(this);
}
	public void actionPerformed(ActionEvent event) {

		int amount, percentage;
		double tip;
		String occupationString;
		String amountString;
		String percentageString;

		occupationString = JOptionPane.showInputDialog("Person Occupation: ");
		amountString = JOptionPane.showInputDialog("Amount of the Bill: ");
		amount = Integer.parseInt(amountString);
		percentageString = JOptionPane.showInputDialog("Percentage tip: ");
		percentage = Integer.parseInt(percentageString);
		tip = (amount * percentage) / 100;
		JOptionPane.showMessageDialog(null, "Tip the " + occupationString + " " + "$" + tip);
		
		


	}
}

Hi what values are you using for the calculations, also you should use a doube instead of an int

Ah yes sorry. I was using 56 for amount and 15 for percentage.

I get a tip of $8.0 when I want $8.4. What am I missing here?

tip = (amount * percentage) / 100;

Bam, right there, you have an int times an int divided by an int. Simply change 100 to 100.00 and that should do it.

I get a tip of $8.0 when I want $8.4. What am I missing here?

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
 
public class Tip extends JFrame
    implements ActionListener {
 
    private JButton button;
 
    public static void main(String[] args) {
        Tip frame = new Tip();
        frame.setSize(400, 300);
        frame.createGUI();
        frame.setVisible(true);
 
}
 
    private void createGUI() {
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        Container window = getContentPane();
        window.setLayout(new FlowLayout() );
 
        button = new JButton("Compute Tip");
        window.add(button);
        button.addActionListener(this);
}
    public void actionPerformed(ActionEvent event) {
 
        int amount, percentage;
        double tip;
        String occupationString;
        String amountString;
        String percentageString;
 
        occupationString = JOptionPane.showInputDialog("Person Occupation: ");
        amountString = JOptionPane.showInputDialog("Amount of the Bill: ");
        amount = Integer.parseInt(amountString);
        percentageString = JOptionPane.showInputDialog("Percentage tip: ");
        percentage = Integer.parseInt(percentageString);
        tip = (amount * percentage) / 100;
        JOptionPane.showMessageDialog(null, "Tip the " + occupationString + " " + "$" + tip);
 
 
 
 
    }
}

Hi,

Just look at the comments and changes I made to make this program run in right manner.


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Tip extends JFrame implements ActionListener {
private JButton button;
public static void main(String[] args) {
Tip frame = new Tip();
frame.setSize(400, 300);
frame.createGUI();
frame.setVisible(true);
}
private void createGUI() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container window = getContentPane();
window.setLayout(new FlowLayout());
button = new JButton("Compute Tip");
window.add(button);
button.addActionListener(this);
}
public void actionPerformed(ActionEvent event) {
/**
* Amount can be integer but since you'r dividing this by 100 you need
* to keep percentage field to be double else it will end up with an
* integer division. Thats what the problem was...
*/
double percentage;
int amount;
double tip;
String occupationString;
String amountString;
String percentageString;
occupationString = JOptionPane.showInputDialog("Person Occupation: ");
amountString = JOptionPane.showInputDialog("Amount of the Bill: ");
amount = Integer.parseInt(amountString);
percentageString = JOptionPane.showInputDialog("Percentage tip: ");
percentage = Double.parseDouble(percentageString);
/** Also previous statement was correct "tip = (amount*percentage)/100" this will definately work, no problem with this,
* but be careful with the field types. Because amount is integer and percentage is double... */
tip = amount * (percentage / 100);
JOptionPane.showMessageDialog(null, "Tip the " + occupationString + " " + "$" + tip);
}
}

This question has already been answered. Start a new discussion instead.