What is wrong?


#1

why the maxStudent and minStudent don’t print out?

import java.util.Scanner;

public class ReArrays1 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		Scanner input = new Scanner(System.in);
		System.out.print("How much students?");
		
		int size = input.nextInt();
		
		String []names= new String [size];
		int []degree = new int[size];
		
		for(int x=0; x<size; x++) {
			System.out.print("student name");
			names[x]= input.nextLine();
			input.nextLine();
			System.out.print("student degree:");
			degree[x]=input.nextInt();
			
		}
		
		int maxDegree= degree[0];
		String maxStudent = names[0];
		int minDegree= degree[0];
		String minStudent= names[0];
		
		for(int x=0; x<size; x++) {
			if(degree[x] > maxDegree) {
				maxDegree= degree[x];
				maxStudent = names[x];
				
			}else if(degree[x] < minDegree) {
				minDegree = degree[x];
			}
			
		}
		
		System.out.println(maxDegree);
		System.out.println(minDegree);
		System.out.println(maxStudent);
		System.out.println(minStudent);
	
	}
}

#2

It is printed out. They’re (probably, but not necessarily, depending on input) empty strings.

You’re mixing reads of tokens and lines, I suggest sticking with one or the other, to either read only tokens one after the other, or to read the whole lines and split them up and convert yourself.

Or if you do mix, think really carefully about how you left the input stream (the “think carefully” part is what I’d avoid, that’s just saying it’s error-prone)

A good idea when posting code that requires input is to also include the input that you are using, like this:

2
bob
10
otherbob
9

Because, in a sense, that is part of your program and your program isn’t complete without it.


#3

Could you explain more, please? because I don’t understand.

And that is the input:

Enter How much students?
3
Enter student name: sa
Enter student name: ah
Enter student name: mo
Enter student degree: 80
Enter student degree: 60
Enter student degree: 40

80

40


#4

why is output of maxStudent and minStudent is blank?