By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
462,375 Members | 469 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 462,375 IT Pros & Developers. It's quick & easy.

Insert a number at a specified position

shashankraj1231
P: 13
I am trying to write a program in which i can insert a given number, taken as a input from the user, in a array, provided by the user at a position of the user's choice, but i am not able to do that. Please let me know what i am doing wrong.

Also, I am trying to achieve that using two arrays. Is there a possibility to achieve the same result using just one parent array?


Expand|Select|Wrap|Line Numbers
  1. import java.io.*;
  2.  
  3. public class findnum {
  4.     public static void main(String[] args) throws IOException
  5.     {
  6.        int arr[]= new int[100];
  7.        int arr2[]=new int[100];
  8.        String s1= new String();
  9.        Integer loc= new Integer(0);
  10.        Integer insert= new Integer(0);
  11.        int i=0;
  12.        BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
  13.        System.out.println("Enteger the Elements of the array, -1 to quit: ");
  14.        System.out.flush();
  15.        for( i=0; ;i++)
  16.        {
  17.            s1=br.readLine();
  18.            if((Integer.valueOf(s1))==-1)
  19.                break;
  20.            arr[i]=Integer.valueOf(s1);
  21.  
  22.        }
  23.        System.out.println("Total = "+" "+i);
  24.        System.out.println("Enter the location to insert a number :");
  25.        System.out.flush();
  26.        s1=br.readLine();
  27.        loc=Integer.valueOf(s1);
  28.        System.out.println("Enter the element to add: ");
  29.        s1= br.readLine();
  30.        insert= Integer.valueOf(s1);
  31.         for(int j=0;j<=i ;j++)
  32.         {
  33.                 if (j==loc-1)
  34.                 {
  35.                 int temp = arr[j];
  36.                 arr2[j]=insert;
  37.                 arr2[j++]=temp;
  38.                 //j++;
  39.                 }else
  40.                arr2[j]=arr[j];
  41.            }
  42.            //arr2[loc]=insert;
  43.  
  44.            for(int temp=0;temp<50;temp++)
  45.                System.out.print("-");
  46.            System.out.println(" ");
  47.           for(int j:arr2)
  48.         System.out.println(j);
  49.     }
  50. }
May 28 '10 #1

✓ answered by jkmyoung

Yes. Instead of starting from the front, start from the back of the array. If you insert a number into a position, all the other numbers before that point must be bumped forward first. Then insert your number in the gap you've created. The rest of the numbers can stay in the same spot.

Share this Question
Share on Google+
2 Replies

jkmyoung
Expert 100+
P: 2,057
Yes. Instead of starting from the front, start from the back of the array. If you insert a number into a position, all the other numbers before that point must be bumped forward first. Then insert your number in the gap you've created. The rest of the numbers can stay in the same spot.
May 31 '10 #2

shashankraj1231
P: 13
Thanks for the reply. Let me try that and see how it goes.
Jun 1 '10 #3

Post your reply

Sign in to post your reply or Sign up for a free account.