![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Sep 2007
Posts: 5
Rep Power: 0
![]() |
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.)
//Input: A bunch of numbers
//Output: A bunch of sums
//Filename: Lab6.java
import java.util.*;
public class Lab6
{
public static void main(String [] args)
{
System.out.println("How many numbers will be input?");
Scanner keyboard = new Scanner(System.in);
int n = keyboard.nextInt();
int [] numbers = new int[n];
getNumbers(numbers);
for(int i = 0; i < n/2; i++)
System.out.println(numbers[i] + " + " + numbers[n-i] + " = "
+ numbers[i]+numbers[(n-1)-i]);
if(n % 2 != 0)
System.out.println(numbers[n/2+1] + " could not be added to anything.");
}
static void getNumbers(int [] numbers)
{
System.out.println("Please enter the numbers.");
Scanner keyboard = new Scanner(System.in);
for(int i = 0; i < numbers.length; i++)
numbers[i] = keyboard.nextInt();
return;
}
} |
|
|
|
|
|
#2 | |
|
Not a user?
Join Date: Sep 2007
Posts: 231
Rep Power: 1
![]() |
Quote:
i<n/2 is the problem i think. it should be, i<(n/2)-1 |
|
|
|
|
|
|
#3 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#4 |
|
Newbie
Join Date: Sep 2007
Posts: 5
Rep Power: 0
![]() |
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++)
System.out.println(numbers[i] + " + " + numbers[(n-1)-i] + " = "
+ numbers[i]+numbers[(n-1)-i]);I also added this to my first prompt: int n; //number of numbers to be read into the array
do
{
n = keyboard.nextInt();
if(n < 0)
System.out.println("You must input a positive amount of numbers!");
} while(n < 0); |
|
|
|
|
|
#5 | ||
|
Expert Programmer
|
Quote:
Quote:
|
||
|
|
|
|
|
#6 |
|
Newbie
Join Date: Sep 2007
Posts: 5
Rep Power: 0
![]() |
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.
|
|
|
|
|
|
#7 |
|
Expert Programmer
|
No, println() has nothing to do with it.
|
|
|
|
|
|
#8 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Actually, println has everything to do with it. Compare the output for these two statements:
System.out.println (1 + 13);
System.out.println ("here:" + 1 + 13);
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#9 |
|
Expert Programmer
|
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; Object b = "here:" + 1 + 13; System.out.println(a); System.out.println(b); |
|
|
|
|
|
#10 |
|
Hobbyist Programmer
Join Date: May 2006
Location: West Jordan, Utah, United States
Posts: 176
Rep Power: 3
![]() |
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;
int a = b + 13434; //b becomes an int
EXAMPLE: int i = 10;
EXAMPLE: String s = i + "cat"; // i becomes "10", the stringSystem.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" ); |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| relation between array and pointer | n00b | C++ | 6 | Oct 12th, 2006 3:38 PM |
| Median/Mode in arrays? {Need help} | Java|Tera | Java | 27 | Nov 29th, 2005 10:50 AM |
| fill array with unique numbers | secrecy230 | Java | 4 | Aug 9th, 2005 9:55 PM |
| random numbers in 2D array | cwl157 | Java | 4 | Apr 29th, 2005 6:08 AM |
| Installing IPB 2.03 | bh4575 | Other Web Development Languages | 0 | Apr 23rd, 2005 2:36 AM |