0

I am a little stuck with my coursework in JAVA. Could you help me?

let's say you want to write a program that repetitively asks the names of people and their age and stops when the string FINISH is entered, then prints out the name and the age of the youngest person of all the people entered. And if 2 or more people have the same age, jsut write the name of the last person of that age entered. How can you ask JAVA to select that person, without the use of arrays, but only while loops, if statements and for loops.

Here is the example, starting from the method.

String name = "";

int age = 0;

while (!name.equals ("finish))    

{ 
   name = JOptionPane.showInputDialog ("Give name: ");

   String ageText = JOptionPane.showInputDialog ("Give age: ");

   int age = Integer.parseInt (ageText);    
}

JOptionPane.showMessageDialog ("the yougest person is " + age + ". It's " + name + ".");

An example run of the program:

Give name: Lucy Give age: 52

Give name: Paul Give age: 29

Give name:John Give age: 28

Give name: Mary Give age: 31

Give name: finish

The youngest person is 28. It's John.

Please note how string "finish" is not printed out at the end.

I have been thinking trying to find a way of doing that for 8 hours. I beg you help please.

Kind regards,

2
  • 1
    you need another variable to hold the value of the oldest persons name, and another one to hold the age. Each time thru the loop you need to decide whether to set these two variables. Commented Nov 6, 2011 at 2:08
  • I have done that. It seemed to work for the integer values but does not work for the string value.... :-(I divided the program into methods (functions and procedures) How would you write the solution code please? Commented Nov 6, 2011 at 2:18

3 Answers 3

1

You need to add more variables to keep track of what is currently known to be the youngest age and the person associated with it:

String name="", youngName;
int age=Integer.MAX_VALUE, youngAge;

while (!name.equals("FINISH")) {
    name = JOptionPane.showInputDialog("Enter a name: ");
    age = Integer.parseInt(JOptionPane.showInputDialog("Enter " + name + "'s age: "));

    if(age <= youngAge && !name.equals("FINISH")) {
        youngAge = age;
        youngName = name;
    }
}

JOptionPane.showMessageDialog("The youngest age is: " + youngAge + ".  It's " + youngName + ".");

Be careful that the program will break if you input something that isn't a number into the age field, and does not account for the fact that there might be two or more people with the lowest age.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you soooo much this has helped me a lot! I have just learnt about integer.MAX_VALUE and MIN_VALUE now... Thanks a lotttt :-)
You can easily make sure you get the last entered person's name and age that is lowest (of equal to the lowest) by changing the < sign to <=.:)
@MH. No matter which one you use, if there are two or more people with the same age, and this age is the lowest one, only one name will be outputted. EDIT: Oops, didn't read the question all the way through, you're right :)
1

Within your while loop, compare the given age with the current lowest one. If it's less than or equal to the current lowest, replace the current age with the one give and also store the name.

String minName = "";
int minAge = Integer.MAX_VALUE;
while (!name.toLowerCase().equals("finish"))    { 
    //fetch name and age
    int age = ...
    String name = ...
    if (age <= minAge) {
        minAge = age;
        minName = name;
    }
}

When the loop breaks, you will have the lowest age and matching name given. Note that you should initialize minAge with a value that will reasonably be replaced by the first one entered - e.g. you could have safely assumed a person to not have an age over 150 years.

1 Comment

Thanks so much your explanation is so clear. Thanks thanks thanks :-).
0

Why not just keep an int variable with the current "oldest age", and a second string variable for the corresponding name?

1 Comment

that's one way of doing it. Thanks a lot :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.