# Thread: Deleting an element in an array

1. Member
Join Date
Dec 2010
Location
Chicago, IL
Posts
10
Rep Power
0

## Deleting an element in an array

Hey guys,
So, I'm trying to get my program to do a few things. It will ask the user how big they want the array, then it will print out the array with random numbers. Next, it will ask the user what position in the array they want deleted. It will delete that position and then print out the array with the position given deleted.

Here is my code:

Java Code:
```import java.util.Random;
import java.util.Scanner;

public class Deleting {
public static void main(String[] args) {

int numCt = 0;
int k = 0;
int[] n;
int num;

Scanner keyboard = new Scanner(System.in);

Random randomNumbers = new Random();

System.out.println("How many elements would you like to print? ");
num = keyboard.nextInt();

n = new int[num];

for (int i = 0; i < num; i++) {
n[i] = (int) (randomNumbers.nextInt(1000) + 1);

}

for (int b = 0; b < num; b++) {
System.out.print(n[b] + " ");
System.out.println();
}

System.out.println("What position would you like deleted? ");
k = keyboard.nextInt();

for (int i = k + 1; i < numCt; i++) {

n[i - 1] = n[i];

}

for (int b = 0; b < num; b++) {
int i = k;
System.out.print(n[i] + " ");
System.out.println();

}
numCt--;

}
}```

I can't figure out how to make it delete the given position.
Right now, my program will print out the given array size with random numbers. And then after asked the position they want deleted, it will print out the number in that position repeatedly to the array size given. For instance, here is a sample output:

How many elements would you like to print?
8
686
578
639
772
483
796
988
183
What position would you like deleted?
5
796
796
796
796
796
796
796
796

It prints out 8 random numbers. Then reprints the number in the position given (which is 5 in this example) 8 times.

Any help would be appreciated!

Thanks!

2. Banned
Join Date
Nov 2010
Posts
44
Rep Power
0
first of all error in logic as 796 is not the fifth but rather sixth element in array. and second logic problem is in the for loops expression statments:
Java Code:
`int i = k + 1; i < numCt; i++`

3. Member
Join Date
Dec 2010
Location
Chicago, IL
Posts
10
Rep Power
0
Isn't it counted as 0, 1, 2, 3, 4, 5, etc.?

And whats wrong with the logic in the for loop exactly? I know I'm off, but I just can't pin point the exact problem.

4. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,712
Rep Power
15
Java Code:
```for (int i = k + 1; i < numCt; i++) {
n[i - 1] = n[i];
}```

How many times do you expect this loop to be executed? Put in a System.out.println() and see how many times it *is* executed.

5. Member
Join Date
Dec 2010
Location
Chicago, IL
Posts
10
Rep Power
0
When deleting the number in position k, I wanna keep the remaining numbers in the same order. All the numbers in positions k+1 and above move down one position in the array. Number k+1 replaces number k, which is the deleted number. Number k+2 fills the spot left open when number k+1 moved. And so on. Thats what I thought I was doing.

6. Member
Join Date
Dec 2010
Location
Chicago, IL
Posts
10
Rep Power
0
I want the output to look something like this:

How many elements would you like to print?
8
686
578
639
772
483
796
988
183
What position would you like deleted?
5
686
578
639
772
483
988
183

7. Banned
Join Date
Nov 2010
Posts
44
Rep Power
0
okay last time giving solution going forward only pseudo code :D
here is what you want to do
Java Code:
```for(i=indexToDelete;i<n.length;i++)
{
n[i] = n[i+1];
}```
ofcourse this prints the entire array but i will leave that for you to figure out how to make it NOT print the entire array :)
my sample output:
Java Code:
```How many elements would you like to print? Must be between 0 and 100.5
342 655 557 970 271
what position you want deleted?: 2
342
655
970
271
0
0
0
0
0
0
0...0```
hope this helps.

8. Member
Join Date
Dec 2010
Location
Chicago, IL
Posts
10
Rep Power
0
I suppose pseudo code is good for me, that way I can learn more. =)

Call me slow, but where is that piece of code supposed to go? Is it replacing this?

Java Code:
```for (int i = k + 1; i < numCt; i++)
{
n[i - 1] = n[i];
}```

