Whats happening in sum[thirteen] = multiply * thirteen;? All elements in an array are numbered, so why doesn't sum[thirteen] give me an exception message or something?
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.
Whats happening in sum[thirteen] = multiply * thirteen;? All elements in an array are numbered, so why doesn't sum[thirteen] give me an exception message or something?
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!
Kevin I am trying to follow your advice but not understanding line 8is really being a hurdle. I really don't understand that line at all, it was used in another project from the Jin24 so I thought I'd try it and see if it works.sum[thirteen] = multiply * thirteen;
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!
Sorry I didn't post in about a week. Netbeans started to compile forever(at least for how long I let it), reinstalled it and it seams to be working as it should.
Meanwhile I've been trying to figure out what was going on in line 8, did a few searches online, and found out what was happening. And after a few tries got it right!
Kevin I think I got you a little angry with all the line 8 questions, sorry if so. Was trying everything I could think of and none worked, and I thought sum[thirteen] was causing the mistake.:/
Here is the updated code and output below.
Updated code:
class ThirteenArray { public static void main(String[] args){ //Hour 9, Second activity. int multiply = 13; int[] sum = new int[400]; int num = 1; for(int thirteen = 0; thirteen < sum.length; thirteen++){ sum[thirteen] = multiply * num; System.out.print(" " + sum[thirteen]); num++; } System.out.print("\n " + sum[0]); } }
Output:
run:
13 26 39 52 65 78 91.... 5174 5187 5200
13BUILD SUCCESSFUL (total time: 0 seconds)
Thanks everyone for taking the time to help me!!
Last edited by Melawe; March 27th, 2011 at 09:33 PM.
I'm glad you got it working, but for posterity's sake, I should just point out that your "num" variable isn't really necessary.
Hint- What is sum when compared to thirteen each iteration?
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!
When I use the code below sum[0] is always being set to 0.
class ThirteenArray { public static void main(String[] args){ //Hour 9, Second activity. int multiply = 13; int[] sum = new int[400]; //int num = 1; for(int thirteen = 0; thirteen < sum.length; thirteen++){ sum[thirteen] = multiply * thirteen; System.out.print(" " + sum[thirteen]); //num++; } System.out.print("\n " + sum[0]); } }
Output:
0 13 26 39 52 65 78 91.... 5148 5161 5174 5187
0BUILD SUCCESSFUL (total time: 1 second)
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!
Hopefully you can print the table (2X1=2, 2X2=4 ..............), just use a little logic and relationships of the indexes being transfered through the loop and you will be able to do that...
What would happen if you started with
sum[thirteen] = multiply * (thirteen + 1);
?
The first value of thirteen is 0, so sum[0] = multiply * 0 = 0 the way you have it. (I think Mr. 777 said that above already.)
If you change it like I recommend you do, it will do:
sum[0] = multiply * (0+1) = 13
Sorry I haven't been posted any updates. PC is acting up keeps giving a nasty "Error loading os." message. So I've been backing up and hadn't much time for coding. I did get a chance to update the code and I think I got it this time.
Output: 13 26 39 52....5161 5174 5187 5200
Does that do it with out anything unnecessary?
Changed the names of the variables to something more suitable.
Anything I could do better?
I would still say your multiply variable is pretty unnecessary.
But if I were you, I wouldn't spend so much time making sure everything is perfect. Does it work? Does it do what you expect? If so, kudos. Move on to the next thing. You'll drive yourself crazy if you keep trying to make your code perfect this early in the game.
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!
Thanks all!