Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do i rerun part of a code that uses an array?

I’m trying to make a code that takes a users input and prints their schedule, but I’m running into a problem with my do-while loop.

My program will not rerun. I’m getting an error that says:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 2 out of bounds for length 2 at com.company.Main.main(Main.java:25)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Here is my code:

import java.util.*;

public class Main {

    public static void main(String[] args)  {
        Scanner input = new Scanner(System.in);
        String rerun;
        do {
            System.out.println("What is your name?");
            String name = input.nextLine();
            System.out.println("How many courses do you have?");
            int numCourse = input.nextInt();
            input.nextLine();
            String[][] timetable = new String[numCourse][2];
            for (int j = 0; j < numCourse; j++) {
                System.out.println("What is the name of your course #" + (j + 1) + "?");
                String course = input.nextLine();
                timetable[j][0] = course;
                System.out.println("What is your teachers name for " + course + "?");
                String teacher = input.nextLine();
                timetable[j][1] = teacher;
            }
            System.out.println("Hello " + name + ", here is your timetable:");
            for (int i = 0; i <= numCourse; i++) {
                System.out.format("\n%-30s%-30s", "Course #" + (i+1) + ": " + timetable[i][0],"Teacher: " + timetable[i][1]);
            }
            System.out.println("Would anyone else like to print their schedule? (yes/no)");
            rerun = input.next();
        }while(rerun.equalsIgnoreCase("yes"));
        System.out.println("Goodbye!");
    }
}

>Solution :

Problem is in second for loop where you display your from array. put next() inplace of nextLine() because sometimes it skip the position.

Change

for (int i = 0; i <= numCourse; i++) {
      System.out.format("\n%-30s%-30s", "Course #" + (i+1) + ": " + timetable[i][0],"Teacher: " + timetable[i][1]);
}

To

for (int i = 0; i < numCourse; i++) {
      System.out.format("\n%-30s%-30s", "Course #" + (i+1) + ": " + timetable[i][0],"Teacher: " + timetable[i][1]);
}

Suppose your numCorse is 2. In your code, loop start from 0 and terminate after 2 so, Your loop working right while i is 0 and 1 but if i is going to 3 you get exception ArrayIndexOutOfBound.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading