user311280
user311280

Reputation: 31

Display an Array of user inputs

I'm new to Java and working on a program for my College course. Oddly enough i feel like my professor is doing something wrong or maybe i'm not understanding it properly. I have coded some scripts and things in the past but i'm not that great and coding applications. I lean more toward using code to automate some things. Any ways he gave us the code below and is asking us to calculate 20 inputs of peoples BMI. I feel like the code he gave us isn't working or i'm missing a step someplace in the Array loop that he has set up? I copied his code as he told us too i'm supposed to use this to then make a Jtext GUI but not sure his code is properly working.. I have tried to print results that i have inputed into the array but i don't don't get anything back.

Thank you for the help

enter code here
package bmi;


import javax.swing.* ;
import java.awt.* ;
import java.util.Arrays;
import java.util.Scanner ;

 public class Bmi {


public static void main(String[] args) {
   final int size = 20 ;
   String [] name = new String[size];
   double [] weight = new double[size];
   double [] height = new double[size];
   double [] BMI = new double[size];
   String Userquit ;
   final double BMIUnder = 18.5;
   final double BMINormal = 25.0;
   final double BMIOver  = 30.0;
   final double BMIObese = 30.0;

   int normalWeight = 0;
   int overWeight = 0 ;
   int obese = 0;
   Scanner keyboard = new Scanner(System.in);
   System.out.println("This program calculats BMI for 20 people");
   System.out.println("Please enter name, weight and height for everyone 
   firstI");

   int person = 0;
   while(person < size)
   {
       System.out.println("Enter name");
            name[person] = keyboard.nextLine();

       System.out.println("Enter weight in pounds");
            weight[person] = keyboard.nextDouble();

       System.out.println("Ener height in inches");
            height[person] = keyboard.nextDouble();

       BMI[person] = (weight[person]*703/height[person]*height[person]);

       System.out.println("Enter Y to continue or enter anything else 
       quit");
            Userquit = keyboard.next();

       if(Userquit.equals("Y")){
           person++;
       }else 
           break;
       }

       int i = 0;
       while(i < size){
           if (BMI[person] <= BMINormal)
                   {
                       normalWeight++;
                   }else if (BMI[person] <= BMIOver)
                               {
                                    overWeight++;
                      }else
                                    obese++;

                               }
                   }

       }

Upvotes: 2

Views: 221

Answers (3)

watchtower314
watchtower314

Reputation: 1

Ran this code on Eclipse, I saw one thing that needs fixing:

On the second while loop, you can see that the condition is while(i<size), but i is never used in this loop, what you actually want to do is run through the BMI array using the person variable - which IS used in this loop as an array index, so instead of

int i = 0;

you should initialize person:

person = 0;

then, at the bottom of the while loop, increase it by one

person++;

Hope it helps & good luck!

Upvotes: 0

Diyarbakir
Diyarbakir

Reputation: 2099

You have an infinite loop at the end:

    int i = 0;
    while(i < size){
        if (BMI[person] <= BMINormal)
        {
            normalWeight++;
        }else if (BMI[person] <= BMIOver)
        {
            overWeight++;
        }else
            obese++;
    }

i is always 0 and never increased so you're stuck in the while loop.

Upvotes: 0

soulcoder
soulcoder

Reputation: 301

while(i < size){
       if (BMI[person] <= BMINormal)
               {
                   normalWeight++;
               }else if (BMI[person] <= BMIOver)
                           {
                                overWeight++;
                  }else
                                obese++;

                           }
               }

   }

This part of code is weird. There is no 'i' variable incrementation so this loop will never end. By the way comparing BMI[person] where person is already incremented does not make sense because it is empty. BMI[person-1] makes sense because it stores data for person who entered data most recently. For me you are right, code is wrong.

Upvotes: 1

Related Questions