Results 1 to 12 of 12
Thread: Populate an array dynamically
- 12-31-2010, 01:04 PM #1
Member
- Join Date
- Nov 2009
- Posts
- 20
- Rep Power
- 0
Populate an array dynamically
Hi,
I'm new to arrays and was trying to figure out how to use Scanner method nextInt() to populate an array.
I first determine the number of elements in the array and use that in method populateArray(). The for loop is the tricky part which is outputting errors.
I'm sure there's a much easier way to do this so I'm open to suggestions.
thanks
Java Code:import java.util.Scanner; public class Arrays { Scanner input = new Scanner(System.in); int elementNum = input.nextInt(); int[] array = new int[elementNum]; public void populateArray() { for (int i = 0; i < array.length; i++) { int element = input.nextInt(elementNum); ++array[1 + element]; System.out.println(array[i]); }//end for }//end method }//end class
- 12-31-2010, 01:25 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
- 12-31-2010, 01:34 PM #3
Senior Member
- Join Date
- Dec 2010
- Posts
- 165
- Rep Power
- 11
Java Code:import java.util.Scanner; public class ArraysPopulate { public static void main(String[] args){ System.out.print("How many numbers? "); Scanner input = new Scanner(System.in); int elementNum = input.nextInt(); int[] array = new int[elementNum]; for( int i=0; i<array.length; i++ ){ System.out.print("Enter integer "+ (i+1) +": " ); array[i] = input.nextInt(); } printArray( array ); } public static void printArray( int[] a ) { for (int i = 0; i < a.length; i++) System.out.println(a[i]); } }//end class
- 12-31-2010, 01:39 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
@JavaHater: I know you hate Java but there's no reason to hate the posters in this forum; posting boilerplate code without any explanation is not helping them; they should do their own homework. This way there's nothing to be learned.
kind regards,
JosBuild a wall around Donald Trump; I'll pay for it.
- 12-31-2010, 02:04 PM #5
Senior Member
- Join Date
- Dec 2010
- Posts
- 165
- Rep Power
- 11
why do you assume I hate the posters?
posting boilerplate code without any explanation is not helping them; they should do their own homework. This way there's nothing to be learned.
There's nothing i can do if you are not happy with the way I do things. But i am certainly going to stick to my ways, whether you care or not, that is.
cheers
- 12-31-2010, 02:14 PM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
Most of those posters will simply copy and paste your code and turn it in as if it were their own work. They'll learn nothing but after all the copy and paste stuff they'll graduate and ruin the ICT market. Personally I don't care if you go on like this but I doubt the moderators like it.
kind regards,
JosBuild a wall around Donald Trump; I'll pay for it.
- 12-31-2010, 02:39 PM #7
Senior Member
- Join Date
- Dec 2010
- Posts
- 165
- Rep Power
- 11
that's not my concern. and certainly is not your concern as well. I am sure if its homework, their teacher will expect them to explain what the program does. If not, they would most probably fail.
There are already so many of them that "ruin the ICT market" as you speak, so why do you even care? If you really do care about such things, then why not blame the school and the teachers instead? After all , if they were to really pass these students who hand in code without explaining what they do, they are the real culprits for producing programmers that "ruin the ICT market".
- 12-31-2010, 02:47 PM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
- 12-31-2010, 02:56 PM #9
Senior Member
- Join Date
- Dec 2010
- Posts
- 165
- Rep Power
- 11
then what if you explain everything and they just copy and paste your explanation and hand them up , isn't it the same? do they learn anything? Are you going to blame yourself then ? (since you like explaining things ). I suggest you suck it up and get on with your life, because what they do afterwards we have no control over.
Last edited by JavaHater; 12-31-2010 at 03:01 PM.
- 12-31-2010, 03:01 PM #10
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
- 12-31-2010, 03:05 PM #11
Senior Member
- Join Date
- Dec 2010
- Posts
- 165
- Rep Power
- 11
- 12-31-2010, 04:31 PM #12
Member
- Join Date
- Sep 2010
- Posts
- 24
- Rep Power
- 0
I am new to Java too but this may help
I am trying to learn Java too. I am reading a book called Head First Java by Kathy Sierra & Bert Bates. In this book it goes into great detail on a java API called ArrayList which works like an array but you can manipulate it.
P.S. I had trouble learning Java until I started reading the above book and I am understanding it alot better now. It's well worth taking a look at
Similar Threads
-
dynamically populate the city combo box based on the values of state combo +ajax+jsp
By sandy1000 in forum JavaServer Pages (JSP) and JSTLReplies: 0Last Post: 12-29-2010, 11:00 AM -
dynamically populate a dropdown box using jsp
By sandy1000 in forum JavaServer Pages (JSP) and JSTLReplies: 1Last Post: 12-27-2010, 03:21 PM -
Ripping apart a array list to populate a vector
By Adrien in forum AWT / SwingReplies: 0Last Post: 03-07-2010, 10:55 PM -
Help Pls!! Jcombobox populate with mysql
By kwink in forum AWT / SwingReplies: 1Last Post: 03-23-2009, 05:11 AM -
array passing dynamically
By jazz2k8 in forum Advanced JavaReplies: 2Last Post: 10-16-2008, 11:29 PM
Bookmarks