Ok so i am writing a program to determine a users hat,jacket, and waist size. I have most of it done but i am still getting an error. Any help would be appreciated!

import java.util.*;

public class Sizes1 {


	public static void main(String[] args) {
		int answer = 0;
		int choice = 0, height = 0, weight = 0, age = 0;
		Scanner kbd = new Scanner(System.in);
		
		getData(kbd, height, weight, age);
		do {
			choice = menu(kbd, height, weight, age);
	
			switch (choice) {
				case 1:  displayHat(weight, height); break;
				case 2:  displayJacket(weight, height); break;
				case 3:  displayWaist(weight); break;
				case 4:  System.out.println("Is there another person for which to calculate sizes?" + " " + "(Y/N)");
						 answer = kbd.nextInt();

						 if( (answer == 'Y') || (answer == 'y')) {
							menu(kbd, height, weight, age);
						 }
						 else if ((answer == 'N') || (answer == 'n')) {
								System.out.print("Thanks");
						}
			}
		} while ((answer != 'N') || (answer != 'n'));
	
	}

	public static void getData(Scanner kbd, int height, int weight, int age)
	{
		System.out.println("Enter your height (in inches):");
		height = kbd.nextInt();

		System.out.println("Enter your weight (in pounds):");
		weight = kbd.nextInt();

		System.out.println("Enter your age (in years):");
		age = kbd.nextInt();
	}

		public static int menu(Scanner kbd, int height, int weight, int age)
	{
		int choice=0;
		System.out.println("1. Calculate Hat Size");
		System.out.println("2. Calculate Jacket Size");
		System.out.println("3. Calculate Waist Size");
		System.out.println("4. No More Calculations");

		System.out.printf("\nEnter your choice:");
		choice = kbd.nextInt();
		return choice;
	}


	public static double hatSize(int weight, int height)
	{
		return (double)weight/height * 2.9;
	}

	public static void displayHat(int weight, int height)
	{
		System.out.printf("Your hat size is: %.3f inches \n" , hatSize(weight,height));
	}

	public static double jacketSize(int weight, int height)
	{
		return weight * height / 288.0;
	}


	public static void displayJacket(int weight, int height)
	{
		System.out.printf("Your jacket size is: %.3f inches \n" , jacketSize(weight,height));
	}


	public static double waistSize(int weight)
	{	
		return weight / 5.7;
	}

	public static void displayWaist(int weight)
	{
		System.out.printf("Your waist size is: %.3f inches \n" , waistSize(weight));
	}

}

Recommended Answers

All 7 Replies

i am still getting an error

Please post the full text of the error message or explain what the problem is.

It is line 20 i believe, below is a run of my program:

Enter your height (in inches):
68
Enter your weight (in pounds):
165
Enter your age (in years):
20
1. Calculate Hat Size
2. Calculate Jacket Size
3. Calculate Waist Size
4. No More Calculations

Enter your choice:1
Your hat size is: NaN inches
1. Calculate Hat Size
2. Calculate Jacket Size
3. Calculate Waist Size
4. No More Calculations

Enter your choice:

What is the error?

When i run the program it says there is an error yet the program never crashes and shows the error. But Where it says "Your hat size: NaN inches", it should be "Your hat size: 7.037 inches"

Add some printlns in your code to print out all the values used in computing the hatsize.
Some of them must be bad.

public static void getData(Scanner kbd, int height, int weight, int age) {
  System.out.println("Enter your height (in inches):");
  height = kbd.nextInt();

The variable "height" here is a parameter in the getData method. It's not the same thing as the other "height" that you declared earlier. It's a new separate variable. You get a value from the user and put it in the parameter height, but that "height" variable is discarded at the end of the method.
Basically, in Java, you can never change the value of a primitive (int/double/boolean) etc by passing it as a parameter.
If you just change the method to

public static void getData(Scanner kbd) {
  System.out.println("Enter your height (in inches):");
  height = kbd.nextInt();

then the variable "height" that you refer to will be the original one from the main program, which is what you want.

BUT

The original height variable is defined inside the method "main", so that's not accessible outside that method.
The variables like height that you want to share between your methods need to be declared inside the class, but outside any single method. Because the methods are static, you need to make the variables static as well

Member Avatar for hfx642

There is a VAST difference between an error and the fact that it doesn't work!

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.