not looking for a hand out just a hand up
i have a project for school and i cant get it to run all the way through. can any one tell me why? also im getting a error that moves around every time i run it.
Code:
package lockers;
/* Program Name: Lockers
* Author: Shane Cloutier
* Date:
* Description:
*/
public class Lockers {
public static void main(String[] args) {
// Close is a closed locker
// Open is an open locker
char [] Lockers = new char[101];
int Counter = 0;
int Student = 1;
int LockerNumber = 0;
while (Counter != 101){
Lockers[Counter] = 'C';
Counter++;
}
Counter = 0;
while (Student != 100){
while (LockerNumber != 100){
int Locker = LockerNumber + Student;
//System.out.println("locker"+Locker);
if (Lockers[Locker] == 'C'){
Lockers[Locker] = 'O';
LockerNumber += Student;
}
else if (Lockers[LockerNumber + Student] == 'O'){
Lockers[LockerNumber + Student] = 'C';
LockerNumber += Student;
}
//System.out.println("Student"+Student+ "locker"+LockerNumber);
}
LockerNumber = 0;
Student += 1;
System.out.println("student"+Student);
}
while (Counter !=100){
LockerNumber = 0;
if (Lockers[LockerNumber] == 'O'){
System.out.println("Locker "+LockerNumber+" is open");
}
LockerNumber++;
}
}
}
Re: not looking for a hand out just a hand up
The error say:
run:
student2
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 102
student3
at Lockers.main(Lockers.java:33)
Java Result: 1
It means that the "array" Lockers containing only 100 cells and you are trying to enter a cell number 102.
The problem is that add two counters in this line "int locker = lockerNumber + student;"
Re: not looking for a hand out just a hand up
now i can get it to run through all the students but my array has to be way bigger than i want it to be. i would like my array to have 100 places.
also it will not go in to the last while loop to give me the final out put of what lockers are open.
Code:
package lockers;
/* Program Name: Lockers
* Author: Shane Cloutier
* Date:
* Description:
*/
public class Lockers {
public static void main(String[] args) {
// Close is a closed locker
// Open is an open locker
char [] Lockers = new char[200];
int Counter = 0;
int Student = 1;
int LockerNumber = 0;
while (Counter != 200){
Lockers[Counter] = 'C';
Counter++;
}
Counter = 0;
while (Student != 100){
if (LockerNumber < 100){
int Locker = LockerNumber + Student;
System.out.println("locker"+Locker);
if (Lockers[Locker] == 'C'){
Lockers[Locker] = 'O';
LockerNumber += Student;
}
else if (Lockers[Locker] == 'O'){
Lockers[Locker] = 'C';
LockerNumber += Student;
}
//System.out.println("Student"+Student+ "locker"+LockerNumber);
}
else{
LockerNumber = 0;
Student += 1;
}
}
int i = 0;
while (i != 100){
System.out.println("test"+Lockers[i]);
i++;
}
Counter = 0;
while (Counter !=100){
LockerNumber = 0;
if (Lockers[LockerNumber] == 'O'){
System.out.println("Locker "+LockerNumber+" is open");
}
LockerNumber++;
}
}
}
Re: not looking for a hand out just a hand up
In future posts, please use code tags. First type [code] Then paste your code and type [/code]