beginner101 beginner101 - 7 days ago 6
Java Question

How to insert an index of an array to another array through conditions in java

Write a java program that has 3 arrays. The first array will store lists of random integer values from 50 to 100. The program will identify passed and failed values. The second array will contain all passed values. And the third array will contain all the failed values. Required input includes size of array and passing grade. The outputs are the following: the original list, list of passed grades, and list of failed grades.
I know this is not the best code, but please do help me. I have done my research but this is all I've got.

import java.util.Arrays;
import java.util.Scanner;
public class array {

public static void main(String[] args) {

int size;
int passing;
Scanner input = new Scanner(System.in);

System.out.println("Size of Array: ");
size = input.nextInt();

System.out.println("Passing Grade: ");
passing = input.nextInt();

int [] score = new int [size];
int [] passed= new int [size];
int [] failed= new int [size];

int index = 0;
int max = 100;
int min = 50;

System.out.print("Original List:\n");
for(int a =0; a<=score.length; a++){
score[a] = min + (int)(Math.random() * ((max-min) + 1) );
System.out.print(score[a] + "\n");
}

System.out.print("Passed Values:\n");
for(int a=1; a<=score.length; a++){
if (score[a] >= passing){
int res = (passed[index++] = score[a]);
System.out.print(res + "\n");
}
}
System.out.print("Failed Values:\n");
for(int a=1; a<=score.length; a++){
if (score[a] <passing){
failed[index++] = score[a];
int res2 = (passed[index++] = score[a]);
System.out.print(res2 + "\n");
}
}
}
}

Answer

I found three main issues, for loop should check for a<score.length instead of a<=score.length. Second and third for loop initial condition should be a=0 instead of a=1. After execution of second for loop you should reset index variable to 0

public static void main(String[] args) {

            int size;
            int passing;
            Scanner input = new Scanner(System.in);

            System.out.println("Size of Array: ");
            size = input.nextInt();

            System.out.println("Passing Grade: ");
            passing = input.nextInt();

            int [] score = new int [size];
            int [] passed= new int [size];
            int [] failed= new int [size];

            int index = 0;
            int max = 100;
            int min = 50;

            System.out.print("Original List:\n");
            for(int a =0; a<score.length; a++){
                score[a] = min + (int)(Math.random() * ((max-min) + 1) );
                System.out.print(score[a] + "\n");
            }

            System.out.print("Passed Values:\n");
            for(int a=0; a<score.length; a++){
                if (score[a] >= passing){
                    int res = (passed[index++] = score[a]);
                    System.out.print(res + "\n");
                }
            }
            System.out.print("Failed Values:\n");
            index = 0;
            for(int a=0; a<score.length; a++){
                if (score[a] <passing){
                    failed[index++] = score[a];
                    int res2 = (passed[index++] = score[a]);
                    System.out.print(res2 + "\n");
                }
            }
        }