![]() |
code to add numbers in an array
This program should be simple. I think, however, I'm misunderstanding how Java handles arrays. This code compiles fine but when my for loop tries to access the elements I get an out of bounds error. My only guess is that I've either declared my array wrong or I've misunderstood how Java handles passing by reference. Please help me.
For reference, the output is supposed to look similar to: Input: 1, 2, 3, 4 Ouput: 1+4=5, 2+3=5 :
//Lab6: Using void methods and 1D arrays to print the sum of numbers in an array (1st + nth, 2nd + nth-1, etc.) |
Quote:
i<n/2 is the problem i think. it should be, i<(n/2)-1 |
First of all, pick some small number like 3 or 5 or something, and go through your code, working it out by hand. After you've done that and fixed your code, then ask yourself what happens if the user enters -12 at the first prompt.
|
Thanks DaWei. I found it immediately when I started going through by hand. I was trying to access numbers[n-i] instead of numbers[(n-1)-i).
I have another question. Look at this segment of code. When my program outputs, it is concatenating the numbers as if they were strings. I get output like 1 + 13 = 113. Why does this happen? I've already fixed the problem by creating an integer variable and setting it equal to numbers[i]+numbers[(n-1)-i] but I'm still curious. :
for(int i = 0; i < n/2; i++)I also added this to my first prompt: :
int n; //number of numbers to be read into the array |
Quote:
Quote:
|
I see. Anything included in println() and used with the + operator is made into a string automatically. I didn't realize that. I thought only what was enclosed in " " was a string.
|
No, println() has nothing to do with it.
|
Actually, println has everything to do with it. Compare the output for these two statements:
:
System.out.println (1 + 13); |
My point, DaWei, was that the println() method has no effect on how the + operator is treated as the OP suggested in his last post, as this code demonstrates.
:
Object a = 1 + 13; |
There are three things to be taken from this:
1) PrintStream.println() converts whatever is inside to a string...basically 2) Operations inside a function call's parenthesis are evaulated before the call. 3) Operations in Java are evaluated from left to right. 4) When an operation between two types happens, the lesser type is promoted to the greater type. The operators in Java are evaluated from left to right: EXAMPLE: 1 + 2 + 3 + "a" == ((1 + 2) + 3) + "a" EXAMPLE: 1 + 2 + 3 + "a" != 1 + (2 + (3 + "a")) More specific examples are included at the bottom More than you want to know about type promotions When an operation between two types happens, the lesser type is promoted to the greater type: :
EXAMPLE: byte b = (byte)1;System.out.println( 1 + 2 ); --> 1 + 2; System.out.println( 3 ); System.out.println( "a" + 1 + 2 ); --> "a" + 1 "a1" + 2 System.out.println( "a12" ); System.out.println( 1 + 2 + "a" ); --> 1 + 2 3 + "a" System.out.println( "3a" ); |
| All times are GMT -5. The time now is 12:47 AM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC