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 4 of 4

Thread: Help!!

  1. #1
    Junior Member
    Join Date
    Aug 2013
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Help!!

    I need your help.
    for example I input 3 elements

    Enter any number= 1
    Enter any number= 2
    Enter any number= 3
    public int getNum(){
            if(n<=99){
                for(int i=0;i<n;i++){
                     System.out.print("Enter any number: ");
                     ar[i]=br.nextInt();
                }                 
            }
            else
                System.out.print("Out of Bound!!");
            return n;
        }
    The output will be:
    1 2 3

    suppose I want to insert 6 in the position of 2 and the output is
    1 6 2 3
    and if I delete position 3 the output is
    1 6 3

    This is a sample code for Insert and Delete.

    Can someone help me to explain this following code of insert and delete

        public void Insert(){
            int j;
            if(pos<=n)
            {
                for( j=n;j>=pos;j--){
                    ar[j+1]=ar[j];}
                System.out.print("Insert a new Element : ");
                ar[j+1] = br.nextInt();
                n++;
     
            }
        else
            System.out.println("Invalid Position");
        }
     
        public void Delete(){
            int j;
            if(pos<=n)
            {
                for(j=pos;j<n;j++){
                ar[j]=ar[j+1];
                }
               System.out.println();
                System.out.println("Element Deleted");
                n--;
            }
     
        else
            System.out.println("Invalid Position");
        }

  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Help!!

    I would expect an insert method to accept two parameters, like the value to insert and the index number of where to insert it.
    I would expect a delete method to accept one parameter, the index number to delete.
    (but those are just my opinions)

    Work on one method at a time. What is it you are stuck on?

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Help!!

    Using better variable names and adding comments to the code would help explain what is being done and will help you remember when you look at the code after some time away from it.

    Other than explaining to you what those simple methods are doing, what do you need help with? What specifically is your question?

  4. #4
    Junior Member
    Join Date
    Sep 2012
    Posts
    20
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Help!!

    this looks like an assignment on stacking