Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Page 1 of 2 12 LastLast
Results 1 to 25 of 40

Thread: A little assignment involving arrays.

  1. #1
    Member
    Join Date
    Mar 2010
    Posts
    271
    Thanks
    21
    Thanked 7 Times in 7 Posts

    Default A little assignment involving arrays.

    Hello everyone.
    I'm having a little problem with an activity I have to do. I have to "Write a program that stores the first 400 numbers that are multiples of 13 in an array".

    My Current code:
    class ThirteenArray {
        public static void main(String[] args){
            //Hour 9, Second activity.
            int thirteen, multiply;
     
           for(thirteen = 13, multiply = 1; multiply * thirteen <= 400; multiply++){
                int[] sum = new int[multiply] sum;
                System.out.print(" " + sum[multiply]);
            }
        }
    }

    Output:

    run:
    Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - not a statement
    at ThirteenArray.main(ThirteenArray.java:7)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 15 seconds)

    IDE Flaged Line 7:
    int[] sum = new int[multiply] sum;


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: A little assignment involving arrays.

    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Member
    Join Date
    Mar 2010
    Posts
    271
    Thanks
    21
    Thanked 7 Times in 7 Posts

    Default Re: A little assignment involving arrays.

    Changed it up a bit.

    class ThirteenArray {
        public static void main(String[] args){
            //Hour 9, Second activity.
            int multiply = 13;
            int[] sum = new int[1200];
           for(int thirteen = 1; thirteen <= 400; thirteen++){
               multiply = multiply * thirteen;
                sum[multiply] = multiply;
                System.out.print(" " + sum[multiply]);
            }
        }
    }

    Output: run:
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1560
    at ThirteenArray.main(ThirteenArray.java:8)
    13 26 78 312Java Result: 1
    BUILD SUCCESSFUL (total time: 1 second)

    If your wondering why I set the sum array to 1200 elements, it's because I was getting the ArrayIndexOutOfBounds error message. So I raised it up a few times but still get that error message.

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: A little assignment involving arrays.

    The Exception is pretty self-explanatory. Your array has 1200 elements, so 1199 is the last index. You're trying to access index 1560, which is way off. Why are you holding such a small number of items in such a large array like that?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Member
    Join Date
    Mar 2010
    Posts
    271
    Thanks
    21
    Thanked 7 Times in 7 Posts

    Default Re: A little assignment involving arrays.

    To be honest I am not quite sure of everything that is happening. I get about everything expect line 8 to be exact. I'm just experimenting, trying to find a way that works. And when I do, understand why and how.

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: A little assignment involving arrays.

    First off, you only have to store 400 numbers. So your array should have 400 indexes. And you don't want to store the values to the array in indexes equal to the value, you store them sequentially (first value at index 0, second value at index 1, etc.).
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    Member
    Join Date
    Mar 2010
    Posts
    271
    Thanks
    21
    Thanked 7 Times in 7 Posts

    Default Re: A little assignment involving arrays.

    I see, is there a way I can make the for loop do that?

  8. #8
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: A little assignment involving arrays.

    I don't understand the question. You already have a loop that goes from 1 to 400 in your original post. It shouldn't be too hard to loop from 0 to 399.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  9. #9
    Member
    Join Date
    Mar 2010
    Posts
    271
    Thanks
    21
    Thanked 7 Times in 7 Posts

    Default Re: A little assignment involving arrays.

    I meant to make each element of the sum array equal to the first 400 multiples of 13.

    Instead of doing it line by line like this.
    sum[0] = 13;
    sum[1] = 26;
    sum[2] = 39;
    sum[3] = 52;
    // and so on
    Last edited by Melawe; March 7th, 2011 at 02:48 PM.

  10. #10
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: A little assignment involving arrays.

    I understand that part, I just don't understand why you were asking about making a loop when you already have almost exactly what you needed in your original post.

    Think about the relationship between the index and the value you want to store at that position.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  11. #11
    Member
    Join Date
    Mar 2010
    Posts
    271
    Thanks
    21
    Thanked 7 Times in 7 Posts

    Default Re: A little assignment involving arrays.

    :/ Everything I've been trying gave me an error message.

  12. #12
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: A little assignment involving arrays.

    I'm guessing this is what you meant.
    for (int i=0; i < 400; i++)
    {
    sum[i] = 13 * (i+1);
    }

  13. #13
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: A little assignment involving arrays.

    Once again, I'm forced to tell you that spoonfeeding is NOT helping. You have robbed yet another OP of the process of figuring out the problem, which is extremely important for a programmer to learn, and then having that "ah-ha!" moment, which programmers love.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  14. #14
    Member
    Join Date
    Mar 2010
    Posts
    271
    Thanks
    21
    Thanked 7 Times in 7 Posts

    Default Re: A little assignment involving arrays.

    Is there any reason for Net beans to run a file and display the output with no problems? And display an error message the second time the file was run even though there were no changes made to the file...
    Last edited by Melawe; March 9th, 2011 at 06:39 AM.

  15. #15
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: A little assignment involving arrays.

    Quote Originally Posted by Melawe View Post
    Is there any reason for Net beans to run a file and display the output with no problems? And display an error message the second time the file was run even though there were no changes made to the file...
    What is the error?
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  16. #16
    Member
    Join Date
    Mar 2010
    Posts
    271
    Thanks
    21
    Thanked 7 Times in 7 Posts

    Default Re: A little assignment involving arrays.

    Object.
    public class Virus {
        //Hour 11, Workshop, object.
        static int virusCount = 0;
     
        public Virus() {
            virusCount++;
        }
     
        static int getVirusCount() {
            return virusCount;
        }
    }
    Main class.
    class ZTesting {
     
        public static void main(String[] args) {
            int numViruses = Integer.parseInt(args[0]);
            if (numViruses > 0) {
                Virus[] virii = new Virus[numViruses];
                for (int i = 0; i < numViruses; i++) {
                    virii[i] = new Virus();
                }
                System.out.println("There are " + Virus.getVirusCount()
                    + " viruses.");
            }
        }
    }

    Argument set as : 36.

    Error displayed:
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
    at ZTesting.main(ZTesting.java:4)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 0 seconds)

  17. #17
    Member
    Join Date
    Mar 2010
    Posts
    271
    Thanks
    21
    Thanked 7 Times in 7 Posts

    Default Re: A little assignment involving arrays.

    Updated code:
    class ThirteenArray {
        public static void main(String[] args){
            //Hour 9, Second activity.
            int multiply = 13;
            int[] sum = new int[400];
           for(int thirteen = 1; thirteen <= 400; thirteen++){
               //multiply = multiply * thirteen;
                sum[thirteen] = 13 * (thirteen + 1);
                System.out.print(" " + sum[thirteen]);
            }
        }
    }

    Output:
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 400
    26 39 52 65 78 91 104 117 130 143 156 169 182 195 208.... 5122 5135 5148 5161 5174 5187 5200 at ThirteenArray.main(ThirteenArray.java:8)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 2 seconds)

    Line 8:
    sum[thirteen] = 13 * (thirteen + 1);
    Last edited by Melawe; March 9th, 2011 at 07:24 PM.

  18. #18
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: A little assignment involving arrays.

    You never increment the variable in your loop. I'm actually a little surprised that compiles.

    But even when you fix that, you're still accessing the indexes wrongly- remember, the indexes go from 0-399, which gives you 400 indexes. Array indexes don't start at 1, and they don't go all the way to the size of the array.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  19. #19
    Member
    Join Date
    Mar 2010
    Posts
    271
    Thanks
    21
    Thanked 7 Times in 7 Posts

    Default Re: A little assignment involving arrays.

    Excuse that, removed it to try something and forgot to add it later on, I updated the post please take a look at it again.

    Oh and I change the for loop to the way JavaPenguin said to do it, what's wrong with the loop?
    Last edited by Melawe; March 9th, 2011 at 07:02 PM.

  20. #20
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: A little assignment involving arrays.

    So, what does your program do now? You need to provide more information if you want any help. I explained how you're going out of the bounds of the array. What about that don't you understand?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  21. #21
    Member
    Join Date
    Mar 2010
    Posts
    271
    Thanks
    21
    Thanked 7 Times in 7 Posts

    Default Re: A little assignment involving arrays.

    hmmm, so I have to set the highest element to 401?

  22. #22
    Member
    Join Date
    Mar 2010
    Posts
    271
    Thanks
    21
    Thanked 7 Times in 7 Posts

    Default Re: A little assignment involving arrays.

    Update code:
    class ThirteenArray {
        public static void main(String[] args){
            //Hour 9, Second activity.
            int multiply = 13;
            int[] sum = new int[401];
           for(int thirteen = 1; thirteen <= 399; thirteen++){
               //multiply = multiply * thirteen;
                sum[thirteen] = 13 * (thirteen + 1);
                System.out.print(" " + sum[thirteen]);
            }
        }
    }

    Output:
    26 39 52 65 78 91 104 117 130 143 156 169 182 195 208 5096 5109 5122 5135 5148 5161 5174 5187 5200 5200BUILD SUCCESSFUL (total time: 3 seconds)


    Thanks everyone!

  23. #23
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: A little assignment involving arrays.

    Quote Originally Posted by Melawe View Post
    hmmm, so I have to set the highest element to 401?
    No. Like I told you, like javapenguin spoonfed you, and like it undoubtedly says on every array tutorial out there- indexes start at zero, not one. So you start at zero and go up to the number of indexes minus one. What you have now does not print out 400 numbers.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  24. #24
    Member
    Join Date
    Mar 2010
    Posts
    271
    Thanks
    21
    Thanked 7 Times in 7 Posts

    Default Re: A little assignment involving arrays.

    Is that comment for the updated code too?

  25. #25
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: A little assignment involving arrays.

    Quote Originally Posted by Melawe View Post
    Is that comment for the updated code too?
    Yes. If you don't believe me, try figuring out what's in the first index (which is index zero). Or try printing out the number of elements in the array. Hint- increment a variable each time you add something to the array, then print it out after your loop completes.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Page 1 of 2 12 LastLast

Similar Threads

  1. Really need help on assignment
    By coorscollector in forum Java Theory & Questions
    Replies: 2
    Last Post: January 16th, 2012, 06:08 PM
  2. need help on an assignment :(
    By gamfreak in forum What's Wrong With My Code?
    Replies: 6
    Last Post: February 23rd, 2010, 04:20 PM
  3. Replies: 1
    Last Post: February 22nd, 2010, 08:20 AM
  4. Need help with assignment
    By TonyL in forum Loops & Control Statements
    Replies: 2
    Last Post: February 20th, 2010, 09:44 PM
  5. Connect 4 involving minimax.
    By mandar9589 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 21st, 2009, 10:52 AM