473,396 Members | 2,115 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Java Array Problem

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
5 2498
JosAH
11,448 Expert 8TB
[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
xirowei
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
JosAH
11,448 Expert 8TB
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
xirowei
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
JosAH
11,448 Expert 8TB
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

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

Similar topics

133
by: Gaurav | last post by:
http://www.sys-con.com/story/print.cfm?storyid=45250 Any comments? Thanks Gaurav
7
by: Christian Wilhelm | last post by:
Hi! I'm trying to call a Java WebService out of a .net Client. There are two Methods, one Method requires one Parameter of type Parameter, the other Method requires one Parameter of type...
2
by: Jobs | last post by:
Download the JAVA , .NET and SQL Server interview with answers Download the JAVA , .NET and SQL Server interview sheet and rate yourself. This will help you judge yourself are you really worth of...
0
by: VeeraLakshmi | last post by:
I am doing a project for internet control using Java,PHP and MySql.All sites should go through the proxy server only.We are giving access rights as allow or deny to the sites.If we type the...
0
by: r035198x | last post by:
Inheritance We have already covered one important concept of object-oriented programming, namely encapsulation, in the previous article. These articles are not articles on object oriented...
0
by: anuptosh | last post by:
Hi, I have been trying to run the below example to get a Oracle Array as an output from a Java code. This is an example I have found on the web. But, the expected result is that the code should...
5
by: benyboy | last post by:
hi, after hours of staring at my computer screen, and browsing through various internet forums i am totally stuck! I have created the following array which reads from an input file the following...
2
by: yeshello54 | last post by:
so here is my problem...in a contact manager i am trying to complete i have ran into an error..we have lots of code because we have some from class which we can use...anyways i keep getting an error...
0
Debadatta Mishra
by: Debadatta Mishra | last post by:
Introduction In this article I will provide you an approach to manipulate an image file. This article gives you an insight into some tricks in java so that you can conceal sensitive information...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.