Taking integer information from one array and converting it to string information

Hey everyone. I've got about an hour left to finish this project so If you can help me I'd appreciate it. I need a solution to this. The problem starts at the area marked with the <<<<<<<<<<<<< on the code below close to the bottom. I'm trying to make an array that allows me to enter names. The amount of names I enter should be the same amount of students there are. Basically, the students, the grades, and the names all have to be the same amount. When ever I run this program, I get to the enter name portion and I have to enter 25 names. How do I make it so that I only have to enter the amount of names that correlates with the amount of students and grades.

The error I get is                     "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2
                                                                         at Lab5Start.main(Lab5Start.java:78)"






public static void main(String[] args) {
            Scanner input = new Scanner(System.in);
            //enter student amount                                                                Enter student amount
            System.out.println("Enter number of students: ");                          
            
            //check if only numbers were entered (done)
            while (!input.hasNextInt()) {
                  System.out.println("Invalid number. Please enter only digits.");
                  System.exit(0);
            }
            //student = number of students
            int student = input.nextInt();
                  
            //check if more than 25 students
            if (student > 25) {
                  System.out.println("Cannot have more than 25 students, try again.");
                  System.exit(0);      
            }
            
            
            System.out.println("Enter grades for the entered number of students: ");
            //grade = the multiple grades                                 Enter Grades
            int[] grade = new int[student];
            
            int i = 0;
            
            while( i < grade.length) {
                  grade[i] = input.nextInt();
                  i++;
                  
            }
            
            
                  
            //enter student names                                                                 //not working goes to 25<<<<<<<<<<
            System.out.println("Enter the student names");
            String[] stuNames = new String[25];
            
            for(int z = 0;  z < stuNames.length; z++) {
                  stuNames[z] = input.nextLine();
                  if(z > stuNames.length) {
                        System.out.println("Too many students");
                  }      
                  String name = stuNames[z];
                  if (name == null || name.isEmpty()) {
                        continue;
                  }
      
            }
Grant RichardsStudentAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

NorieAnalyst Assistant Commented:
Shouldn't this,
String[] stuNames = new String[25];

Open in new window

be this?
String[] stuNames = new String[student];

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
krakatoaCommented:
It would (have) be(en) a lot easier to read and obtained more response if you had put your code into the code tags.
1
krakatoaCommented:
See what your tutor says about this :

public static void main(String[] args) {
            Scanner input = new Scanner(System.in);
            //enter student amount                                                                Enter student amount
            System.out.println("Enter number of students: ");                          
            
            //check if only numbers were entered (done)
            while (!input.hasNextInt()) {
                  System.out.println("Invalid number. Please enter only digits.");
                  System.exit(0);
            }
            //student = number of students
            int student = input.nextInt();
                  
            //check if more than 25 students
            if (student > 25) {
                  System.out.println("Cannot have more than 25 students, try again.");
                  System.exit(0);      
            }
            
            
            System.out.println("Enter grades for the entered number of students: ");
            //grade = the multiple grades                                 Enter Grades
            int[] grade = new int[student];
            
            int i = 0;
            
            while( i < grade.length) {
                  grade[i] = input.nextInt();
                  i++;
                  
            }
            
                  
            //enter student names                                                                 //not working goes to 25<<<<<<<<<<
            System.out.println("Enter the student names");
            String[] stuNames = new String[student];
            Integer iy =null;
            for(int z = 0;  z < stuNames.length; z++) {
                    
                  stuNames[z] = input.next();
                  
                  try{
                   iy = Integer.parseInt(stuNames[z]);
                  }
                  catch(Exception e){iy=null;continue;}
                  if((iy instanceof Integer)){iy=null;z--;}
                  
            }
            
            }

Open in new window

0
Angular Fundamentals

Learn the fundamentals of Angular 2, a JavaScript framework for developing dynamic single page applications.

Shaun VermaakTechnical SpecialistCommented:
Shouldn't this then be marked as High Priority?
1
krakatoaCommented:
Maybe he is not working in real time?
1
krakatoaCommented:
Closed.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
arrays

From novice to tech pro — start learning today.