Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Sep 29th, 2007, 3:36 AM   #1
truBlu1
Newbie
 
Join Date: Sep 2007
Posts: 5
Rep Power: 0 truBlu1 is on a distinguished road
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;
  }
}
truBlu1 is offline   Reply With Quote
Old Sep 29th, 2007, 7:42 AM   #2
Jabo
Not a user?
 
Join Date: Sep 2007
Posts: 330
Rep Power: 7 Jabo is on a distinguished road
Quote:
Originally Posted by truBlu1 View Post
for(int i = 0; i < n/2; i++)

i<n/2 is the problem i think. it should be, i<(n/2)-1
Jabo is offline   Reply With Quote
Old Sep 29th, 2007, 9:05 AM   #3
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 8,368
Rep Power: 18 DaWei will become famous soon enoughDaWei will become famous soon enough
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.
__________________
Contributor's Corner:
Politically Incorrect
DaWei on Pointers
DaWei is offline   Reply With Quote
Old Sep 29th, 2007, 12:40 PM   #4
truBlu1
Newbie
 
Join Date: Sep 2007
Posts: 5
Rep Power: 0 truBlu1 is on a distinguished road
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);
truBlu1 is offline   Reply With Quote
Old Sep 29th, 2007, 1:50 PM   #5
titaniumdecoy
Programming Guru

 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Location: California
Posts: 1,535
Rep Power: 11 titaniumdecoy will become famous soon enoughtitaniumdecoy will become famous soon enough
Send a message via AIM to titaniumdecoy
Quote:
Originally Posted by truBlu1 View Post
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.
titaniumdecoy is offline   Reply With Quote
Old Sep 29th, 2007, 2:09 PM   #6
truBlu1
Newbie
 
Join Date: Sep 2007
Posts: 5
Rep Power: 0 truBlu1 is on a distinguished road
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.
truBlu1 is offline   Reply With Quote
Old Sep 29th, 2007, 2:17 PM   #7
titaniumdecoy
Programming Guru

 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Location: California
Posts: 1,535
Rep Power: 11 titaniumdecoy will become famous soon enoughtitaniumdecoy will become famous soon enough
Send a message via AIM to titaniumdecoy
No, println() has nothing to do with it.
titaniumdecoy is offline   Reply With Quote
Old Sep 29th, 2007, 3:27 PM   #8
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 8,368
Rep Power: 18 DaWei will become famous soon enoughDaWei will become famous soon enough
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);
__________________
Contributor's Corner:
Politically Incorrect
DaWei on Pointers
DaWei is offline   Reply With Quote
Old Sep 29th, 2007, 4:53 PM   #9
titaniumdecoy
Programming Guru

 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Location: California
Posts: 1,535
Rep Power: 11 titaniumdecoy will become famous soon enoughtitaniumdecoy will become famous soon enough
Send a message via AIM to titaniumdecoy
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);
titaniumdecoy is offline   Reply With Quote
Old Oct 12th, 2007, 10:08 PM   #10
Harakim
Hobbyist Programmer
 
Join Date: May 2006
Location: West Jordan, Utah, United States
Posts: 176
Rep Power: 8 Harakim is on a distinguished road
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" );
Harakim is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 9:09 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC