I can't find this typing error. Somewhere on Line 44.

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

public class Weight extends JFrame
	implements ActionListener {

	private JButton button;

	public static void main(String[] args) {
		Weight frame = new Weight();
		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("Calculate Weight Loss");
		window.add(button);
		button.addActionListener(this);
}
	public void actionPerformed(ActionEvent event) {
			
		int bikeHours, joggingHours, swimmingHours, weightLoss;
		int bikeCalFinal, jogCalFinal, swimCalFinal;
		int bikeCal = 200, jogCal = 475, swimCal = 275;
		String bikeString;
		String joggingString;
		String swimmingString;
		

		bikeString = JOptionPane.showInputDialog("Hours cycling: ");
		bikeHours = Integer.parseInt(bikeString);
		bikeCalFinal = bikeHours * bikeCal;
		joggingString = JOptionPane.showInputDialog("Hours jogging: ");
		joggingHours = Integer.parseInt(joggingString);
		jogCalFinal = joggingHours * jogCal;
		swimmingString = JOptionPane.showInputDialog("Hours swimming: ");  // This is where I get the error. The line above. Line 44
		swimmingHours = Integer.parseInt(swimmingHours);
		swimCalFinal = swimmingHours * swimCal;
		weightLoss = (bikeCalFinal + jogCalFinal + swimCalFinal) / 3500;
		JOptionPane.showMessageDialog(null, weightLoss + "pounds were lost");
		
		
		


	}
}

Dani AI

Generated

Two separate problems appeared in the thread: a parse/variable mixup that caused the compile-time error, and integer arithmetic that produced truncated results. correctly pointed out the parse bug; was on the right track suggesting a floating parse but wrote the wrong classname; ’s general advice about using floating types is also relevant.

The compile error: a String-parsing method was being given an int variable instead of the String from the input dialog. The correct parse method for fractional input is Double.parseDouble(...) (not Integer.parseDouble). After parsing to a numeric type, keep calculations in a floating type so fractional hours survive.

The precision/truncation: dividing two ints performs integer division first, then the result is converted to double — the fractional part is already lost. Fixes include making at least one operand a double (e.g., 3500.0) or casting the numerator to double. A minimal, safe pattern:

double bikeHours = Double.parseDouble(bikeInput.trim());
double bikeCalories = bikeHours * 200.0;
double totalCalories = bikeCalories + jogCalories + swimCalories;
double weightLoss = totalCalories / 3500.0;
JOptionPane.showMessageDialog(null, String.format("%.3f pounds were lost", weightLoss));

For robustness, validate and re-prompt on bad input with a try/catch loop:

private double promptDouble(String message) {
    while (true) {
        String s = JOptionPane.showInputDialog(message);
        if (s == null) return 0.0; // handle Cancel as needed
        try { return Double.parseDouble(s.trim()); }
        catch (NumberFormatException ex) {
            JOptionPane.showMessageDialog(null, "Please enter a number like 2.5");
        }
    }
}

Additional tips: keep constants (calories-per-hour, CALORIES_PER_POUND = 3500.0) and types consistent, and format output with String.format or DecimalFormat for predictable decimal places.

Recommended Answers

All 8 Replies

what's the error message?

swimmingString = JOptionPane.showInputDialog("Hours swimming: "); // This is where I get the error. The line above. Line 44
swimmingHours = Integer.parseInt(swimmingHours);

if i am not wrong ur reading the hours in to swimmingString and trying to parse swimmingHours which is already an int... please check that

Alright it's fixed but now I don't get an exact readout. I get whole numbers but no decimal numbers. If you put in 10 hours for each one it should be like 2.714 but it's rounded to 2.

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

public class Weight extends JFrame
	implements ActionListener {

	private JButton button;

	public static void main(String[] args) {
		Weight frame = new Weight();
		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("Calculate Weight Loss");
		window.add(button);
		button.addActionListener(this);
}
	public void actionPerformed(ActionEvent event) {
			
		int bikeHours, joggingHours, swimmingHours;
		double weightLoss;
		int bikeCalFinal, jogCalFinal, swimCalFinal;
		int bikeCal = 200, jogCal = 475, swimCal = 275;
		String bikeString;
		String joggingString;
		String swimmingString;
		

		bikeString = JOptionPane.showInputDialog("Hours cycling: ");
		bikeHours = Integer.parseInt(bikeString);
		bikeCalFinal = bikeHours * bikeCal;
		joggingString = JOptionPane.showInputDialog("Hours jogging: ");
		joggingHours = Integer.parseInt(joggingString);
		jogCalFinal = joggingHours * jogCal;
		swimmingString = JOptionPane.showInputDialog("Hours swimming: ");
		swimmingHours = Integer.parseInt(swimmingString);
		swimCalFinal = swimmingHours * swimCal;
		weightLoss = (bikeCalFinal + jogCalFinal + swimCalFinal) / 3500;
		JOptionPane.showMessageDialog(null, weightLoss + " pounds were lost");
		
		
		


	}
}

Alright it's fixed but now I don't get an exact readout. I get whole numbers but no decimal numbers. If you put in 10 hours for each one it should be like 2.714 but it's rounded to 2.

So just use parseDouble()

So the line would look like?

Integer.parseDouble(), where ever you changed the line

weightLoss = (float)(bikeCalFinal + jogCalFinal + swimCalFinal) / 3500;

As every variable u read was integer ... u were getting only rounded values, so type cast it to float. If u want to read decimal values instead of integer then u need to use Float.parseFloat() for every read statement.

Yes, I have just read several posts in the last 20 minutes and almost all of them have the same problem, people using int for any number without thinking about just what an int is...

int, long, double float etc. to each their own and long live the int...

I recommend using the suggestion to read floats, because if you read only integers then you can't enter 10.1 or 10.5 ... If you really don't care, then read float, store float and calculate float, as the type conversions between numbers are both un-necessary overhead and dangerous to your numbers...due to bit/byte lenghts of the different types...

Be like Nancy and Just Say No, to unnecessary numeric type conversions ;-)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.