0

I am semi-beginner in java

This is my code

        while (true) {
        System.out.println("Choose the number");
        System.out.println("1");
        System.out.println("2");
        System.out.println("3");
        System.out.println("4 for Display summary and Exit");

        int cho = input.nextInt();

        System.out.println("Registration");

        System.out.println("Enter your full name (first and lastname)");
        String name = input.next();

        System.out.println("Enter your National ID");
        int id = input.nextInt();

        System.out.println("Enter your phone number");
        int phone = input.nextInt();

I made if statement for all the choices 1,2,3,4

And this is number 4 if statement

            else if (cho == 4) {
            System.out.println("Summary");
        }

I want in this loop if user Choice 4 in the main menu prints a summary of all registered users (name, phone, id) that choices 1,2,3, even if the user made more than one register, and then exits the program.

5
  • Did you try List<String> or String[] array? This is what I would start - anyway, you have to group somehow the data which you refer in some type of records - like objects later on. First try with something simple - adding single String (name only) in the loop to the List, and later on in the 4th case simply show all of it. Commented Mar 5, 2019 at 21:11
  • do you need to persist the users, or is just an in memory database? If first, try something simple, like writing to a text file. Commented Mar 5, 2019 at 21:13
  • This code really doesn't make sense. You are in an infinite loop that means you can never exit this code cleanly. You are not storing any of the data collected because it will be cleared down every time you go around the loop and it looks like data points 1, 2, and 3 all belong to the same user, Surely you want to collect the whole set rather than just 1 piece of the set. I guess what I'm trying to say is what is the intent behind this code? Commented Mar 5, 2019 at 21:14
  • I am asking how to do that even with another way other than if I am totally beginner can I do it with a loop inside the loop or some simple way? Commented Mar 5, 2019 at 21:22
  • Please don't vandalise your posts, other users have taken the time and effort to help you solve your problem. Any vandalism will be reverted. Commented Mar 6, 2019 at 8:40

1 Answer 1

1

You should use a class like Person to store each person at each round of loop, like

class Person{
    String name;
    int id, phone;
    // Constructor with the 3 attributes
    // toString() method implemented
}

And when use ask for 4 print all the list

List<Person> peoples = new ArrayList<>()
while (true) {
    System.out.println("Choose the number\n1\n2\n3\n4 for Display summary and Exit");
    int cho = input.nextInt();

    System.out.println("Registration");

    System.out.println("Enter your full name (first and lastname)");
    String name = input.next();

    System.out.println("Enter your National ID");
    int id = input.nextInt();

    System.out.println("Enter your phone number");
    int phone = input.nextInt();

    Person p = new Person(name, id, phone);
    peoples.add(p)
    //...
    if( cho == 4){
        peoples.forEach(System.out::println);
        break;
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you azro but is there a simple way like a loop inside the loop or some edit with the main loop , my school friend told me to use string + string I don't know what is that.

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.