Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7749357
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T11:01:43+00:00 2026-06-01T11:01:43+00:00

Ok, I got the static error fixed. Now I am just trying to find

  • 0

Ok, I got the static error fixed. Now I am just trying to find out why I’m getting the same entries for every object (ie same name, age, weight, etc). Here is the code:

package classlab3b;

import classlab3B.BodyMassIndex;
import java.text.DecimalFormat;
import java.util.Random;
import java.util.Scanner;

/**
 *
 * @author ccity
 */
public class ClassLab3B {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        System.out.println("Please enter the number of people:");
        //asks user to input number of people
        int numberOfPeople;
        //declares integer variable for number of people
        Scanner input = new Scanner(System.in);
        //creates system input scanner
        numberOfPeople = input.nextInt();
        //captures user input for number of people
        BodyMassIndex[] a = new BodyMassIndex[numberOfPeople];
        //creates an array of BodyMassIndex the size of numberOfPeople
        String name = loadRandomNames(a);
        //loads object with random name
        int age = loadRandomAges(a, numberOfPeople);
        //loads object with random age
        double weight = loadRandomWeights(a);
        //loads object with random weight
        double height = loadRandomHeights(a);
        //loads object with random height
        createObjectsToFillArray(a, name, age, weight, height, numberOfPeople);
        //creates "x" objects to fill the array
        double BMI = BodyMassIndex.getBodyMassIndex();
        //gets BMI from getBodyMassIndex method in BodyMassIndex.java
        String status = BodyMassIndex.getStatus();
        //gets status from getStatus method in BodyMassIndex.java
        //double BMI = BodyMassIndex.bmix.getBodyMassIndex();
        //String status = BodyMassIndex.bmix.getStatus();
        printArray(a, name, age, weight, height, BMI, status);
        //prints array


        System.out.println(" ");
        System.out.println("Current Population");
        System.out.println(" ");
        System.out.println("Obese: " + BodyMassIndex.numberOfObese);
        System.out.println("Overweight: " + BodyMassIndex.numberOfOverweight);
        System.out.println("Normal: " + BodyMassIndex.numberOfNormal);
        System.out.println("Underweight: " + BodyMassIndex.numberOfUnderweight);
        System.out.println("================");
        System.out.println("Total: " + BodyMassIndex.totalNumberOfPeople);


    }

    public static void createObjectsToFillArray(BodyMassIndex[] data, String name, int age, double weight, double height, int numberOfPeople) {
        for (int i = 0; i < numberOfPeople; i++) 
            data[i] = new BodyMassIndex(name, age, weight, height);



        //creates new BodyMassIndex objects with generated variables from methods within
    }

    public static String loadRandomNames(BodyMassIndex[] data) {

        String[] arrayOfFirstNames = {"Joe", "Donna", "Ronald", "Sarah", "David", "Courtney", "Irwin", "Linda", "Michael", "Cindy", "Tom", "Rebekah", "Todd", "Tracy", "Peter", "Nicole", "Marcelo", "Jennifer", "Rick", "Andrea", "Bruce", "Jaclyn", "Doug", "Shirley", "Steve", "Liz", "Waldo", "Theresa", "Scott", "Colby", "Beth", "Larry", "Emily", "Paul", "Kate", "Sam", "Dianne", "Dustin", "Alethea", "Wayne", "Kristina", "Christian", "Danny", "Breya", "Andrew", "Alison", "Tim", "Mary", "Chris", "Susie", "Jeremy", "Willy", "Jessica", "Marcus", "Kelly", "Kyle", "Stephanie", "Isaiah", "Hillary", "Eric", "Julia", "Donald", "Meredith", "Kevin", "Leslie", "Blake", "Angela", "Cliff", "Debbie", "Dylan", "Erin", "Alex", "Monica", "Nathan", "Wendy", "Josh", "Megan", "Adam", "Michelle", "Carey", "Ashley", "Brian", "Jason", "Melanie", "Jim", "Monica", "Jamie", "Rhonda", "Steven", "Perry", "Byron", "Laura", "Harry", "Brooke", "Drew", "Vicki", "Gary", "Anita", "Felipe", "Josie"};
        String[] arrayOfLastNames = {"Smith", "Johnson", "Williams", "Jones", "Brown", "Davis", "Miller", "Wilson", "Moore", "Taylor", "Washington", "Jefferson", "Lincoln", "Hamilton", "Jackson", "Grant", "Franklin", "McKinley", "Cleveland", "Madison", "Chase", "Nicholson", "Fauver", "Doe", "Southard", "Schmidt", "Hodson", "McDonald", "Stickley", "Miller", "Combs", "Bohus", "Krippner", "Amtower", "Banks", "Wallace", "Bannister", "Dehaven", "Yost", "Still", "Timbrook", "Peters", "Vaught", "Shellhammer", "Andrews", "Krippner", "McAlister", "Wright", "Kensinger", "McClellan", "Ganoe", "Shiley", "Layman", "Gearhart", "Yost", "Kushnir", "Bush", "Lowder", "Connolly", "Lowman", "Terveen", "Staton", "Settle", "Tinsman", "Nichols", "Baker", "Walters", "Dawe", "Renner", "Michaels", "Faircloth", "Looker", "Hastings", "Vaughan", "Anderson", "Zimmerman", "Deere", "Daher", "Lauck", "Stottlemyer", "Clinton", "Obama", "Reagan", "Montgomery", "Pugh", "Gavis", "Clark", "Bowers"};

        String first = get(arrayOfFirstNames);
        String last = get(arrayOfLastNames);
        String name = first + " " + last;

        return name;
    }

    public static String get(String[] array) {
        Random generator = new Random();
        int rnd = generator.nextInt(array.length);
        return array[rnd];
    }

    public static int loadRandomAges(BodyMassIndex[] data, int numberOfPeople) {
        double min = 13;
        double max = 99;
        int age = 0;
        for (int i = 0; i < numberOfPeople; i++) 
            age = (int) randomInt(min, max);


        return age;
    }

    public static double randomInt(double min, double max) {

        double random = (double) ((max - min + 1) * Math.random() + min);
        return random;

    }

    public static double loadRandomWeights(BodyMassIndex[] data) {
        double min = 100;
        double max = 300;
        double weight = randomInt(min, max);
        for (int row = 0; row < data.length; row++) {
        }
        return weight;
    }

    public static double loadRandomHeights(BodyMassIndex[] data) {
        double min = 55;
        double max = 80;
        double height = randomInt(min, max);
        for (int row = 0; row < data.length; row++) {
        }
        return height;
    }

    public static void printArray(BodyMassIndex[] data, String name, int age, double weight, double height, double BMI, String status) {
        System.out.println("    Name           " + "Age    " + "Height    " + "Weight    " + "BMI    " + "Status");
        for (int i = 0; i < data.length; i++) {

            DecimalFormat format = new DecimalFormat();
            format.setMinimumFractionDigits(2);
            format.setMaximumFractionDigits(2);


            System.out.println(name + "      " + age + "      " + format.format(height) + "    " + format.format(weight) + format.format(BMI) + "   " + status);
        }
    }
}

I should be getting random entries but I’m only getting one. Here is the output:

run:
Please enter the number of people:
4
    Name           Age    Height    Weight    BMI    Status
Courtney Anderson      81      79.64    155.0717.19   underweight
Courtney Anderson      81      79.64    155.0717.19   underweight
Courtney Anderson      81      79.64    155.0717.19   underweight
Courtney Anderson      81      79.64    155.0717.19   underweight

Current Population

Obese: 0
Overweight: 0
Normal: 0
Underweight: 1
================
Total: 4
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T11:01:44+00:00Added an answer on June 1, 2026 at 11:01 am

    Looking at weight for example, you generate a random weight as follows:

    double weight = loadRandomWeights(a);
    

    You then pass this to the code that fills the array:

    createObjectsToFillArray(BodyMassIndex[] data, String name, int age, double weight, double height, int numberOfPeople) 
    

    Which then loops a number of times:

    for (int i = 0; i < numberOfPeople; i++) 
        data[i] = new BodyMassIndex(name, age, weight, height);
    

    So your problem is that each iteration of the loop is using the same random value. You need to be generating a new random value inside the loop so each entry in the array gets a different value:

    for (int i = 0; i < numberOfPeople; i++) {
        double weight = loadRandomWeights();
        ....
        data[i] = new BodyMassIndex(name, age, weight, height);
    }
    

    Obviously the same applies for the other values; you’ll need to generate values for them inside the loop too.

    And you have the same bug in your printArray method. It’s reprinting the same value rather than reading it off the objects you have created. In that method read the values off your BodyMassIndex objects.

    Also, I don’t think your loadRandomWeights() method other similar ones needs a data parameter as you’re neither writing to or reading from the array you pass in:

    public static double loadRandomWeights(BodyMassIndex[] data) {
        double min = 100;
        double max = 300;
        double weight = randomInt(min, max);
        for (int row = 0; row < data.length; row++) {
        }
        return weight;
    }
    

    This could just be:

    public static double generateRandomWeight() {
        double min = 100;
        double max = 300;
        return randomInt(min, max);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I Got an error While submit Object reference not set to an instance of
I got a somewhat strange error when trying to resolve the CommonDocuments directory. It
Where I try to create two static overloading methods I got an compilation error.
There is an class android.os.Build that got static variables cointaining device info, but when
I've got a static variable (abstract data type, not a primitive) in my C#
I've got a compiled static library (with an a extension) I want to include
I have got a C function in a static library, let's call it A,
I've got an open source static JavaScript + HTML application that's deployed in about
I got a Utility module since VB.NET doesn't have static class like C# and
I've got the following class: public static class Pages { public static string LoggedOut

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.