Results 1 to 12 of 12
  1. #1
    mmarkym is offline Member
    Join Date
    Nov 2009
    Posts
    20
    Rep Power
    0

    Default 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

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by mmarkym View Post
    [CODE] int element = input.nextInt(elementNum);
    ++array[1 + element];
    Did you read the API documentation and check what that method does? What's that ++array[1+element] supposed to do?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    JavaHater is offline Senior Member
    Join Date
    Dec 2010
    Posts
    165
    Rep Power
    5

    Default

    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

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    @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,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    JavaHater is offline Senior Member
    Join Date
    Dec 2010
    Posts
    165
    Rep Power
    5

    Default

    Quote Originally Posted by JosAH View Post
    @JavaHater: I know you hate Java but there's no reason to hate the posters in this forum;
    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.
    I don't feel the need to explain because its pretty simple code. And you are definitely right, they should do their own homework, that's why they can figure out what's happening on their own. If they need explanation, they can simply request it and i will explain. On the contrary, if i explain every little thing that i do, are they learning anything on their own?I expect them to run the program on their own., putting in print statements to see what's going on with the program etc. That's called learning.

    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

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by JavaHater View Post
    I don't feel the need to explain because its pretty simple code. And you are definitely right, they should do their own homework, that's why they can figure out what's happening on their own. If they need explanation, they can simply request it and i will explain. On the contrary, if i explain every little thing that i do, are they learning anything on their own?I expect them to run the program on their own., putting in print statements to see what's going on with the program etc. That's called learning.

    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.
    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,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    JavaHater is offline Senior Member
    Join Date
    Dec 2010
    Posts
    165
    Rep Power
    5

    Default

    Quote Originally Posted by JosAH View Post
    They'll learn nothing but after all the copy and paste stuff they'll graduate and ruin the ICT market.
    Jos
    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".

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by JavaHater View Post
    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".
    I'd rather blame all those forums where some users hand out boilerplate code to those students without explaining anything.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    JavaHater is offline Senior Member
    Join Date
    Dec 2010
    Posts
    165
    Rep Power
    5

    Default

    Quote Originally Posted by JosAH View Post
    I'd rather blame all those forums where some users hand out boilerplate code to those students without explaining anything.
    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.

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by JavaHater View Post
    then what if you explain everything and they just hand them up using copy and paste , isn't it the same? do they learn anything? Are you going to blame yourself then ? (since you like explaining things )
    I read your original reply before you edited it (it was sent to me by email). Discussion closed and reported.

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  11. #11
    JavaHater is offline Senior Member
    Join Date
    Dec 2010
    Posts
    165
    Rep Power
    5

    Default

    Quote Originally Posted by JosAH View Post
    Discussion closed and reported.

    Jos
    great. It all started with post #4, which is unnecessary and should be reported as well.

  12. #12
    gkoef is offline Member
    Join Date
    Sep 2010
    Posts
    24
    Rep Power
    0

    Default 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

  1. dynamically populate the city combo box based on the values of state combo +ajax+jsp
    By sandy1000 in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 12-29-2010, 11:00 AM
  2. dynamically populate a dropdown box using jsp
    By sandy1000 in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 12-27-2010, 03:21 PM
  3. Ripping apart a array list to populate a vector
    By Adrien in forum AWT / Swing
    Replies: 0
    Last Post: 03-07-2010, 10:55 PM
  4. Help Pls!! Jcombobox populate with mysql
    By kwink in forum AWT / Swing
    Replies: 1
    Last Post: 03-23-2009, 05:11 AM
  5. array passing dynamically
    By jazz2k8 in forum Advanced Java
    Replies: 2
    Last Post: 10-16-2008, 11:29 PM

Posting Permissions

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