Results 1 to 20 of 23
Thread: Deleting an element in an array
- 12-04-2010, 12:31 AM #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!
- 12-04-2010, 01:27 AM #2
Member
- 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++
- 12-04-2010, 01:34 AM #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.
- 12-04-2010, 01:35 AM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
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.
- 12-04-2010, 01:41 AM #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.
- 12-04-2010, 01:44 AM #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
- 12-04-2010, 02:05 AM #7
Member
- 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
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 :)Java Code:for(i=indexToDelete;i<n.length;i++) { n[i] = n[i+1]; }
my sample output:
hope this helps.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
- 12-04-2010, 02:19 AM #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]; }
- 12-04-2010, 02:33 AM #9
Member
- 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
so even its third number its "index" is 2 ;)Java Code:n[0] n[1] n[2] 0 1 3
- 12-04-2010, 02:53 AM #10
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
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.
It would be helpful if your listArray() method listed positions as well as values. Here's some pseudocode:Java Code:public void listArray(int[] array) { // TODO write this } public int[] deleteItemFromArray(int[] originalArray, int indexToDelete) { // TODO write this too }
That would help your user choose which item they want to delete. Pseudocode for deleteItemFromArray():Java Code:for each item in array print index + ") " + value
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.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
-Gary-
- 12-04-2010, 03:38 AM #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:
This is what I got as the output:Java Code:n[i] = n[i+1];
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-04-2010, 03:41 AM #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.
- 12-04-2010, 04:03 AM #13
Member
- Join Date
- Nov 2010
- Posts
- 44
- Rep Power
- 0
you must have an error somewhere else here is my code below for what i told you:
basically its copy paste of your code with some modifications to get it runningJava 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]); } }
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:
as you can see after removal 34 is gone.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
Last edited by f1gh; 12-04-2010 at 04:07 AM.
- 12-04-2010, 04:11 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!
- 12-05-2010, 11:34 AM #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...
oh no!!!:eek:so embarrassing!!!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) System.out.println (search+" not found!"); 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 (""); }
- 12-05-2010, 12:48 PM #16
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,400
- Blog Entries
- 7
- Rep Power
- 17
- 12-05-2010, 01:08 PM #17
no.. it went that indentation only when i was posting it here... but just to oblige eby1.... i'd post it again...
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....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) System.out.println (search+" not found!"); else if (search>20) System.out.println (search+" not found!"); else System.out.println ("Found "+search); break; } for (int c=0; c<array.length-1; c++) System.out.print (array[c]+" "); System.out.println (""); } }
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 01:17 PM.
-
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.
- 12-05-2010, 01:25 PM #19
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,400
- Blog Entries
- 7
- Rep Power
- 17
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 12-05-2010, 01:50 PM #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???
Similar Threads
-
Variable of an object in an array compared to an element of another array?
By asmodean in forum New To JavaReplies: 23Last Post: 09-07-2010, 08:12 PM -
Trying to make an array list // inserting an element to middle of array
By javanew in forum New To JavaReplies: 2Last Post: 09-06-2010, 01:03 AM -
deleting an element from an array
By moamen in forum New To JavaReplies: 11Last Post: 01-03-2010, 05:38 PM -
Deleting from an array
By Dieter in forum Advanced JavaReplies: 13Last Post: 09-25-2009, 09:27 AM -
How to add an integer to a array element and the store that backinto an array.
By Hannguoi in forum New To JavaReplies: 1Last Post: 03-31-2009, 06:40 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks