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");
		
		
		


	}
}

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.