Results 1 to 7 of 7
  1. #1
    teja is offline Member
    Join Date
    May 2013
    Posts
    3
    Rep Power
    0

    Unhappy Exception Handling

    This is my code to implement stack using arrays!!But i am getting an exception array out of bounds for push() method...please help me wid dis!!
    import java.io.*;
    import java.util.*;
    public class Stack {
    int item,top=-1;
    int a[]=new int[item];
    public void push()
    {


    top++;
    System.out.println("enter the item");
    Scanner sc1=new Scanner(System.in);
    item=sc1.nextInt();
    a[top]=item;
    System.out.println("push operation performed");
    }
    public int pop()
    {

    item=a[top];
    top--;
    return item;
    }
    public void display()
    {

    for(int i=top;i>=0;i--)
    {
    System.out.println(i);
    }
    }

    public static void main(String args[])throws IOException
    {


    int ch;
    String che;
    Stack s1=new Stack();
    Stack s2=new Stack();
    Stack s3=new Stack();
    do{
    System.out.println("1.push 2.pop 3.display \n Enter your choice");
    Scanner sc2=new Scanner(System.in);
    ch=sc2.nextInt();
    switch(ch)
    {
    case 1:
    s1.push();
    break;
    case 2:
    s2.pop();
    break;
    case 3:
    s3.display();
    break;
    }
    System.out.println("continue y or n");
    Scanner sc3=new Scanner(System.in);
    che=sc3.next();
    sc3.close();
    }
    while(che=="y"||che=="Y");
    }

    }

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Exception Handling

    Moved to New To Java.
    When posting code please wrap it in [code] tags [/code] in order to retain the formatting.

    When you get an exception, please post the full exception text, including the stack trace, and highlight the line in your code the exception is thrown from.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    256
    Rep Power
    3

    Default Re: Exception Handling

    Looks to me like you set the size of the array to -1

    int a[]=new int[item]

    arrays are non realizable the size you set here is final, if you had the size -1 you can actually add anything to the array, which will return the exception you are getting.

    This is my understanding but un formatted code is hard to follow

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,038
    Rep Power
    6

    Default Re: Exception Handling

    Nope! It was set to size 0. top = -1. Either way, a zero length array can't hold much.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    256
    Rep Power
    3

    Default Re: Exception Handling

    Ah yes!

    Ye still doesnt change much, since array size in final, you should think about how many elements you want your array to hold, if you are unsure use a realizable structure like arrayList.

  6. #6
    jbarke12's Avatar
    jbarke12 is offline Member
    Join Date
    May 2013
    Location
    Charleston, South Carolina USA
    Posts
    88
    Rep Power
    0

    Default Re: Exception Handling

    This looks like a homework assignment for a data structure class. It is important to remember like everyone has said that array aren't resizable. However you can define your own method to make it that way.
    Java Code:
    private growArray(){
    int temp[]= new int[a.length+10];  
    for(int x=0; x<a.length; x++){
     temp[x]=a[x];
    }
    a=temp;
    }
    Last edited by jbarke12; 05-15-2013 at 07:36 PM.

  7. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,038
    Rep Power
    6

    Default Re: Exception Handling

    As was said earlier, an ArrayList would make your implementation easier. Also, you need to check for popping a value when none exists. That will cause an exception. And why are you using three scanners and three stacks? For the scanners, you just need one. For the stacks, depending on what the user asks, you push on one and pop off the other, and display a third. Is this really what you want to do? Or do you want to simply let the user exercise a single stack?

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. exception handling
    By Elaforestis in forum New To Java
    Replies: 1
    Last Post: 01-29-2013, 05:37 AM
  2. Exception Handling
    By Bicentric in forum New To Java
    Replies: 1
    Last Post: 07-22-2012, 10:37 PM
  3. Exception handling
    By Moedig in forum New To Java
    Replies: 2
    Last Post: 11-10-2011, 11:43 PM
  4. Exception Handling
    By liljester in forum New To Java
    Replies: 4
    Last Post: 06-21-2010, 04:09 PM
  5. Exception Handling...
    By focus_nitin in forum New To Java
    Replies: 1
    Last Post: 02-16-2008, 04:13 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
  •