9. Banned
Join Date
Nov 2010
Posts
44
Rep Power
0
yes its basically where you have already asked the user which position they want deleted. Also keep in mind that position 2 for an array is really the 3 number not second e.g
Java Code:
```n[0] n[1] n[2]
0      1     3```
so even its third number its "index" is 2 ;)

10. Senior Member
Join Date
Mar 2010
Posts
952
Rep Power
8
Clearly this is an exercise, and I'm not sure what you have learned so far, but I would advise breaking your code up into methods.

Java Code:
```    public void listArray(int[] array) {
// TODO  write this
}

public int[] deleteItemFromArray(int[] originalArray, int indexToDelete) {
// TODO write this too
}```
It would be helpful if your listArray() method listed positions as well as values. Here's some pseudocode:

Java Code:
```    for each item in array
print index + ")  " + value```

Java Code:
```    create a new array
for indexes starting from 0 and less than indexToDelete
newArray[index] = originalArray[index]
for indexes starting from indexToDelete + 1 and less than originalArray.length
newArray[index - 1] = originalArray[index]
return newArray```
I probably gave away too much, but you still need to turn that into real Java. And test it too -- I haven't written, compiled, or tested any of this.

-Gary-

11. Member
Join Date
Dec 2010
Location
Chicago, IL
Posts
10
Rep Power
0
f1gh,

I replaced it and it gives an error for this line in specific:

Java Code:
`n[i] = n[i+1];`
This is what I got as the output:

How many elements would you like to print?
7
850
816
53
180
515
799
935
What position would you like deleted?
3
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 7
at DeleteRun.main(DeleteRun.java:36)

12. Member
Join Date
Dec 2010
Location
Chicago, IL
Posts
10
Rep Power
0
Gary,

I'm reading about methods right now. I know my professor mentioned something about us using them. I definitely plan on using them, I just have to learn them first. Lol.

Thanks for the psuedo code, I'm gonna try to apply it right now and see how it works.

13. Banned
Join Date
Nov 2010
Posts
44
Rep Power
0
Originally Posted by rania.idan
f1gh,

I replaced it and it gives an error for this line in specific:

Java Code:
`n[i] = n[i+1];`
...
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 7
at DeleteRun.main(DeleteRun.java:36)
you must have an error somewhere else here is my code below for what i told you:
Java Code:
```public class PA6 {
public static void main(String[] args) {

int n[] = new int[100];

Scanner keyboard = new Scanner(System.in);

Random randomNumbers = new Random();

System.out.print("How many elements would you like to print? Must be between 0 and 100:");
int num = keyboard.nextInt();//////// what to put here?? //////////////////

for (int i = 0; i < num; i++) {
n[i] = (int) (randomNumbers.nextInt(1000) + 1);
}
for (int b = 0; b < num; b++)

System.out.print(n[b] + " ");
System.out.println();
System.out.print("what position you want deleted?: ");
num = keyboard.nextInt();
for(int i=num;i<n.length -1;i++)
n[i] = n[i+1];
for(int i = 0;i<n.length -1;i++)
System.out.println(n[i]);

}
}```
basically its copy paste of your code with some modifications to get it running

i have used this same code to create 100 numbers (max array size) and delete up to 100 position of array without any errors.

sample output:
Java Code:
```How many elements would you like to print? Must be between 0 and 100.100
974 502 660 624 153 78 878 687 825 888 16 814 743 845 811 487 352 854 364 801 40 766 423 245 612 517 226 299 922 193 791 509 453 678 191 797 232 110 976 474 358 780 707 878 348 402 724 155 44 243 943 947 359 795 971 967 990 546 6 863 524 631 165 153 795 713 926 796 562 273 404 228 96 283 318 32 569 732 973 502 594 589 407 649 833 725 349 425 870 597 381 577 376 716 133 317 685 835 261 34
what position you want deleted?: 100
974 502 660 624 153 78 878 687 825 888 16 814 743 845 811 487 352 854 364 801 40 766 423 245 612 517 226 299 922 193 791 509
453 678 191 797 232 110 976 474 358 780 707 878 348 402 724 155 44 243 943 947 359 795 971 967 990 546 6 863 524 631 165 153
795 713 926 796 562 273 404 228 96 283 318 32 569 732 973 502 594 589 407 649 833 725 349 425 870 597 381 577 376 716 133 317
685 835 261```
as you can see after removal 34 is gone.
Last edited by f1gh; 12-04-2010 at 05:07 AM.

