i trying assign current array element in temp array student object returned after calling getstudent method.... called getstudent method (step 2) , have temp[i] = assign current element in temp array cannot figure out should = pair student object returned. when using getstudent() , running program, output enter number of students, user enters number, , happens, not ask user enter name , etc, i'm not sure if step 2 problem or if there issue entirely.
import java.util.scanner; public class students { private static scanner input = new scanner(system.in); public static void main(string[] args) { student[] students; students = getstudents(); printstudents(students); } private static student[] getstudents() { student[] temp; int how_many; system.out.print("how many students? "); how_many = input.nextint(); purgeinputbuffer(); temp = new student[input.nextint()]; // step 1 ??? (int = 0; < temp.length; i++) { getstudent(); // step 2 temp[i] = ; // <---------- } return temp; // step 3 } private static student getstudent() { string name, address, major; double gpa; system.out.print("enter name: "); name = input.nextline(); system.out.print("enter address: "); address = input.nextline(); system.out.print("enter major: "); major = input.nextline(); system.out.print("enter gpa: "); gpa = input.nextdouble(); purgeinputbuffer(); return new student (name, address, major, gpa); // step 4 } private static void printstudents(student[] s) { system.out.println(); (int = 0; < s.length; i++) // step 5 { system.out.println(getstudent()); // step 6 } } private static void purgeinputbuffer() { // ---------------------------------------------------- // purge input buffer reading , ignoring remaining // characters in input buffer including newline // ---------------------------------------------------- input.nextline(); } }
so first problem first on line:
temp = new student[input.nextint()];
in line have asked user enter how many students , store in how_many
. i'm assuming want instead do:
temp = new student[how_many];
also said in comment:
but please @ private static void printstudents(student[] s)
method , acutally on line //step 6 don't believe how want doing that. instead want system.out.println(s[i]);
not system.out.println(getstudent());
code substitution work though need override tostring method can display information
Comments
Post a Comment