Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 14 of 14

Thread: There's an arrayoutofbounds, but my partner coded this and he doesn't know whats wrong.

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default There's an arrayoutofbounds, but my partner coded this and he doesn't know whats wrong.



  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: There's an arrayoutofbounds, but my partner coded this and he doesn't know whats wrong.

    You forgot to post the full text of the error message and the code where the error happens.

    An arrayoutofbounds error happens when the index to an array goes past the end of the array.
    Remember array indexes range in value from 0 to the array length-1
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2013
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: There's an arrayoutofbounds, but my partner coded this and he doesn't know whats wrong.

    Quote Originally Posted by Norm View Post
    You forgot to post the full text of the error message and the code where the error happens.

    An arrayoutofbounds error happens when the index to an array goes past the end of the array.
    Remember array indexes range in value from 0 to the array length-1
    here's the error:

    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
    at cs213.photoAlbum.simpleview.CmdView.main(CmdView.j ava:22)

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: There's an arrayoutofbounds, but my partner coded this and he doesn't know whats wrong.

    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
    at cs213.photoAlbum.simpleview.CmdView.main(CmdView.j ava:22)
    If there is no first element at index 0, the array must have a size of 0. The code needs to test the array's length before trying to index into the array with an invalid index.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Mar 2013
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: There's an arrayoutofbounds, but my partner coded this and he doesn't know whats wrong.

    Quote Originally Posted by Norm View Post
    If there is no first element at index 0, the array must have a size of 0. The code needs to test the array's length before trying to index into the array with an invalid index.
    right, the thing is I'm supposed to run it in command line mode and the arguments would be inputted before running the main class....
    like this:

    To add a new user:
    java cs213.photoAlbum.simpleview.CmdView adduser <user id> "<user name>"
    Output:
    created user <user id> with name <user name>

    I don't know how to change the code to wait for the user inputs then test it from there

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: There's an arrayoutofbounds, but my partner coded this and he doesn't know whats wrong.

    to wait for the user inputs
    What you posted looks like for the user giving the program input on the command line where the args on the command line are passed to the main() method.

    If you want to prompt the user for input and read the response use the System.out.println() method for the prompt and use one of the Scanner class's methods to read the reponse.
    Or use a JOptionPane method to show a message and get a response.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Mar 2013
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: There's an arrayoutofbounds, but my partner coded this and he doesn't know whats wrong.

    I added this to my code, and it doesn't work.
                    int count = 0;
            	cs213.photoAlbum.model.backend.readPerUser();
     
                    //s.useDelimiter("\n");
     
            	System.out.println("Enter instruction");
            	Scanner input = new Scanner(System.in);
     
            	while(input.hasNext())
                {
                	count++;
                }
     
            	String a[] = new String[count];
     
     
                    if(a[0].equals("listusers")==true){
                            cs213.photoAlbum.model.backend.listUsers();
                    }

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: There's an arrayoutofbounds, but my partner coded this and he doesn't know whats wrong.

    it doesn't work.
    Can you explain what "doesn't work" means?
    What does the code do when you execute it?
    What do you want it to do differently?

    What is the posted code supposed to do?
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Mar 2013
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: There's an arrayoutofbounds, but my partner coded this and he doesn't know whats wrong.

    album.txt backend.txt controll.txt photo.txt user.txt CmdView.txt


    these are the files. I'm supposed to implement a code that is allows a user to sign in and edit their albums, and photos. Also we're supposed to edit the captions, and tags. What I added to the CmdView in the before reply "doesn't work" because it doesn't allow me to add a user or list the photos. I just need to get the code to work and allow me to run the code my partner has written

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: There's an arrayoutofbounds, but my partner coded this and he doesn't know whats wrong.

    it doesn't allow me to add a user or list the photos.
    What happens when you execute the code?
    It looks like it prints out a message and waits for the user to enter something.

    Is the program supposed to read something from the user?

    What do want done with the data that the user enters?
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Mar 2013
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: There's an arrayoutofbounds, but my partner coded this and he doesn't know whats wrong.

    I added the print statement and scanner to try to make the code work..but failed. The program is supposed to wait for the user to enter like:
    To add a new user:
    java cs213.photoAlbum.simpleview.CmdView adduser <user id> "<user name>"
    Output:
    created user <user id> with name <user name>
    Or:
    user <user id> already exists with name <user name>

    and other functions too.

    Say I wanted to add user, the user inputs the arguments of the user ID and user name. Then I take those arguments and save it and shows the existing photos under that name and albums.

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: There's an arrayoutofbounds, but my partner coded this and he doesn't know whats wrong.

    Have you read the API doc for the Scanner class to see what methods to use for reading Strings from a user's input? The method you are using only tests if input is available.
    Do you want to read a String from the user? If not, what do you want to read?
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Junior Member
    Join Date
    Mar 2013
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: There's an arrayoutofbounds, but my partner coded this and he doesn't know whats wrong.

    Scratch the scanner stuff I added before, and I have read the scanner API doc but I want to read in Strings from the user. I want to read in String array arguments, but in Java the arguments are inputted after the main class has started running.

    The User is supposed to type in the terminal the path and the specific function they want to do such as add user and the arguments are <userID> <"username">

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: There's an arrayoutofbounds, but my partner coded this and he doesn't know whats wrong.

    arguments are inputted after the main class has started running.
    No they are put on the command line with the java command and the class name as you've shown in your posts. If there aren't any args passed to the main() method, then the program will have to ask the user for them and read them as discussed earlier.

    If the args MUST come from the command line, the program can test if they are there and if not, print out an error message to the user and exit.

    I'm done for tonight.
    If you don't understand my answer, don't ignore it, ask a question.

  15. The Following User Says Thank You to Norm For This Useful Post:

    cryslam (March 22nd, 2013)

Similar Threads

  1. WHATS WRONG
    By subodhkr in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 8th, 2012, 07:47 AM
  2. Whats wrong here?
    By Java Sucks in forum What's Wrong With My Code?
    Replies: 4
    Last Post: June 8th, 2011, 08:33 PM
  3. whats wrong?
    By whattheeff in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 25th, 2011, 02:35 PM
  4. whats wrong with this one....
    By chronoz13 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: October 6th, 2009, 10:08 AM