Java Array Problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • xirowei
    New Member
    • Jun 2007
    • 17

    Java Array Problem

    Code:
    public class Result
    {
    	private int countA = 0;
    	private int countB = 0;
    	private int statement;
    	private boolean statusA = false;
    	private boolean statusB = false;
    	[B]private int[] arrayA = new int[2]; [B]<= the problem seem like happen at here?[/B]
    	private int[] arrayB = new int[2];[/B] [B]<= the problem seem like happen at here?[/B]
    
    	public Result()
    	{
    
    	}
    	public void setA(int valueA)
    	{
    		countA = valueA;
    	}
    	public void setB(int valueB)
    	{
    		countB = valueB;
    	}
    	public void setOption(int stm)
    	{
    		statement = stm;
    	}
    	public void setStatusA(boolean a)
    	{
    		statusA = a;
    	}
    	public void setStatusB(boolean b)
    	{
    		statusB = b;
    	}
    	public void resetToFalse()
    	{
    		statusA = false;
    		statusB = false;
    	}
    	public void sumTotal()
    	{
    		if(statement==1)
    		{
    			if(statusA == true)
    			{
    				int temp = arrayA[0];
    				arrayA[0] = temp + 1;
    				[B][I]System.out.println("arrayA[0] is: " + arrayA[0]);[/I][/B] [B]<= This line can correctly show increament of the array value everytime i click one time buttonA from another class[/B] 
    			}
    			if(statusB == true)
    			{
    				int temp = arrayA[1];
    				arrayA[1] = temp + 1;
    				[B][I]System.out.println("arrayA[1] is: " + arrayA[1]);[/I][/B] [B]<= This line can correctly show increament of the array value everytime i click one time buttonB from another class[/B] 
    			}
    		}
    		if(statement==2)
    		{
    			if(statusA == true)
    			{
    				int temp = arrayB[0];
    				arrayB[0] = temp + 1;
    				System.out.println("arrayB[0] is: " + arrayB[0]);
    			}
    			if(statusB == true)
    			{
    				int temp = arrayB[1];
    				arrayB[1] = temp + 1;
    				System.out.println("arrayB[1] is: " + arrayB[1]);
    			}
    		}
    	}
    	[I][B]public  void printReport()
    	{
    		System.out.print(arrayA[0] + " " + arrayA[1] + "\n");
    		System.out.print(arrayB[0] + " " + arrayB[1]);
    	}[/B][/I]
    }
    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.
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by xirowei
    [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

    Comment

    • xirowei
      New Member
      • Jun 2007
      • 17

      #3
      Java Array Problem, unable to print out accumulate value.

      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.jav a
      Code:
      import java.awt.*;
      import java.awt.event.*;
      import javax.swing.*;
      
      public class ButtonPanel extends JPanel implements ActionListener
      {
      	private JButton jbtnA = new JButton("Button A");
      	private JButton jbtnB = new JButton("Button B");
      	private int countA = 0;
      	private int countB = 0;
      	private boolean statusA = false;
      	private boolean statusB = false;
      	private ResponsePanel responsePanel;
      	private Result result;
      
      	public ButtonPanel(ResponsePanel responsePanel,Result result)
      	{
      		this.responsePanel = responsePanel;
      		this.result = result;
      
      		jbtnA.setEnabled(false);
      		jbtnB.setEnabled(false);
      
      		jbtnA.addActionListener(this);
      		jbtnB.addActionListener(this);
      
      		add(jbtnA);
      		add(jbtnB);
      	}
      	public void actionPerformed(ActionEvent e)
      	{
      		if(e.getSource()==jbtnA)
      		{
      			countA += 1;//Increase count value by 1
      			responsePanel.setA(countA);//Send countA value to ResponsePanel setA() method
      			statusA = true;//Change jbtnA status from false to true
      			result.setStatusA(statusA);//Send status value to Result setStatusA() method
      			result.sumTotal();//Call Result sumTotal() method to increament countA value by 1
      			result.resetToFalse();//Call Result resetToFalse() to reset all button status back to false
      		}
      		if(e.getSource()==jbtnB)
      		{
      			countB += 1;
      			responsePanel.setB(countB);
      			statusB = true;
      			result.setStatusB(statusB);
      			result.sumTotal();
      			result.resetToFalse();
      		}
      	}
      	public void resetValue()
      	{
      		//Reset value back to zero
      		countA = 0;
      		countB = 0;
      	}
      	public void enableButton()
      	{
      		//Enable button
      		jbtnA.setEnabled(true);
      		jbtnB.setEnabled(true);
      	}
      }
      ResponsePanel.j ava
      Code:
      import java.awt.*;
      import javax.swing.*;
      
      public class ResponsePanel extends JPanel
      {
      	private JTextField jtfA = new JTextField(2);
      	private JTextField jtfB = new JTextField(2);
      	private int countA = 0;
      	private int countB = 0;
      
      	public ResponsePanel()
      	{
      		jtfA.setEditable(false);
      		jtfB.setEditable(false);
      
      		jtfA.setText("0");
      		jtfB.setText("0");
      
      		add(jtfA);
      		add(jtfB);
      	}
      	public void setA(int valueA)
      	{
      		//Receive countA value from ButtonPanel
      		countA = valueA;
      		//Display current countA value to textfield
      		jtfA.setText(Integer.toString(countA));
      	}
      	public void setB(int valueB)
      	{
      		countB = valueB;
      		jtfB.setText(Integer.toString(countB));
      	}
      	public void resetText()
      	{
      		//Reset textfield to zero
      		jtfA.setText("0");
      		jtfB.setText("0");
      	}
      }
      CenterPanel.jav a
      Code:
      import javax.swing.*;
      
      public class CenterPanel extends JPanel
      {
      	private ButtonPanel buttonPanel;
      	private ResponsePanel responsePanel;
      	private Result result;
      
      	public CenterPanel()
      	{
      		result = new Result();
      		responsePanel = new ResponsePanel();
      		buttonPanel = new ButtonPanel(responsePanel, result);
      
      		add(buttonPanel);
      		add(responsePanel);
      	}
      	public ResponsePanel getResponsePanel()
      	{
      		return responsePanel;
      	}
      	public ButtonPanel getButtonPanel()
      	{
      		return buttonPanel;
      	}
      	public Result getResult()
      	{
      		return result;
      	}
      }
      QuestionPanel.j ava
      Code:
      import java.awt.*;
      import java.awt.event.*;
      import javax.swing.*;
      
      public class QuestionPanel extends JPanel implements ItemListener
      {
      	private JRadioButton jrbtn1 = new JRadioButton("Option 1");
      	private JRadioButton jrbtn2 = new JRadioButton("Option 2");
      	private int statement = 1;
      	private ButtonPanel buttonPanel;
      	private ResponsePanel responsePanel;
      	private Result result;
      	private int countA, countB;
      
      	public QuestionPanel(ButtonPanel buttonPanel,ResponsePanel responsePanel,Result result)
      	{
      		this.buttonPanel = buttonPanel;
      		this.responsePanel = responsePanel;
      		this.result = result;
      
      		ButtonGroup option = new ButtonGroup();
      
      		option.add(jrbtn1);
      		option.add(jrbtn2);
      
      		jrbtn1.addItemListener(this);
      		jrbtn2.addItemListener(this);
      
      		add(jrbtn1);
      		add(jrbtn2);
      	}
      	public void itemStateChanged(ItemEvent e)
      	{
      		if(jrbtn1.isSelected())
      		{
      			statement = 1;//Indicate current selected jrbtn1
      			buttonPanel.resetValue();//Invoke method from ButtonPanel
      			buttonPanel.enableButton();//Invoke method from ButtonPanel
      			responsePanel.resetText();//Invoke method from ResponsePanel
      		}
      		if(jrbtn2.isSelected())
      		{
      			statement = 2;
      			buttonPanel.resetValue();
      			buttonPanel.enableButton();
      			responsePanel.resetText();
      		}
      		result.setStatement(statement);//Send current selected JRadioButton to Result
      	}
      }
      Result.java
      Code:
      public class Result
      {
      	private int countA = 0;
      	private int countB = 0;
      	private int statement;
      	private boolean statusA = false;
      	private boolean statusB = false;
      	private int[] arrayA = new int[2];
      	private int[] arrayB = new int[2];
      
      	public Result()
      	{
      
      	}
      	public void setStatement(int stm)
      	{
      		statement = stm;//Received value from QuestionPanel
      	}
      	public void setStatusA(boolean a)
      	{
      		statusA = a;//Received and indicate current click JButton is jbtnA from ButtonPanel
      	}
      	public void setStatusB(boolean b)
      	{
      		statusB = b;//Received and indicate current click JButton is jbtnB from ButtonPanel
      	}
      	public void resetToFalse()
      	{
      		//Reset JButton status back to false
      		statusA = false;
      		statusB = false;
      	}
      	public void sumTotal()
      	{
      		if(statement==1)
      		{
      			if(statusA == true)
      			{
      				int temp = arrayA[0];
      				arrayA[0] = temp + 1;
      				System.out.println("arrayA[0] is: " + arrayA[0]);
      			}
      			if(statusB ==true)
      			{
      				int temp = arrayA[1];
      				arrayA[1] = temp + 1;
      				System.out.println("arrayA[1] is: " + arrayA[1]);
      			}
      		}
      		if(statement==2)
      		{
      			if(statusA == true)
      			{
      				int temp = arrayB[0];
      				arrayB[0] = temp + 1;
      				System.out.println("arrayB[0] is: " + arrayB[0]);
      			}
      			if(statusB ==true)
      			{
      				int temp = arrayB[1];
      				arrayB[1] = temp + 1;
      				System.out.println("arrayB[1] is: " + arrayB[1]);
      			}
      		}
      	}
      	public void printReport()
      	{
      		System.out.print(arrayA[0] + " " + arrayA[1] + "\n");
      		System.out.print(arrayB[0] + " " + arrayB[1]);
      	}
      }
      MySurvey.java
      Code:
      import java.awt.*;
      import java.awt.event.*;
      import javax.swing.*;
      
      public class MySurvey extends JFrame implements ActionListener
      {
      	private ButtonPanel buttonPanel;
      	private QuestionPanel questionPanel;
      	private ResponsePanel responsePanel;
      	private CenterPanel centerPanel;
      	private Result result;
      	private JMenu menuFile = new JMenu("File");
      	private JMenu menuPrint = new JMenu("Print");
      	private JMenuBar menuBar = new JMenuBar();
      	private JMenuItem menuFileExit = new JMenuItem("Exit");
      	private JMenuItem menuPrintReport = new JMenuItem("Generate Report");
      
      	public MySurvey(String header)
      	{
      		menuFile.add(menuFileExit);
      		menuPrint.add(menuPrintReport);
      		menuFileExit.addActionListener(this);
      		menuPrintReport.addActionListener(this);
      
      		menuBar.add(menuFile);
      		menuBar.add(menuPrint);
      
      		this.setJMenuBar(menuBar);
      
      		result = new Result();
      		centerPanel = new CenterPanel();
      		buttonPanel = new ButtonPanel(centerPanel.getResponsePanel(),centerPanel.getResult());
      		questionPanel = new QuestionPanel(centerPanel.getButtonPanel(),centerPanel.getResponsePanel(),centerPanel.getResult());
      
      		add(questionPanel, BorderLayout.NORTH);
      		add(centerPanel, BorderLayout.CENTER);
      
      		getContentPane();
      	}
      	public void actionPerformed(ActionEvent e)
      	{
      		if(e.getSource()==menuFileExit)
      		{
      			System.exit(0);//Exit the system
      		}
      		if(e.getSource()==menuPrintReport)
      		{
      			[B]result.printReport();[/B]//Invoke printReport() method from Result
      		}
      	}
      }
      Driver.java
      Code:
      import javax.swing.*;
      
      public class Driver
      {
      	public static void main(String args[])
      	{
      		MySurvey survey = new MySurvey("Student Survey");
      		survey.setVisible(true);
      		survey.pack();
      		survey.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); [B]<= 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?][/B]
      	}
      }
      ]
      Last edited by xirowei; Apr 6 '08, 11:17 AM. Reason: Bytes.com got bug in showing Java code, hence i adding in comment to notice other user that the line of code that appear invalid actually is a line of correct code.

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        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

        Comment

        • xirowei
          New Member
          • Jun 2007
          • 17

          #5
          Originally posted by JosAH
          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.printRep ort(); to centerPanel.get Result().printR eport();

          Jos thank you very much.

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            Originally posted by xirowei
            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.printRep ort(); to centerPanel.get Result().printR eport();

            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

            Comment

            Working...