14. Member
Join Date
Dec 2010
Location
Chicago, IL
Posts
10
Rep Power
0
That works completey fine. I had another variable 'k' and I assigned that to the user input, instead of 'num' like you did.
I made some modifications and got rid of all the extra 0's. Thanks a bunch, again!

15. i have a similar problem... but instead of deleting the array index.... this one should delete the searched item... but even my search doesn't function well...
im a noob in java... so i don't understand much yet of java concepts... but i'll try.. by the way.. here's the code...

PHP Code:
``` public static void main (String args []) {
int[] array={10,12,32,24,54,46,75,58,9,10,151,122,13,14,15,106,17,18,191,20};
for (int c=0; c<array.length; c++)
System.out.print (array[c]+" ");
System.out.println ("");

String a=JOptionPane.showInputDialog ("Enter number search:");
int search=Integer.parseInt (a);

for (int b=0; b<array.length; b++){
if (array[b]==search)
break;
if (b==array.length)

else
System.out.println ("Found "+search);
break;
}

String f=JOptionPane.showInputDialog ("Enter number to delete:");
int del=Integer.parseInt (f);

for (int b=0; b<array.length; b++){
if (array[b]==del)
break;}

for (int c=0; c<array.length-1; c++)
System.out.print (array[c]+" ");
System.out.println ("");

}```
oh no!!!:eek:so embarrassing!!!

16. Originally Posted by speachy_15
i have a similar problem... but instead of deleting the array index.... this one should delete the searched item... but even my search doesn't function well...
im a noob in java... so i don't understand much yet of java concepts... but i'll try.. by the way.. here's the code...

[ snip ... ]

oh no!!!:eek:so embarrassing!!!
Please fix your indentation and then you'll see what is wrong with the code.

kind regards,

Jos

17. no.. it went that indentation only when i was posting it here... but just to oblige eby1.... i'd post it again...

PHP Code:
```import javax.swing.*;
public class activity2_arrayApp {

public static void main (String args []) {
int[] array={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};
for (int c=0; c<array.length; c++)
System.out.print (array[c]+" ");
System.out.println ("");

String a=JOptionPane.showInputDialog ("Enter number search:");
int search=Integer.parseInt (a);

for (int b=0; b<array.length; b++){
if (array[b]==search)
break;
if (b==20)
else if (search>20)
else
System.out.println ("Found "+search);
break;
}
for (int c=0; c<array.length-1; c++)
System.out.print (array[c]+" ");
System.out.println ("");

}

}```
this one's a weeny bit different from the first code... but i still have a problem with the search function... i know it's impractical to use the else if (search>20) especially if the array elements are random... but i don't know what else to do... and printing out the new array minus the searched item.. i don't know what to do about deleting the searched item....
thanks a lot!!!

looks like my array doesn't fit in the window... so it moved to another line...
but in original text.. it really was aligned with array[]...
Last edited by speachy_15; 12-05-2010 at 02:17 PM.

18. Your indentation still is terrible and is still giving you false information. One other thing you need to fix, all blocks, including all if blocks, else blocks, for loops, while loops, any other loops etc, all need to be enclosed in curly braces {} even if only one line long. Lack of this and faulty indentation are both messing up your logic.

19. Originally Posted by speachy_15
no.. it went that indentation only when i was posting it here... but just to oblige eby1.... i'd post it again...
What is 'eby1'? Oh, and your indentation still sucks and is heavily misleading; I, for one, can hardly read it. Indentation has to serve a purpose and needs to help the reader, not to confuse the reader.

kind regards,

Jos

20. yeah... thanks for the advice.. i've checked and rechecked and made sure all the loops were within braces... but i still have a trouble with my search and print... if i search a value higher than the max value of the array.. it still prints found....

not to mention the delete part...*sigh*...

why did i suddenly feel depressed???

Page 1 of 2 12 Last

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•