Zero question needs explanation

I am new to programming and Java, and this is my first zero, I'm a bit confused because I don’t know what happened, are these errors in coding? Or something else? I ask you, please, explain your explanation in this situation and about the error as a whole in a simple way.

public static void main(String[] args) {
    Scanner input = new Scanner (System.in);
    System.out.println("Enter grades size :");

    int Size = input.nextInt();

    String[] y = new String[Size];
    int[] x = new int[Size];

    int Max = 0;
    int Min = x[0];

    String Max_studen = y[0];
    String Min_studen = y[0];

    for (int i = 0 ; i < Size ; i++) {
        System.out.println("Enter student name #" + (i));
        y[i] = input.next();

        System.out.println("Enter student grade : #" + (i));
        x[i] = input.nextInt();

        if (x[i] > Max) {
            Max = x[i];
            Max_studen = y[i];
        } else if (x[i] < Min) {
            Min = x[i];
            Min_studen = y[i];
        }
    }

    System.out.println("Max Student is: " + Max_studen);
    System.out.println("Max grade is: ");
    System.out.println(Max);
    System.out.println("Min Student is: " + Min_studen);
    System.out.println("Min grade is : ");
    System.out.println(Min);
}

Output:

Enter grades size :
2
Enter student name #0
Sam
Enter student grade : #0
85
Enter student name #1
Samy
Enter student grade : #1
95
Max Student is: Samy
Max grade is:
95
Min Student is: null
Min grade is :
0 
+6
source share
4 answers

For a beginner it is difficult. Let's go through this code together.

The first important lines are as follows:

String[] y = new String[Size];
int[] x = new int[Size];

. x - , y - . Java ( 0), x , 0. , . , Java , , , Car. null. null , " " 1. , y null.

:

int Min = x[0];
[...]
String Min_studen = y[0];

, x 0. , Min 0. , < 0 , , Min . , - , Integer.MIN_VALUE ( ) Integer.MAX_VALUE ( ) . long, float double. , . Min , Min_studen . y null , Min_studen.


:

  • camelCase.
  • [] : String[] String [].
  • { .
  • . studentGrades studentNames x y.

, , 2. , , , Java .


1 null , , heap- , .

2 , "". , , . , . , , .

+8

null , . :

String[] y = new String[Size];
int[] x = new int[Size];
...
int Min = x [0];
...
String Min_studen = y[0];

x[0] y[0] / , . Min 0, Min_studen - null.

, Min Min_studen:

else if (x[i] < Min) { Min = x[i];
    Min_studen = y [i];
}

x[] 0, if , Min_studen . ​​

, , , Min , .

+2

null , ​​ , null. : http://www.programcreek.com/2013/12/what-exactly-is-null-in-java/

, :

if (x[i] > Max) {
    Max = x[i] ;
    Max_studen = y[i];
} else if (x[i] < Min) {
    Min = x[i];
    Min_studen = y[i];
}
//x[i] == Max???

You have no occasion for x[i] == Max. Add this case (perhaps changing your current case to x[i] >= Max, and this should fix the null issue.

0
source

The only problem is x [0], and y [0] are not initialized. Therefore, the strings y [0] are stored as NULL. x [0] is stored as 0.

You need to insert additional conditions for initializing x [0] and y [0] with the correct parameters.

if (i == 0) {Max = x [0]; Min = x [0]; }

Check the code -

import java.util.Scanner;
public class HelloWorld {
public static void main(String[] args) {

    Scanner input = new Scanner (System.in);
    System.out.println("Enter grades size : ");

    int Size = input.nextInt();
    String[] y = new String[Size];
    int[] x = new int[Size];

    // Initialize string to store student names and grades
    // Student[0] for highest grade
    // Student[1] for lowest grade
    String[] Student = new String[2];
    int Max = 0, Min = 0;

    for (int i = 0 ; i < Size ; i++) {
        System.out.println("Enter student (" + (i) + ") name : ");
        y[i] = input.next();
        System.out.println("Enter student (" + (i) + ") grade : ");
        x[i] = input.nextInt();

        // This extra condition was missing in the above code.
        if (i==0) {
            Max = x[0];
            Min = x[0];
        }
        else {
            if (x[i] > Max) {
                Max = x[i] ;
                Student[0] = y[i];
                }

            else if (x[i] < Min) {
                Min = x[i];
                Student[1] = y[i];
                }
            }
        }

    System.out.println("Student with highest grade is: " + Student[0]);
    System.out.println("Max grade is: " + Max);
    System.out.println("Student with lowest grade is: " + Student[1]);
    System.out.println("Min grade is : " + Min);

   }
}

The output of the above program here is

Enter grades size : 
3
Enter student (0) name : 
David
Enter student (0) grade : 
34
Enter student (1) name : 
Sam
Enter student (1) grade : 
42
Enter student (2) name : 
Nancy
Enter student (2) grade : 
25
Student with highest grade is: Sam
Max grade is: 42
Student with lowest grade is: Nancy
Min grade is : 25

Hope this helps.

0
source

Source: https://habr.com/ru/post/1598996/


All Articles