Helpful Information
 
 
Category: Java
Bad Print out method...

Ok once again with the same program. I was trying to run it jsut to see if it printed out correctly. but it gave me some wierd error. ill post all files that are needed and the error im getting.

import java.util.Scanner;
class Project8Driver
{
static int age, id;
static double gpa;
static String schoolName, name;
static Scanner scan = new Scanner(System.in);
static int userChoice = 1;
static int counter = 1;
static Student[] ListOfStudents = new Student[20];
final static int LIMIT = 14;
static int[] activity = new int[LIMIT];
static int[] exercise = new int[LIMIT];
static int[] project = new int[LIMIT];

public static void main(String[]args)
{
// gather general info
/* System.out.print("Please enter your name: ");
name = scan.next();
System.out.print("Please enter your age: ");
age = scan.nextInt();
System.out.print("Please enter your school's name: ");
schoolName = scan.next();
System.out.print("Please enter your ID: ");
id = scan.nextInt();
System.out.print("Please enter your current GPA: ");
gpa = scan.nextDouble();*/

//int userChoice = 1;
while (userChoice != 0)
{
menu();
userChoice = scan.nextInt();
menuSwitch(userChoice);
}

}

public static void menuSwitch(int choice)
{
int num;
switch(choice)
{
case 4:

System.out.print("Please enter your name: ");
name = scan.next();
System.out.print("Please enter your age: ");
age = scan.nextInt();
System.out.print("Please enter your school's name: ");
schoolName = scan.next();
System.out.print("Please enter your ID: ");
id = scan.nextInt();
System.out.print("Please enter your current GPA: ");
gpa = scan.nextDouble();
System.out.println("Please enter your expected graduation year");
int graduationYear = scan.nextInt();

ListOfStudents[counter] = new MiddleSchool(name, age, schoolName, id, gpa, graduationYear);
counter++;


break;
case 3:
/*int gradYear, numOfCreditsRemaining, satScore, apCredits;
System.out.print("Please enter your expected graduation year: ");
gradYear = scan.nextInt();
System.out.print("Please enter the number of credits remaining for you to graduate: ");
numOfCreditsRemaining = scan.nextInt();
System.out.print("Please enter your SAT score: ");
satScore = scan.nextInt();
System.out.print("Please enter your number of AP credits: ");
apCredits = scan.nextInt(); */

System.out.print("Please enter your name: ");
name = scan.next();
System.out.print("Please enter your age: ");
age = scan.nextInt();
System.out.print("Please enter your school's name: ");
schoolName = scan.next();
System.out.print("Please enter your ID: ");
id = scan.nextInt();
System.out.print("Please enter your current GPA: ");
gpa = scan.nextDouble();
System.out.print("What year do you plan to graduate? ");
int gradYear = scan.nextInt();
System.out.print("How many credits do you need to aquire before you can graduate? ");
int credLeft = scan.nextInt();
System.out.print("What did you score on the SAT? ");
int SAT = scan.nextInt();
System.out.print("How many AP credits do you have? ");
int apCred = scan.nextInt();

ListOfStudents[counter] = new HighSchool(name, age, schoolName, id, gpa, gradYear, credLeft, SAT, apCred);
counter++;



case 1:


System.out.print("Please enter your name: ");
name = scan.next();
System.out.print("Please enter your age: ");
age = scan.nextInt();
System.out.print("Please enter your school's name: ");
schoolName = scan.next();
System.out.print("Please enter your ID: ");
id = scan.nextInt();
System.out.print("Please enter your current GPA: ");
gpa = scan.nextDouble();
System.out.print("What is your current major? ");
String major = scan.next();
System.out.print("What is your current minor? ");
String minor = scan.next();
boolean time;
System.out.print("Are you a full time or part time student? (1 - full time, 2 - part time) ");
int pTime = scan.nextInt();
if (pTime == 1)
time = true;
else
time = false;
System.out.print("Please enter your exam 1 grade: ");
int examOne = scan.nextInt();
System.out.print("Please enter your exam 2 grade: ");
int examTwo = scan.nextInt();
// create for loop to gather grades and put them in an array
for (int i = 1; i <= 14; i++)
{
System.out.print("Please enter Activity grade for Activity " + i +" : ");
activity[i-1] = scan.nextInt();
}
for (int j = 1; j <= 14; j++)
{
System.out.print("Please enter Exercise grade for Exercise "+j+" : ");
exercise[j-1] = scan.nextInt();
}
for (int k = 1; k <= 14; k++)
{
System.out.print("Please enter project grade for Project " + k +" : ");
project[k-1] = scan.nextInt();
}

break;

case 5:
System.out.println(ListOfStudents);
default:
System.out.println("Invalid choice");
}
}

public static void menu()
{

System.out.println("*************");

System.out.println("1: College - Undergraduate");
System.out.println("2: College - Graduate");
System.out.println("3: High School");
System.out.println("4: Middle School");
System.out.println("5: Print");
System.out.print("\nEnter your choice: ");

}

}


class Student
{
String sName, nameSchool;
int age, id;
double gpa;

Student(String sName, int age, int id, String nameSchool, double gpa)
{
this.sName = sName;
this.age = age;
this.id = id;
this.nameSchool = nameSchool;
this.gpa = gpa;
}


public String toString()
{//String s1 = "";

return"Name: "+sName+"\tAge: "+age+"\tID: "+id+"\tSchool's name: "+nameSchool+"\tGPA: "+gpa;
//for(i;i<end of arry)
//{
//s1 += array[i] + " "

}
}



class MiddleSchool extends Student
{
int gradYear1;

public MiddleSchool(String name, int age, String schoolName, int id, double gpa, int gradYear)
{
super(name, age, id, schoolName, gpa);
gradYear1 = gradYear;
}

public String toString()
{
String temp = super.toString();
return temp + gradYear1;
}
}

this is the output i get when i try and print it

"[LStudent;@8965fb"

case 5:
System.out.println(ListOfStudents);You're printing an array. That's the sort of output the Java VM gives when converting an object without a toString() method to a string. The [ means it's an array, the L means it contains objects, Student is the type of object, and 8965fb is the memory address (relative to the VM, I think).

Any translation for that? I've printed an array of object like that before, but it was probably different.


EDIT

Im supposed to print the results in an array. am i even going about it the right way so that when every student is added and you hit print its prints the array?

I doubt you did -- it may have been a subclass of Array with a toString() method.
In this case, you probably want to loop through the array and print out each Student:
case 5:
for(int i = 0; i < ListOfStudents.length; ++i)
System.out.println(ListOfStudents[i]);

Ok thats what i was kinda looking for but i tried some short cut method that my book shows but its more for jsut listing numbers rather thank objects.

Ok 1 more thing. How can i make it not print out null for the other 19 slots, other than filling them all up

case 5:
for(int i = 0; i < ListOfStudents.length; ++i)
if(ListOfStudents[i] != null)
System.out.println(ListOfStudents[i]);Or, if you're certain that no more elements will be encountered after the first null, incorporate it into the termination condition for the for loop:
case 5:
for(int i = 0; i < ListOfStudents.length && ListOfStudents[i] != null; ++i)
System.out.println(ListOfStudents[i]);

Ok thanks that works for it. thansk a ton again... ill probably be back before this project is done.










privacy (GDPR)