Home > Blockchain >  Avoid Adding Null Value to Array
Avoid Adding Null Value to Array

Time:12-01

I am making a function that gets the students' Id, score, last name, and first name and compares them according to their Score, then the last name, and then the first name and prints the result in the descending format.

I have used a Comparator to compare the elements of the array. The problem is that when the user enters -1 as score or ID, the program needs to break and show the result but when I use break, it will add a null value to my array which prevents comparing.

This is my code:

import java.util.Arrays;
import java.util.Scanner;
import java.util.*;

public class Main {
    private static class Student {
        String fName;
        String lName;
        int id;
        int score;

        public Student(String fName, String lName, int id, int score) {
            this.fName = fName;
            this.lName = lName;
            this.id = id;
            this.score = score;
        }

        public int getScore() {
            return score;
        }

        public String getFirstName() {
            return fName;
        }

        public String getLastName() {
            return lName;
        }

        public int getId() {
            return id;
        }

        @Override
        public String toString() {
            return "id: "   this.id   " "   "fName: "   this.fName   " "   "lName: "   this.lName   " "   "score: "   this.score;
        }

    }

    public static boolean alphabetic(String str) {
        char[] charArray = str.toCharArray();
        for (char c : charArray) {
            if (!Character.isLetter(c)) {
                if (c != ' ') {
                    return false;
                }
            }

        }
        return true;
    }

    static class Comparators implements Comparator<Student> {
        @Override
        public int compare(Student s1, Student s2) {
            int n = Integer.compare(s2.getScore(), s1.getScore());
            if (n == 0) {
                int last = s1.getLastName().compareTo(s2.getLastName());
                return last == 0 ? s1.getFirstName().compareTo(s2.getFirstName()) : last;

            } else {
                return n;
            }

        }
    }

    public static void main(String[] args) {
        System.out.println("Enter the number of students");
        Scanner input = new Scanner(System.in);
        HashSet<Integer> used = new HashSet<>();
        List<List<Object>> listData = new ArrayList<List<Object>>();
        int k = input.nextInt();
        Student[] students = new Student[k];
        for (int i = 0; i < k; ) {
            System.out.println("Enter id");
            int id = input.nextInt();
            if (id == -1) {
                break;
            }
            input.nextLine();
            System.out.println("Enter first name ");
            String fName;
            while (alphabetic(fName = input.nextLine()) == false) {
                System.out.println("Wrong! please enter again");
            }
            fName = fName.replace(" ", "");
            System.out.println("Enter last name ");
            String lName;
            while (alphabetic(lName = input.nextLine()) == false) {
                System.out.println("Wrong! please enter again");
            }
            lName = lName.replace(" ", "");
            System.out.println("Enter score ");
            int score = input.nextInt();
            if (score == -1) {
                break;
            }
            if (used.contains(id)) {
                listData.add(Arrays.asList(id, lName, fName, score));
                continue;
            }
            used.add(id);
            students[i  ] = new Student(fName, lName, id, score);

        }
        Arrays.toString(students);
        System.out.println(Arrays.toString(students));
        Arrays.sort(students, new Comparators());
        Arrays.toString(students);
        System.out.println("Data without duplication:");
        for (int i = 0; i < k; i  ) {
            System.out.println(students[i]);
        }
        if (listData.toArray().length == 0) {
            System.out.println("No duplicated Data");

        } else {
            System.out.println("Data with duplication:");
            System.out.println("ID/ LastName/Name/ Score");
            System.out.println(Arrays.toString(listData.toArray()));
        }

I have tried nullhandlingExpectation methods but I could not get the result I want.

Is there any method to avoid adding the null value to the array while breaking, or remove the null from the array before comparing?

CodePudding user response:

you can put it inside a while loop and put all your code in it and put the condition of breaking it if the value is -1

CodePudding user response:

Note, your array is of predefined size k:

Student[] students = new Student[k];

Therefore, if user needs to break input before entering all k values, you have to truncate the array. E.g.:

            if (score == -1) {
                students = Arrays.copyOf(students, i);
                break;
            }

But better consider using ArrayList instead of plain array

  • Related