Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Java (http://www.programmingforums.org/forum17.html)
-   -   code to add numbers in an array (http://www.programmingforums.org/showthread.php?t=14061)

truBlu1 Sep 29th, 2007 3:36 AM

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;
  }
}


Jabo Sep 29th, 2007 7:42 AM

Quote:

Originally Posted by truBlu1 (Post 134432)
:

for(int i = 0; i < n/2; i++)


i<n/2 is the problem i think. it should be, i<(n/2)-1

DaWei Sep 29th, 2007 9:05 AM

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.

truBlu1 Sep 29th, 2007 12:40 PM

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);


titaniumdecoy Sep 29th, 2007 1:50 PM

Quote:

Originally Posted by truBlu1 (Post 134445)
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.

See the documentation for the String class:
Quote:

Originally Posted by Java String Documentation
The Java language provides special support for the string concatenation operator ( + ), and for conversion of other objects to strings. ... String conversions are implemented through the method toString, defined by Object and inherited by all classes in Java.


truBlu1 Sep 29th, 2007 2:09 PM

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.

titaniumdecoy Sep 29th, 2007 2:17 PM

No, println() has nothing to do with it.

DaWei Sep 29th, 2007 3:27 PM

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);


titaniumdecoy Sep 29th, 2007 4:53 PM

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);


Harakim Oct 12th, 2007 10:08 PM

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 string



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