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?
 
NorieVBA ExpertCommented:
Shouldn't this,
String[] stuNames = new String[25];

Open in new window

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

Open in new window

0
 
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
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

 
Shaun VermaakTechnical Specialist/DeveloperCommented:
Shouldn't this then be marked as High Priority?
1
 
krakatoaCommented:
Maybe he is not working in real time?
1
 
krakatoaCommented:
Closed.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.