By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,180 Members | 1,016 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,180 IT Pros & Developers. It's quick & easy.

Java Array Problem

P: 17
Expand|Select|Wrap|Line Numbers
  1. public class Result
  2. {
  3.     private int countA = 0;
  4.     private int countB = 0;
  5.     private int statement;
  6.     private boolean statusA = false;
  7.     private boolean statusB = false;
  8.     private int[] arrayA = new int[2]; <= the problem seem like happen at here?
  9.     private int[] arrayB = new int[2]; <= the problem seem like happen at here?
  10.  
  11.     public Result()
  12.     {
  13.  
  14.     }
  15.     public void setA(int valueA)
  16.     {
  17.         countA = valueA;
  18.     }
  19.     public void setB(int valueB)
  20.     {
  21.         countB = valueB;
  22.     }
  23.     public void setOption(int stm)
  24.     {
  25.         statement = stm;
  26.     }
  27.     public void setStatusA(boolean a)
  28.     {
  29.         statusA = a;
  30.     }
  31.     public void setStatusB(boolean b)
  32.     {
  33.         statusB = b;
  34.     }
  35.     public void resetToFalse()
  36.     {
  37.         statusA = false;
  38.         statusB = false;
  39.     }
  40.     public void sumTotal()
  41.     {
  42.         if(statement==1)
  43.         {
  44.             if(statusA == true)
  45.             {
  46.                 int temp = arrayA[0];
  47.                 arrayA[0] = temp + 1;
  48.                 System.out.println("arrayA[0] is: " + arrayA[0]); <= This line can correctly show increament of the array value everytime i click one time buttonA from another class 
  49.             }
  50.             if(statusB == true)
  51.             {
  52.                 int temp = arrayA[1];
  53.                 arrayA[1] = temp + 1;
  54.                 System.out.println("arrayA[1] is: " + arrayA[1]); <= This line can correctly show increament of the array value everytime i click one time buttonB from another class 
  55.             }
  56.         }
  57.         if(statement==2)
  58.         {
  59.             if(statusA == true)
  60.             {
  61.                 int temp = arrayB[0];
  62.                 arrayB[0] = temp + 1;
  63.                 System.out.println("arrayB[0] is: " + arrayB[0]);
  64.             }
  65.             if(statusB == true)
  66.             {
  67.                 int temp = arrayB[1];
  68.                 arrayB[1] = temp + 1;
  69.                 System.out.println("arrayB[1] is: " + arrayB[1]);
  70.             }
  71.         }
  72.     }
  73.     public  void printReport()
  74.     {
  75.         System.out.print(arrayA[0] + " " + arrayA[1] + "\n");
  76.         System.out.print(arrayB[0] + " " + arrayB[1]);
  77.     }
  78. }
The problem is when i invoke printReport() from another class, the output of the two array become:
arrayA: 0 0
arrayB: 0 0

May i know what is the problem cause the output become zero value even though code at line 48 and 54 can print out correct updated output.
Apr 6 '08 #1
Share this Question
Share on Google+
5 Replies


Expert 10K+
P: 11,448
[code]The problem is when i invoke printReport() from another class, the output of the two array become:
arrayA: 0 0
arrayB: 0 0

May i know what is the problem cause the output become zero value even though code at line 48 and 54 can print out correct updated output.
Your class works fine, i.e. it does whatever it has to do; are you sure you are
using just one 'Result' object? Possibly you have accidentally created another
Result object that does the printing while the first one did the incrementing.

kind regards,

Jos
Apr 6 '08 #2

P: 17
I been keep trace the problem for very long but i still cannot find the reason why it cant print out the correct output. Hereby i show all classes in my small Java Application. Hope anyone able to help me identify the source of the problem causing printing zero value in the array.

ButtonPanel.java
Expand|Select|Wrap|Line Numbers
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4.  
  5. public class ButtonPanel extends JPanel implements ActionListener
  6. {
  7.     private JButton jbtnA = new JButton("Button A");
  8.     private JButton jbtnB = new JButton("Button B");
  9.     private int countA = 0;
  10.     private int countB = 0;
  11.     private boolean statusA = false;
  12.     private boolean statusB = false;
  13.     private ResponsePanel responsePanel;
  14.     private Result result;
  15.  
  16.     public ButtonPanel(ResponsePanel responsePanel,Result result)
  17.     {
  18.         this.responsePanel = responsePanel;
  19.         this.result = result;
  20.  
  21.         jbtnA.setEnabled(false);
  22.         jbtnB.setEnabled(false);
  23.  
  24.         jbtnA.addActionListener(this);
  25.         jbtnB.addActionListener(this);
  26.  
  27.         add(jbtnA);
  28.         add(jbtnB);
  29.     }
  30.     public void actionPerformed(ActionEvent e)
  31.     {
  32.         if(e.getSource()==jbtnA)
  33.         {
  34.             countA += 1;//Increase count value by 1
  35.             responsePanel.setA(countA);//Send countA value to ResponsePanel setA() method
  36.             statusA = true;//Change jbtnA status from false to true
  37.             result.setStatusA(statusA);//Send status value to Result setStatusA() method
  38.             result.sumTotal();//Call Result sumTotal() method to increament countA value by 1
  39.             result.resetToFalse();//Call Result resetToFalse() to reset all button status back to false
  40.         }
  41.         if(e.getSource()==jbtnB)
  42.         {
  43.             countB += 1;
  44.             responsePanel.setB(countB);
  45.             statusB = true;
  46.             result.setStatusB(statusB);
  47.             result.sumTotal();
  48.             result.resetToFalse();
  49.         }
  50.     }
  51.     public void resetValue()
  52.     {
  53.         //Reset value back to zero
  54.         countA = 0;
  55.         countB = 0;
  56.     }
  57.     public void enableButton()
  58.     {
  59.         //Enable button
  60.         jbtnA.setEnabled(true);
  61.         jbtnB.setEnabled(true);
  62.     }
  63. }
ResponsePanel.java
Expand|Select|Wrap|Line Numbers
  1. import java.awt.*;
  2. import javax.swing.*;
  3.  
  4. public class ResponsePanel extends JPanel
  5. {
  6.     private JTextField jtfA = new JTextField(2);
  7.     private JTextField jtfB = new JTextField(2);
  8.     private int countA = 0;
  9.     private int countB = 0;
  10.  
  11.     public ResponsePanel()
  12.     {
  13.         jtfA.setEditable(false);
  14.         jtfB.setEditable(false);
  15.  
  16.         jtfA.setText("0");
  17.         jtfB.setText("0");
  18.  
  19.         add(jtfA);
  20.         add(jtfB);
  21.     }
  22.     public void setA(int valueA)
  23.     {
  24.         //Receive countA value from ButtonPanel
  25.         countA = valueA;
  26.         //Display current countA value to textfield
  27.         jtfA.setText(Integer.toString(countA));
  28.     }
  29.     public void setB(int valueB)
  30.     {
  31.         countB = valueB;
  32.         jtfB.setText(Integer.toString(countB));
  33.     }
  34.     public void resetText()
  35.     {
  36.         //Reset textfield to zero
  37.         jtfA.setText("0");
  38.         jtfB.setText("0");
  39.     }
  40. }
CenterPanel.java
Expand|Select|Wrap|Line Numbers
  1. import javax.swing.*;
  2.  
  3. public class CenterPanel extends JPanel
  4. {
  5.     private ButtonPanel buttonPanel;
  6.     private ResponsePanel responsePanel;
  7.     private Result result;
  8.  
  9.     public CenterPanel()
  10.     {
  11.         result = new Result();
  12.         responsePanel = new ResponsePanel();
  13.         buttonPanel = new ButtonPanel(responsePanel, result);
  14.  
  15.         add(buttonPanel);
  16.         add(responsePanel);
  17.     }
  18.     public ResponsePanel getResponsePanel()
  19.     {
  20.         return responsePanel;
  21.     }
  22.     public ButtonPanel getButtonPanel()
  23.     {
  24.         return buttonPanel;
  25.     }
  26.     public Result getResult()
  27.     {
  28.         return result;
  29.     }
  30. }
QuestionPanel.java
Expand|Select|Wrap|Line Numbers
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4.  
  5. public class QuestionPanel extends JPanel implements ItemListener
  6. {
  7.     private JRadioButton jrbtn1 = new JRadioButton("Option 1");
  8.     private JRadioButton jrbtn2 = new JRadioButton("Option 2");
  9.     private int statement = 1;
  10.     private ButtonPanel buttonPanel;
  11.     private ResponsePanel responsePanel;
  12.     private Result result;
  13.     private int countA, countB;
  14.  
  15.     public QuestionPanel(ButtonPanel buttonPanel,ResponsePanel responsePanel,Result result)
  16.     {
  17.         this.buttonPanel = buttonPanel;
  18.         this.responsePanel = responsePanel;
  19.         this.result = result;
  20.  
  21.         ButtonGroup option = new ButtonGroup();
  22.  
  23.         option.add(jrbtn1);
  24.         option.add(jrbtn2);
  25.  
  26.         jrbtn1.addItemListener(this);
  27.         jrbtn2.addItemListener(this);
  28.  
  29.         add(jrbtn1);
  30.         add(jrbtn2);
  31.     }
  32.     public void itemStateChanged(ItemEvent e)
  33.     {
  34.         if(jrbtn1.isSelected())
  35.         {
  36.             statement = 1;//Indicate current selected jrbtn1
  37.             buttonPanel.resetValue();//Invoke method from ButtonPanel
  38.             buttonPanel.enableButton();//Invoke method from ButtonPanel
  39.             responsePanel.resetText();//Invoke method from ResponsePanel
  40.         }
  41.         if(jrbtn2.isSelected())
  42.         {
  43.             statement = 2;
  44.             buttonPanel.resetValue();
  45.             buttonPanel.enableButton();
  46.             responsePanel.resetText();
  47.         }
  48.         result.setStatement(statement);//Send current selected JRadioButton to Result
  49.     }
  50. }
Result.java
Expand|Select|Wrap|Line Numbers
  1. public class Result
  2. {
  3.     private int countA = 0;
  4.     private int countB = 0;
  5.     private int statement;
  6.     private boolean statusA = false;
  7.     private boolean statusB = false;
  8.     private int[] arrayA = new int[2];
  9.     private int[] arrayB = new int[2];
  10.  
  11.     public Result()
  12.     {
  13.  
  14.     }
  15.     public void setStatement(int stm)
  16.     {
  17.         statement = stm;//Received value from QuestionPanel
  18.     }
  19.     public void setStatusA(boolean a)
  20.     {
  21.         statusA = a;//Received and indicate current click JButton is jbtnA from ButtonPanel
  22.     }
  23.     public void setStatusB(boolean b)
  24.     {
  25.         statusB = b;//Received and indicate current click JButton is jbtnB from ButtonPanel
  26.     }
  27.     public void resetToFalse()
  28.     {
  29.         //Reset JButton status back to false
  30.         statusA = false;
  31.         statusB = false;
  32.     }
  33.     public void sumTotal()
  34.     {
  35.         if(statement==1)
  36.         {
  37.             if(statusA == true)
  38.             {
  39.                 int temp = arrayA[0];
  40.                 arrayA[0] = temp + 1;
  41.                 System.out.println("arrayA[0] is: " + arrayA[0]);
  42.             }
  43.             if(statusB ==true)
  44.             {
  45.                 int temp = arrayA[1];
  46.                 arrayA[1] = temp + 1;
  47.                 System.out.println("arrayA[1] is: " + arrayA[1]);
  48.             }
  49.         }
  50.         if(statement==2)
  51.         {
  52.             if(statusA == true)
  53.             {
  54.                 int temp = arrayB[0];
  55.                 arrayB[0] = temp + 1;
  56.                 System.out.println("arrayB[0] is: " + arrayB[0]);
  57.             }
  58.             if(statusB ==true)
  59.             {
  60.                 int temp = arrayB[1];
  61.                 arrayB[1] = temp + 1;
  62.                 System.out.println("arrayB[1] is: " + arrayB[1]);
  63.             }
  64.         }
  65.     }
  66.     public void printReport()
  67.     {
  68.         System.out.print(arrayA[0] + " " + arrayA[1] + "\n");
  69.         System.out.print(arrayB[0] + " " + arrayB[1]);
  70.     }
  71. }
MySurvey.java
Expand|Select|Wrap|Line Numbers
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4.  
  5. public class MySurvey extends JFrame implements ActionListener
  6. {
  7.     private ButtonPanel buttonPanel;
  8.     private QuestionPanel questionPanel;
  9.     private ResponsePanel responsePanel;
  10.     private CenterPanel centerPanel;
  11.     private Result result;
  12.     private JMenu menuFile = new JMenu("File");
  13.     private JMenu menuPrint = new JMenu("Print");
  14.     private JMenuBar menuBar = new JMenuBar();
  15.     private JMenuItem menuFileExit = new JMenuItem("Exit");
  16.     private JMenuItem menuPrintReport = new JMenuItem("Generate Report");
  17.  
  18.     public MySurvey(String header)
  19.     {
  20.         menuFile.add(menuFileExit);
  21.         menuPrint.add(menuPrintReport);
  22.         menuFileExit.addActionListener(this);
  23.         menuPrintReport.addActionListener(this);
  24.  
  25.         menuBar.add(menuFile);
  26.         menuBar.add(menuPrint);
  27.  
  28.         this.setJMenuBar(menuBar);
  29.  
  30.         result = new Result();
  31.         centerPanel = new CenterPanel();
  32.         buttonPanel = new ButtonPanel(centerPanel.getResponsePanel(),centerPanel.getResult());
  33.         questionPanel = new QuestionPanel(centerPanel.getButtonPanel(),centerPanel.getResponsePanel(),centerPanel.getResult());
  34.  
  35.         add(questionPanel, BorderLayout.NORTH);
  36.         add(centerPanel, BorderLayout.CENTER);
  37.  
  38.         getContentPane();
  39.     }
  40.     public void actionPerformed(ActionEvent e)
  41.     {
  42.         if(e.getSource()==menuFileExit)
  43.         {
  44.             System.exit(0);//Exit the system
  45.         }
  46.         if(e.getSource()==menuPrintReport)
  47.         {
  48.             result.printReport();//Invoke printReport() method from Result
  49.         }
  50.     }
  51. }
Driver.java
Expand|Select|Wrap|Line Numbers
  1. import javax.swing.*;
  2.  
  3. public class Driver
  4. {
  5.     public static void main(String args[])
  6.     {
  7.         MySurvey survey = new MySurvey("Student Survey");
  8.         survey.setVisible(true);
  9.         survey.pack();
  10.         survey.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); <= this line of code is JFrame.EXIT_ON_CLOSE [the output in bytes.com show some space between CLO  SE, display bug in bytes.com?]
  11.     }
  12. }
]
Apr 6 '08 #3

Expert 10K+
P: 11,448
Carefully read my reply #2 again: you have two different Result objects, one
does the printing (it is unaltered) and the other one is manipulated. Just search
for "new Result" and see for yourself.

kind regards,

Jos
Apr 6 '08 #4

P: 17
Carefully read my reply #2 again: you have two different Result objects, one
does the printing (it is unaltered) and the other one is manipulated. Just search
for "new Result" and see for yourself.

kind regards,

Jos
Thanks for the advise on finding "new Result". I didn't aware that what I'm doing actually is initialize another Result object rather that manipulated on existing method. Now I finally know the problem is locate on MySurvey.java line 48. Need change from result.printReport(); to centerPanel.getResult().printReport();

Jos thank you very much.
Apr 6 '08 #5

Expert 10K+
P: 11,448
Thanks for the advise on finding "new Result". I didn't aware that what I'm doing actually is initialize another Result object rather that manipulated on existing method. Now I finally know the problem is locate on MySurvey.java line 48. Need change from result.printReport(); to centerPanel.getResult().printReport();

Jos thank you very much.
Good you've figured it out. A handy rule of thumb here is 'ownership', i.e. one thing
'owns' an object (Result) and others should ask the owner for it (your getResult()
method). If things get complicated you can change ownership but then another
object is responsible for the 'owned' object again. Normally an owner creates the
objects it owns (and possibly destroys them again afterwards).

If you find difficulties implementing the scenario above you've probably selected
a wrong owner.

kind regards,

Jos
Apr 6 '08 #6

Post your reply

Sign in to post your reply or Sign up for a free account.