Hi guys
I need help with Inserting an Element into an Array, I'm using a book and when I code what's in the book called "Java Programming Made Easy - by Georgina Ramsamy" it comes up with many errors
this is what I have :
Please could someone explain to me what is wrong with this code and how to fix it and could you try to explain it in the simplest form
Thanks
325i
I need help with Inserting an Element into an Array, I'm using a book and when I code what's in the book called "Java Programming Made Easy - by Georgina Ramsamy" it comes up with many errors
Code:
import java.util.*;
public class Insert
{
int [] arr = new int[6];
Scanner kb = new Scanner (System.in);
public static void InsertElement()
{
Scanner kb = new Scanner (System.in);
System.out.println("Enter position to insert a value");
int pos = kb.nextInt();
System.out.println("Enter number to insert");
int num = kb.nextInt();
for(int i=arr.length -1; i>=pos;i--)
{
arr[i]= array[i-1];
}
arr[pos-1] = num;
for (int i=0;i<6;i++)
{
System.out.println(arr[i] +" ");
}
}
public static void main (String[]args)
{
for(int i=0;i<5;i++)
{
System.out.println("Enter value");
arr[i]=kb.nextInt();
}
InsertElement();
}
}
Please could someone explain to me what is wrong with this code and how to fix it and could you try to explain it in the simplest form
Thanks
325i
Last edited: