I have these three classes:

Student.java

public class Student
{
    private int grade;
    private String name;

    public Student(String n, int g)
    {
        grade = g;
        name = n;
    }

    public int getGrade()
    {
        return grade;
    }

    public String getName()
    {
        return name;
    }
}

GradeBook.java

public class GradeBook
{
    private int size;
    private Student[] students;

    public GradeBook(int s)
    {
        size = s;
        students = new Student[size];
    }

    public void setStudent(int i, String name, int grade)
    {
        students[i] = new Student(name, grade);
    }

    public int getSize()
    {
        return size;
    }

    public Student getStudent(int i)
    {
        return students[i];
    }

    public void printGradeBook()
    {
        for (int i = 0; i < size; i++)
        {
            System.out.println(students[i].getName() + "\t" 
                + students[i].getGrade());
        }
    }

    public int getAverageGrade()
    {
        int sum = 0;
        for (int i = 0; i < size; i++)
        {
            sum += students[i].getGrade();
        }
        return sum / size;
    }

    public void sort()
    {
        int n = size;
        for (int i = 0; i < n - 1; i++)
        {
            Student smallest = students[i];
            int smallestPosition = i;
            for (int j = i + 1; j < n; j++)
            {
                if (students[j].getName().compareTo(
                    smallest.getName()) < 0)
                {
                    smallest = students[j];
                    smallestPosition = j;
                }
            }
            students[smallestPosition] = students[i];
            students[i] = smallest;
        }
    }

    public int getGradeSequential(String name)
    {
        for (int i = 0; i < size; i++)
        {
            if (students[i].getName().equals(name))
                return students[i].getGrade();	
        }
        return -1;	
    }

    public int getGrade(String item)
    {
        int low = 0;
        int high = size - 1;
        while (low <= high)
        {
            int middle = (low + high) / 2;
            if (students[middle].getName().equals(item))
                return students[middle].getGrade();
            if (students[middle].getName().compareTo(item) < 0)
                low = middle + 1;
            else
                high = middle - 1;
        }
        return -1;
    }
}

GradeBookTester.java

import java.io.*;
import java.util.Scanner;

public class GradeBookTester
{
	public static void main(String[] args) throws IOException
	{
		int numberOfStudents = 10;
	    GradeBook myGradeBook = new GradeBook(numberOfStudents);

		Scanner nameScanner = new Scanner(System.in);
		Scanner gradeScanner = new Scanner(System.in);
		
		for (int i = 0; i < myGradeBook.getSize(); i++)
		{
			String name = nameScanner.nextLine();
			int grade = gradeScanner.nextInt();
			myGradeBook.setStudent(i, name, grade);
		}
		
		myGradeBook.printGradeBook();
		System.out.println("Average grade is " + myGradeBook.getAverageGrade());
		myGradeBook.sort();
		myGradeBook.printGradeBook();
		
		Scanner keyboard = new Scanner(System.in);
		System.out.print("Enter name: ");
		String searchName = keyboard.nextLine();
		
		int myGrade = myGradeBook.getGrade(searchName);
		
		if (myGrade == -1)
			System.out.println("Not found");
		else
			System.out.println("Grade is " + myGrade);
	}
}

Everything compiles okay, but when I run it, the virtual machine just stays there and nothing happens (no pop-up, input box, etc).

Don't create multiple Scanner objects for the same underlying stream. As far as the "VM just stays there" is concerned, `nameScanner.nextLine()' is a blocking call which waits for a line to be entered via the stdin.

This article has been dead for over six months. Start a new discussion instead.