Home > Enterprise >  Using while loop on a user specified condition
Using while loop on a user specified condition

Time:04-06

So, this below is my code:

public class StudentRun {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String[] names = new String[50];
        int[] rolls = new int[50];
        System.out.print("Do you want to register a student ?(yes/no): ");
        String res = sc.nextLine();
        while((res.toUpperCase()).equals("YES")) {
            System.out.print("Enter the student's name: ");
            String n = sc.nextLine();
            for(int i=1; i<50; i  ) {
                names[i] = n;
            }
            System.out.print("Enter their roll number: ");
            int r = sc.nextInt();
            for(int j=0; j<50; j  ) {
                rolls[j] = r;
            }
            
        }
        for(int a=0; a<50; a  ) {
            System.out.println(names[a]);
            System.out.println(rolls[a]);
        }
        
        
    
    }
    
}

What I want is to happen is that, the program should keep registering students name and roll no. until the array is full or the user ends it. How do I do it ? I got that far

CodePudding user response:

You need to have the "continue" question in the while loop, and you don't need the for loop every time you insert a name.

public class StudentRun {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String[] names = new String[50];
        int[] rolls = new int[50];
        int index = 0;

        while(true) {
            System.out.print("Do you want to register a student ?(yes/no): ");
            String res = sc.nextLine();

            if(res.toUpperCase().equals("NO") || index == 50)
                break;

            System.out.print("Enter the student's name: ");
            String n = sc.nextLine();

            names[index] = n;

            System.out.print("Enter their roll number: ");
            int r = sc.nextInt();

            rolls[index] = r;

            index  ;
        }

        for(int i=0; i<50; i  ) {
            System.out.println(names[i]);
            System.out.println(rolls[i]);
        }
    }

}

CodePudding user response:

A common approach when using fixed sized arrays is to use a separate int variable to track the current index position for a new item, as well as the total used slots in the array:

import java.util.*;
class Main {
  
  public static void main(String[] args)
  {
    Scanner sc = new Scanner(System.in);
    int size = 50;    
    String[] names = new String[size];
    int[] rolls = new int[size];
    
    int counter = 0;
    String res = "";
    do {
      System.out.print("Do you want to register a student ?(yes/no): ");
      res = sc.nextLine().toUpperCase();
      if (res.equals("YES")) {
        System.out.print("Enter the student's name: ");
        names[counter] = sc.nextLine();

        System.out.print("Enter their roll number: ");
        rolls[counter] = sc.nextInt();
        sc.nextLine(); // clear enter out of buffer;
        
        counter  ;
      }      
    } while (counter < size && res.equals("YES"));
  
    for(int a=0; a<counter; a  ) {
        System.out.print(names[a]   " : ");
        System.out.println(rolls[a]);
    }
  }
  
}
  • Related