aspose file tools*
The moose likes Beginning Java and the fly likes Multipying array elements together Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Multipying array elements together" Watch "Multipying array elements together" New topic

Multipying array elements together

Edward Amankwa

Joined: Jun 18, 2006
Posts: 15
I have tried the code below, but when run I get zero for total size.
package arrayoffset;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class arrayOffset extends JFrame implements ActionListener{
int dim = 5;
private String theOutput = "";
private int [] collectinput1 = new int[dim];
private int [] collectinput2 = new int[dim];
private JLabel [] enter;
private JTextField [] input1;
private JTextField [] input2;
private JTextArea output;

/** Creates a new instance of arrayOffset */
public arrayOffset() {
// Collect the number of dimension from user
String num = JOptionPane.showInputDialog("Enter the number of dimension");
dim = Integer.parseInt(num);
Container con = getContentPane();
con.setLayout(new FlowLayout());
//layout components to collect data
enter = new JLabel [dim];
input1 = new JTextField[dim];
input2 = new JTextField[dim];
for(int i = 0; i<dim; i++)
enter [i]= new JLabel("Enter the upper and lower bounds for subscript:" + i);
input1 [i] = new JTextField(2);
input1 [i].addActionListener(this);
input2 [i] = new JTextField(2);
input2 [i].addActionListener(this);

//add component to display data
output = new JTextArea(10, 25);

setSize(450, 400);

public void actionPerformed(ActionEvent l)
int size [] = new int [dim];
int totalSize = 0;
//String [] myoutput = l.getActionCommand();

for(int j= 0; j<dim; j++)
collectinput1[j] = Integer.parseInt(input1 [j].getText());
collectinput2[j] = Integer.parseInt(input2 [j].getText());
size[j] = (collectinput2[j]+1)- collectinput1[j];
theOutput = "Index\tLower\t Upper\t Size\n";
for (int f=0; f < dim; f++)
totalSize *= size[f];
theOutput += f + "\t"+collectinput1[f] +"\t" + collectinput2[f]+"\t"+size[f]+"\n" ;
theOutput+="TotalSize\n" + totalSize;
marc weber

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Edward Amankwa:
I have tried the code below, but when run I get zero for total size...

Welcome to JavaRanch!

totalSize is initialized to zero, and then "modified" with the following line...

totalSize *= size[f];

Note that this is like saying totalSize = 0 * size[f];

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
Edward Amankwa

Joined: Jun 18, 2006
Posts: 15
Thanks a lot Marc for your help. Sometime is not easy to see such mistakes.
I agree. Here's the link:
subject: Multipying array elements together
Similar Threads
Changing an arrays size
wrong output
Problem with FOR loop..Need Help ASAP...
Multiplying elements of two arrays together
product all array elements without the first element