Results 1 to 4 of 4
Like Tree1Likes
  • 1 Post By DIego912

Thread: not looking for a hand out just a hand up

  1. #1
    cloutier172 is offline Member
    Join Date
    Oct 2011
    Posts
    5
    Rep Power
    0

    Default 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.


    Java 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++;
            }
        }
    }
    Last edited by sunde887; 10-04-2011 at 03:03 AM. Reason: Added code tags, [code]...[/code]

  2. #2
    DIego912 is offline Member
    Join Date
    Sep 2011
    Posts
    20
    Rep Power
    0

    Default 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;"
    Last edited by Fubarable; 10-04-2011 at 02:57 AM. Reason: corrected
    Fubarable likes this.

  3. #3
    cloutier172 is offline Member
    Join Date
    Oct 2011
    Posts
    5
    Rep Power
    0

    Default 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.
    Java 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++;
            }
        }
    }
    Last edited by cloutier172; 10-04-2011 at 03:46 AM. Reason: Added code tags, [code]...[/code]

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default 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]

Similar Threads

  1. I need some one to give me a hand
    By HosHos in forum Advanced Java
    Replies: 9
    Last Post: 09-05-2009, 03:18 PM
  2. Can someone give me a helping hand...
    By kwesiaryee in forum Advanced Java
    Replies: 1
    Last Post: 09-17-2008, 07:11 PM
  3. Really Would use a hand
    By mollentze in forum New To Java
    Replies: 7
    Last Post: 07-03-2008, 02:30 PM
  4. Replies: 9
    Last Post: 03-25-2008, 03:46 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •