Results 1 to 6 of 6
Like Tree2Likes
  • 2 Post By blackdiz

Thread: Help with these two codes

  1. #1
    blackdiz is offline Member
    Join Date
    Apr 2012
    Posts
    16
    Rep Power
    0

    Default Help with these two codes

    I want to write a code to judge if an int[3] array is full then throws a stackfullexception to tell user the stack is full, if the user wants to put new data in the array.
    When I try two different ways to write,one is :
    Java Code:
    public void push(int i) throws StackFullException {	
    		top++;
    		if(top >= 3)
    			throw new StackFullException("The stack is full.");
    		stack[top] = i;
    		System.out.println(stack[top]);		
    	}//close push()
    this works ok, when I call push(i) four times in the main() the output is:
    3
    5
    6
    The stack is full.

    but if I change the code to:
    Java Code:
    public void push(int i) throws StackFullException {	
    		if(top++ >= 3)
    			throw new StackFullException("The stack is full.");
    		stack[top] = i;
    		System.out.println(stack[top]);		
    	}//close push()
    the output is:
    3
    5
    6
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
    at com.black.exception.Stack.push(Stack.java:10)
    at com.black.exception.StackMain.main(StackMain.java: 10)

    Why I just put top++ in the if, the runtime exception happens?I still can't figure out why.
    Please if you can help to explain.
    Hope you can understand my poor English, Thank you, best regards.

  2. #2
    blackdiz is offline Member
    Join Date
    Apr 2012
    Posts
    16
    Rep Power
    0

    Default Re: Help with these two codes

    I just figure out why.
    Because if(top++ >= 3) means it will decide if top >= 3 or not ,then top + 1.Because top is still 2, so it won't throw an exception.
    But after that , top will plus one , and run the stack[top] = i;
    And it will run stack[3] = 1 , that's why ArrayIndexOutOfBoundException happen.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: Help with these two codes

    In Java, array indexes are zero based. A 3 element array has elements at [0] [1] and [2].

    db

    edit Glad you found that out for yourself.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    blackdiz is offline Member
    Join Date
    Apr 2012
    Posts
    16
    Rep Power
    0

    Default Re: Help with these two codes

    Quote Originally Posted by DarrylBurke View Post
    In Java, array indexes are zero based. A 3 element array has elements at [0] [1] and [2].

    db

    edit Glad you found that out for yourself.
    Thank you for reminding me that.
    Sometimes I am still confused with some basic syntaxes like differents between x++ and ++x etc.
    Hope one day I can write codes as breathing air naturally.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: Help with these two codes

    Oh, and for your future reference: Forum Rules -- particularly the third paragraph

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #6
    blackdiz is offline Member
    Join Date
    Apr 2012
    Posts
    16
    Rep Power
    0

    Default Re: Help with these two codes

    Quote Originally Posted by DarrylBurke View Post
    Oh, and for your future reference: Forum Rules -- particularly the third paragraph

    db
    OH.....I am Really Sorry about that......didn't notice that before.......Apologies
    Thanks again.
    I will be more careful about posting threads in the future.
    Best Regards.
    DarrylBurke and Fubarable like this.

Similar Threads

  1. Need help with my codes
    By vesperia in forum NetBeans
    Replies: 3
    Last Post: 06-29-2011, 12:23 PM
  2. I need some codes
    By johnmergene in forum New To Java
    Replies: 11
    Last Post: 01-25-2011, 03:42 AM
  3. What do the following codes do?
    By javaguy2 in forum New To Java
    Replies: 2
    Last Post: 01-23-2011, 10:23 PM
  4. two short codes
    By Libertyman in forum New To Java
    Replies: 7
    Last Post: 06-21-2010, 03:22 PM
  5. What's wrong with my codes?
    By ayoood in forum New To Java
    Replies: 16
    Last Post: 09-01-2008, 03:57 AM

Posting Permissions

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