简体   繁体   中英

How to take user input multiple times and store it in the same array list?

I am a beginner, trying to make a basic hotel management system.

    Scanner sc = new Scanner(System.in);
System.out.print("Number of guests: ");
int no = sc.nextInt();
String[] arr = new String[no];

while(true)
{
  Scanner input = new Scanner(System.in);
  System.out.println("Welcome, How may I help you");
  String req = input.nextLine();

  if(req.equals("register"))
  {
    System.out.println("Enter name:");

    for(int i=0;i<no;i++)
    {
      arr[i] = input.nextLine();
    }
  }
  else if(req.equals("check"))
  {
    Scanner out = new Scanner(System.in);
    System.out.print("Search name: ");
    String ser = out.nextLine();
    int log=0;

    for(int j=0;j<no;j++)
    {
      if(arr[j].equals(ser))
      {
        System.out.println(ser + " is present");
        log++;
      }
    }
    if(log==0)
    {
      System.out.println(ser + " is absent");
    }
  }
  else if(req.equals("exit"))
  {
    System.out.println("\nBye...\n");
    System.exit(0);
  }
}

} }

I added a while loop so that the program never ends on its own and give user the authority to end the program.

       else if(req.equals("exit"))
  {
    System.out.println("\nBye...\n");
    System.exit(0);
  }

I want this program to take input again after the loop repeats and store the input in the same array with the previously added elements.

Any suggestion will be appreciated

I am a beginner, trying to make a basic hotel management system.

    Scanner sc = new Scanner(System.in);
System.out.print("Number of guests: ");
int no = sc.nextInt();
String[] arr = new String[no];

while(true)
{
  Scanner input = new Scanner(System.in);
  System.out.println("Welcome, How may I help you");
  String req = input.nextLine();

  if(req.equals("register"))
  {
    System.out.println("Enter name:");

    for(int i=0;i<no;i++)
    {
      arr[i] = input.nextLine();
    }
  }
  else if(req.equals("check"))
  {
    Scanner out = new Scanner(System.in);
    System.out.print("Search name: ");
    String ser = out.nextLine();
    int log=0;

    for(int j=0;j<no;j++)
    {
      if(arr[j].equals(ser))
      {
        System.out.println(ser + " is present");
        log++;
      }
    }
    if(log==0)
    {
      System.out.println(ser + " is absent");
    }
  }
  else if(req.equals("exit"))
  {
    System.out.println("\nBye...\n");
    System.exit(0);
  }
}

} }

I added a while loop so that the program never ends on its own and give user the authority to end the program.

       else if(req.equals("exit"))
  {
    System.out.println("\nBye...\n");
    System.exit(0);
  }

I want this program to take input again after the loop repeats and store the input in the same array with the previously added elements.

Any suggestion will be appreciated

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM