Total members 11894 |It is currently Sat Nov 23, 2024 1:46 pm Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





Hello,
I am working on the problem.
What i need to do is to Add an integer at the beginning of the array and at the end of an array
I keep getting "outofbound" exception, which I can't resolve.
Can you help?
here is my code

Main//
Code:


import java
.io.*;
public class ArrayMain {

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

        BufferedReader stdin = new BufferedReader(new InputStreamReader( System.in ) );
    int a = 0;
    int b = 0;
    String temp;
        ArrayC c = new ArrayC();

    System.out.println("Enter the size of the array so it can be filled: ");
    temp = stdin.readLine();
    a = Integer.parseInt( temp ); // convert inS to int using wrapper classes

    System.out.println("Enter an integer number that will be allocated to the approprite place in the array: ");
    temp = stdin.readLine();
    b = Integer.parseInt( temp ); // convert inS to int using wrapper classes
   
     c
.addAtBegin(a, b);
     c.addAtEnd(a, b);
    }
}


//CLASS
public class ArrayC
{
    // instance variables
   // private int b [];
    private int a;
    private int a1;

    final int MAX_VAL = 100;

    /**
     * Constructor class StoreArray
     */

    public ArrayC()
    {
       a = 0;
      a1 = 0;
      //b = new int [99];
    }
   

    public void addAtBegin
(int n, int m) // adding an integer at the beginning of the array
    {
        a = n;
        a1 = m;
        int i;
        
        if 
(<= MAX_VAL)
        {
        int [] b = new int [a];
        
        for 
(= 0; i < b.length; i++)
            b[i] = i;
       // System.out.println("test display:" + b[i]);

        for(= a; i >= 0; i--)        // move all the integers by one
        b[i+1] = b[i];
        b[0] = a1;
        
        for 
(=0; i < b.length; i++)
               System.out.println("All Values of the Array including the new added value is: ");
            System.out.println(+ b[i]);
        }
        else
        
{
            System.out.println("Error");
        }
        

    
}

        public void addAtEnd(int n, int m) // adding an integer at the End of the array
    {
        a = n;

        // input desirable number of integers into array
        if (<= MAX_VAL)
        {
        int  [] b = new int [100];
        int i;
        for (= 0; i < a; i++)
            b[i] = i;
        System.out.println("test display:" + b[i]); // just a testing but need to learn how to display all the values though


        // Now, input integer the integer to the end of an array
         a1 = m;
         b[i+1] = a1;
        /* move all the integers by one
        for(i= a; i>= a+1; i++)
            b[i+1] = b[i];
        b[a] = a1; */
        System.out.println("test value of the last element " + b[i+1]);
        }
        else
        
{
            System.out.println("Error");
        }

      
    
}

}
 





Author:
Newbie
User avatar Posts: 1
Have thanks: 0 time
Post new topic Reply to topic  [ 1 post ] 

  Related Posts  to : outofbound error (arrays)
 Arrays using Pointers     -  
 Concept of arrays     -  
 Arrays in photoshop     -  
 Multidimensional Arrays in JSP     -  
 Defining arrays in ASP     -  
 Arrays in java     -  
 Computes the intersection of arrays     -  
 Variable Size Arrays     -  
 Multidimensional arrays in Java     -  
 Get union of two arrays set and print it     -  









Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com