0

I am new to java and I am currently trying to make a program that uses an array of 10 inputted names and ages. What I want to do is add an option so that if the user types "done" when prompted to enter a name, the program will skip straight to listing the names and ages already entered.

Code:

import java.util.Arrays;

public class array2 {
    public static void main(String[] args) {
        java.util.Scanner input = new java.util.Scanner(System.in);

        input.useDelimiter(System.getProperty("line.separator"));

        int numofpeople = 10;
        Person[] persons = new Person[numofpeople];

        for (int i = 0; i < numofpeople; i++) {
            System.out.print("Enter the person's name: ");
            String person = input.next();
            System.out.print("Enter the persons's age: ");
            int age = (Integer) input.nextInt();
            persons[i] = new Person(person, age);
        }
        
        Arrays.sort(persons);

        System.out.print("Name" + "\tAge");
        System.out.print("\n----" + "\t----\n");

        for (int i = 0; i < persons.length; i++) {
            System.out.println(persons[i].person + "\t" + persons[i].age);
        }

        System.out.println("The oldest person is: " + persons[numofpeople-1].person);
        System.out.println("The youngest person is: "+ persons[0].person);

    }

}

class Person implements Comparable<Person> {
    public String person;
    public Integer age;
    public Person(String s, Integer g) {
        this.person = person;
        this.age = g;
    }

    @Override
    public int compareTo(Person o) {
        return (this.age>o.age?1:-1);
    }
}

What I'm thinking is that I need to use a boolean if statement that defines whether or not done has been entered, and if it has, then the program skips asking the user for the rest of the names and ages and instead jumps to printing the already entered ones. I am not sure on this so, any help would be appreciated!

5
  • Sounds like you are are most of the way to a solution. If the user types "Done" and your boolean check is true, what I would do as the simplest thing would be to "break" statement out of the loop using a break. You might be able to come up with a neater solution using a do...while or while loop Commented Nov 6, 2020 at 10:14
  • 1
    You are correct, basically - you need an if condition to check for the expected value. To compare Strings, use the equals-method. Something like if(person.equals("done") { break; }. Commented Nov 6, 2020 at 10:15
  • Use List<Person> and prompt within while loop and add it to the list. When the input is -1 or any other stuff like "done" break your while loop. Commented Nov 6, 2020 at 10:15
  • As an aside, nextInt() returns an int so the cast to Integer is unnecessary. Commented Nov 6, 2020 at 10:24
  • Does this answer your question? Trying to finish an array early using a keyword? Commented Nov 6, 2020 at 20:29

3 Answers 3

2

Your thought is correct, the simplest way would be checking if person is equal to "done". If this is true, break the loop and code should continue, and it should produce the result you want.

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

Comments

0

You can do comething like this:

for (int i = 0; i < numofpeople; i++) {
            System.out.print("Enter the person's name: ");
            String person = input.next();

            if (!person.equals("done")) {
                System.out.print("Enter the persons's age: ");
                int age = (Integer) input.nextInt();
                persons[i] = new Person(person, age);
            } else {
                //print table or enter here a break; directive
            }
        }

If user enter done instead of any name, your program will straight to listing the names and ages already entered.

3 Comments

Your example would (in my opinion) be more readable with if you flipped the logic of your if statement. That way you only need the if branch (which would break out of the loop)
This works but, when done is entered, I get the error: Exception in thread "main" java.util.InputMismatchException at java.base/java.util.Scanner.throwFor(Scanner.java:939) at java.base/java.util.Scanner.next(Scanner.java:1594) at java.base/java.util.Scanner.nextInt(Scanner.java:2258) at java.base/java.util.Scanner.nextInt(Scanner.java:2212) at array2.main(array2.java:17)
This is unnecessary complication, checking if person equals "done" after each input and breaking out of the loop just like I posted in my answer is much cleaner.
0

this also jumps out of the for loop and moves on to printing the list if the user types in "done":

    for (int i = 0; i < numofpeople; i++) {
        System.out.print("Enter the person's name: ");
        String person = input.next();

        if(person == "done"){break;}

        System.out.print("Enter the persons's age: ");
        int age = (Integer) input.nextInt();
        persons[i] = new Person(person, age);
    }

5 Comments

I used this code for (int i = 0; i < numofpeople; i++) { System.out.print("Enter the person's name: "); String person = input.next(); if(person.equals("done")){break;} System.out.print("Enter the persons's age: "); int age = (Integer) input.nextInt(); persons[i] = new Person(person, age); } and ran it but when I type done it does not list the values previously entered?
there is a mistake in your Person class. Should be : this.person = s; you are not storing the values correctly.
oh I have that in my actual code I just forgot to edit it. My apologies, but when I run the code even with that it still doesnt dispaly the results of what I previously entered when I type "done".
That's a different problem. doesn't have anything to do with this question you asked. are you getting any crashes? how many names did you enter before inputting "done"?
around 3-4 inputs after

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.