Java Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingJava Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old March 8th, 2005, 10:11 PM
elec elec is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2005
Location: Chelsea, Ok
Posts: 1 elec User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 36 m 35 sec
Reputation Power: 0
Send a message via AIM to elec
Beginner and I need a little help

I am trying to make an array that will sum 16 numbers. This is what I got so far:

public class ArrayMatrix {
/** Main Method */


public static void main(String[] args) {

int [][] Matrix = {
{1, 2, 4, 5},
{6, 7, 8, 9},
{10, 11, 12, 13},
{14, 15, 16, 17}
};
Matrix = new int[3][3];
int Sum = 0;
int number = 0;



for (int i = 0; i < 16; i++) {

String numberString = JOptionPane.showInputDialog(null,
"Enter a integer: ",
"Example 5.6 Input", JOptionPane.QUESTION_MESSAGE);

Sum = (number + Sum);
}

String output =
"The sum of the numbers you entered is: " + Sum;
JOptionPane.showMessageDialog(null, output,
"Example 5.6 output", JOptionPane.INFORMATION_MESSAGE);


System.exit(0);
}
}



I keep getting 0 when I add everything together though. I am using BlueJ and any help will be appreciated.

Reply With Quote
  #2  
Old March 9th, 2005, 05:27 AM
gertcuppens's Avatar
gertcuppens gertcuppens is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2004
Posts: 118 gertcuppens User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 17 h 28 m
Reputation Power: 5
You write the following code :

Code:
int [][] Matrix = {
{1, 2, 4, 5},
{6, 7, 8, 9},
{10, 11, 12, 13},
{14, 15, 16, 17}
};
Matrix = new int[3][3];


It seems to me that the first declaration is superfluous.
You declare Matrix twice, so it's only the last declaration of
an empty Matrix you will be using in the rest of your program.

Then you initialize sum and number
Code:
int Sum = 0;
int number = 0; 


In the loop you note numberString but you don't do
anything with it.
You add number (which is 0) to Sum (which is also 0).
So, at the end, you're bound to keep on getting zeros.

Code:
for (int i = 0; i < 16; i++) {
 
String numberString = JOptionPane.showInputDialog(null,
"Enter a integer: ",
"Example 5.6 Input", JOptionPane.QUESTION_MESSAGE);
Sum = (number + Sum);
}
 



Reply With Quote
  #3  
Old March 10th, 2005, 11:48 AM
dejaone dejaone is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2005
Posts: 34 dejaone User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 47 m 11 sec
Reputation Power: 4
try get rid of this line Matrix = new int[3][3];
to see whether it solves your problem

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingJava Development > Beginner and I need a little help


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway