Home > Blockchain >  Splitiing a string array into multiple subarrays in java based on prefix
Splitiing a string array into multiple subarrays in java based on prefix

Time:01-19

I have several string inputs(registration number of students) from the user and I have to store them into different arrays based on their year of studying and print the output. For example, if string begins with "18" i have to store it as a 4th year student. If it begins with "19" i have to store it as 3rd year student and so on.

    import java.util.Scanner;
public class reg {
    public static void main(String args[]){
    Scanner scan = new Scanner(System.in);
    System.out.println("Enter no of students");
    int n = scan.nextInt();
    scan.nextLine();
    String[] arr = new String[n];
    System.out.println("Enter registration numbers");
    for(int i = 0;i<n;i  ){
        arr[i]=scan.nextLine();

    }

    String[] arr1 = new String[n];
    String[] arr2 = new String[n];
    String[] arr3 = new String[n];
    String[] arr4 = new String[n];
    String[] arr5 = new String[n];


    for(int i = 0;i<n;i  ){
        if(arr[i].startsWith("18")){
            for(int j = 0;j<n;j  ){
                arr1[j] = arr[i];
            }
        }
        else if(arr[i].startsWith("19")){
            for(int j = 0;j<n;j  ){
                arr2[j] = arr[i];
            }
        }
        else if(arr[i].startsWith("20")){
            for(int j = 0;j<n;j  ){
                arr3[j] = arr[i];
            }
        }
        else if(arr[i].startsWith("21")){
            for(int j = 0;j<n;j  ){
                arr4[j] = arr[i];
            }
        }
        else{
            for(int j = 0;j<n;j  ){
                arr5[j] = arr[i];
            }
        }

    }
    System.out.println("4th year students are");
    for(int i = 0;i<n;i  ){
        System.out.println(arr1[i]);
        System.out.println();
    }
    System.out.println("3rd year students are");
    for(int i = 0;i<n;i  ){
        System.out.println(arr2[i]);
        System.out.println();
    }
    System.out.println("2nd year students are");
    for(int i = 0;i<n;i  ){
        System.out.println(arr3[i]);
        System.out.println();
    }
    System.out.println("1st year students are");
    for(int i = 0;i<n;i  ){
        System.out.println(arr4[i]);
        System.out.println();
    }

    }
    
}

The output I'm getting is just the last input matching my condition getting printed n times (no. of students input).

How can I solve this issue?

Edit :

Input and expected ouput :

No of students :

7

Enter registration numbers :

20ABC123

19ABC111

18ABC000

18ABC001

21ABX144

20ABX000

19ABC099

Expected Output: 4th year students are

18ABC000

18ABC001

3rd year students are

19ABC111

19ABC099

2nd year students are

20ABC123

20ABX000

1st year students are

21ABX144

CodePudding user response:

You need to introduce new 5 variable for keeping track of total student in each category so that you can put next student on next empty position in category.

Scanner scan = new Scanner(System.in);
System.out.println("Enter no of students");
int n = scan.nextInt();
scan.nextLine();
String[] arr = new String[n];
System.out.println("Enter registration numbers");
for(int i = 0;i<n;i  ){
    arr[i]=scan.nextLine();

}

String[] arr1 = new String[n];
String[] arr2 = new String[n];
String[] arr3 = new String[n];
String[] arr4 = new String[n];
String[] arr5 = new String[n];
int studntFirstYear = 0;
int studntSecondYear = 0;
int studntThirdYear = 0;
int studntForthYear = 0;
int studntElse = 0;


for(int i = 0;i<n;i  ){
    if(arr[i].startsWith("18")){
        arr1[studntForthYear] = arr[i];
        studntForthYear  ;
    }
    else if(arr[i].startsWith("19")){
        arr2[studntThirdYear] = arr[i];
        studntThirdYear  ;
    }
    else if(arr[i].startsWith("20")){
        arr3[studntSecondYear] = arr[i];
        studntSecondYear  ;
    }
    else if(arr[i].startsWith("21")){
        arr4[studntFirstYear] = arr[i];
        studntFirstYear  ;
    }
    else{
        arr5[studntElse] = arr[i];
        studntElse  ;
        
    }

}
System.out.println("4th year students are");
for(int i = 0;i<studntForthYear;i  ){
    System.out.println(arr1[i]);
    System.out.println();
}
System.out.println("3rd year students are");
for(int i = 0;i<studntThirdYear;i  ){
    System.out.println(arr2[i]);
    System.out.println();
}
System.out.println("2nd year students are");
for(int i = 0;i<studntSecondYear;i  ){
    System.out.println(arr3[i]);
    System.out.println();
}
System.out.println("1st year students are");
for(int i = 0;i<studntFirstYear;i  ){
    System.out.println(arr4[i]);
    System.out.println();
}
  •  Tags:  
  • Related