I have to write a program for two users, a school admin and a student. The admin must first enter a password and enter 15 courses by day Mon-Fri (1-5) and time Morning, Afternoon, Evening (1-3) into an array. Then the student can enter the day and time and view courses available. If the admin has not entered a course for that day, it should display 'Course not available'. This is the code I have:
But I have two problems: when I try to enter a day and time for the student, this error shows up: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 3 out of bounds for length 3 at pkg.Exercise3.main(Exercise3.java:72)
which I know means my array is out of bounds, but I'm not sure why as I entered numbers like 2 for day and 3 for time! Also, I cannot figure out how to display the message that the 'course is not available' if the array has not been filled.
Mona Smith wrote:Index 3 out of bounds for length 3 ... which I know means my array is out of bounds, but I'm not sure why as I entered numbers like 2 for day and 3 for time!
That's correct. Because what you call time is column index, which is the highest 2, as you have: String schedule=new String;
As you see, all that shows how fragile the code is if you rely on user input, whether it will go out of bounds or not.
Problem is, that this code is hard to follow. It is also cluttered with unnecessary comments (you can delete them safely as they don't tell anything extra than actual code does).
Did you learn about the methods? How to decompose long methods to a smaller ones, so they'd do one and only one thing?
posted 1 week ago
Oh ok thank you! I tried subtracting one so the line for the student is [row-1][col-1] and that seems to give me the correct answer. Yes, there are unnecessary comments as my teacher wants us to comment on at least every other line of code to help us better understand it. I didn't do it in the second part, as I was still tinkering around with it. No, I have not learnt about methods yet! The only problem now is that I'm not sure how to display a message if the student asks for an array which has not been entered.
Not that I like being the "formatting and indentation" policeman around here or anything like that but since you're doing so well in that respect, here's a tip for you:
When you notice indentation that skips a level, it's usually a sign that your formatting is a bit off, as is the case here.
Notice how the closing brace of the do-while loop on line 62 appears to be indented 8 spaces in from the closing brace on line 64? You'd expect only one indentation level (4 spaces) there. Because you're code is relatively well-formatted, it's easy to scan your code and see that lines 19-62 are indented one level too deep. If you're using an IDE like Eclipse or IntelliJ IDEA or NetBeans, there's a quick command you can use to autoformat the code for you so that it is back to its proper form. Otherwise, you can just highlight that block of code and press Shift+Tab to unindent the whole block by one level.
Mona Smith wrote:The only problem now is that I'm not sure how to display a message if the student asks for an array which has not been entered.
declares a nested array of Strings. Since String is an object type, all the elements in schedule are initially null until you assign Strings to them. So, if the admin hasn't entered a schedule for that day and hour, then it will be null.
By the way, that is a bad comment. Avoid writing comments that explain implementation details like that. It's clear from the declaration that schedule is an array. That's like putting a Post-It note on your door that says "Door." Well, duh, right?
Here's what I suggest: go to your instructor and raise your concerns about meaningless comments like this. Clarify whether or not he/she really wants you to put comments like that in your code or whether he/she would prefer that you learn to write more meaningful and useful comments. If it were me, I would bring it up in class so all my classmates can hear the answer as well. (But that's just me and sometimes I can be quite cheeky )
Mona Smith wrote:Yes, there are unnecessary comments as my teacher wants us to comment on at least every other line of code to help us better understand it.
I see. Perhaps it isn't that bad idea to ask students at the very beginning to comment their code, however, I would want to believe, that it is not exactly what teacher is asking/expecting.
Comments like those below, really don't help you better understand what is going on.
Perhaps would be useful if during the lectures would be spent a bit more time highlighting the use cases for the comments.
Ok, once again, I see the use of the comments at the very beginning, but not in the form you have written them, and once again, I would want to believe, that it is not what the teacher is asking.
So what actually would make sense, is to ask beginner students to write comments like that. Read and compare with your version:
Now, if you wrote comments like that, I, as a teacher would say: "Great! Student really understood and explained clearly why the scanner object is for, and demonstrated the knowledge about an array of arrays. +5% for elaborate comments. Expecting student to perform well during an exam".
Do you see the difference? Now such comments really give some proof that you really understood the concept behind those.
Let's get back to this comment:
Do you remember for what reason you created this thread? For the index going out of bounds, and do you see now, that really the comment you placed initially didn't prove that you understood an array of arrays structure, it proved differently. But I hope you understand now
Villains always have antidotes. They're funny that way. Here's an antidote disquised as a tiny ad: