Results 1 to 5 of 5
  1. #1
    mallorz is offline Member
    Join Date
    Jan 2014
    Posts
    65
    Rep Power
    0

    Default Calculating values & passing from two-dimensional array. What am I doing wrong

    Long story short: The program takes user values (temperature) and converts them to the opposite (C >> F / F >> C)

    I originally started this program with three separate arrays but then decided that it would be a good opportunity to use a two-dimensional array and one other.

    The two-dimensional array has 2 rows, 10 columns. The second is a normal String array ...
    Java Code:
    	String[][] myTemperatures = new String[2][9];
    	String inputAssembly[] = new String[9];
    I prompt the user for to enter temperature values, using a GUI and jbutton to distinguish F/C. Each time the user clicks 'continue', the values are stored into the two-dimensional array. One row holds the temperature, the other holds the C or F designation.
    Java Code:
    	// CONTINUE BUTTON CLICK ACTIONS
    	class ContinueButtonListener implements ActionListener{
    		
    		@Override
    		public void actionPerformed(ActionEvent arg0) {
    			// TODO Auto-generated method stub
    
    			input = view.getTempValue();
    				
    			if (input !=null){
    				myTemperatures[0][btnClick] = input;
    				myTemperatures[1][btnClick] = view.getDesignator();
    
    				JOptionPane.showMessageDialog(view, "Got it!");
    
    				btnClick++;
    				view.clearTextbox();
    			}
    			else {
    				view.clearTextbox();
    			}
    			
    			if (btnClick == 9){
    				view.disableButton(); // disable CONTINUE button
    			}
    		} // End of actions
    This is where I am experiencing the trouble and I cannot seem to get the Debug to work properly here. When the two-dimensional array is full OR the user clicks 'calculate' instead of 'continue', the Calculate event is performed via an ActionListener.

    Java Code:
    	// CALCULATE BUTTON CLICK ACTIONS
    	class CalculateButtonListener implements ActionListener{
    
    		@Override
    		public void actionPerformed(ActionEvent arg0) {
    			// TODO Auto-generated method stub
    			String hold;
    			Double temp;
    			StringBuilder sb = new StringBuilder();
    			
    			for (int i = 0; i < myTemperatures.length; i++) {	// Row
    				for (int y = 0; y < myTemperatures[i].length; y++) {	// Column
    					if (myTemperatures[i][y] != null) {	// If not NULL
    						if (myTemperatures[i][y].contains("F")) { 		// Convert to C
    							hold = sb.append(myTemperatures[i]).toString();
    						
    							temp = (double) (5 * (Integer.parseInt(hold) - 32) / 9); // Convert to C
    							hold = temp + "C"; // Hold temp value + designator for inputAssembly[]
    							
    							inputAssembly[i] = myTemperatures[i][y] + " is " + hold + "./n";
    						} // End of 'Convert to C' 
    					else {		// Convert to F
    						hold = sb.append(myTemperatures[i]).toString();
    						
    						temp = (double) (9 * (Integer.parseInt(hold) / 5 + 32)); // Convert to F
    						hold = temp + "F";	// Hold temp value + designator for inputAssembly[]
    						
    						inputAssembly[i] = myTemperatures[i][y] + " is " + hold + "./n";
    						} // End of 'Convert to F'
    				//	for (int z = 0; z < inputAssembly.length; z++) {	// Iterate through inputAssembly[]
    						// Assign 'hold' variable to inputAssembly container
    				//		inputAssembly[z] = myTemperatures[i][y] + " is " + hold + "./n";
    				//		} // End of InputAssembly[] FOR loop
    					}
    					else { // If myTemperatures[i][y] is NULL, fill inputAssembly[x] with NULL
    						inputAssembly[i] = null;
    							}
    						} 
    					}  
    			
    			// For testing only
    			view.displayResults(inputAssembly);
    			view.displayMessages("Thanks for playing!");
    			
    		} // End of actions
    		
    	} // End of inner class -- Calculate button
    And I get a ton of errrors ...

    Java Code:
    Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "[Ljava.lang.String;@7441b1fd"
    	at java.lang.NumberFormatException.forInputString(Unknown Source)
    	at java.lang.Integer.parseInt(Unknown Source)
    	at java.lang.Integer.parseInt(Unknown Source)
    	at witc.edu.Ch12_16_17ex1.controller.GUI_Controller$CalculateButtonListener.actionPerformed(GUI_Controller.java:92)
    	at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    	at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    	at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    	at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    	at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    	at java.awt.Component.processMouseEvent(Unknown Source)
    	at javax.swing.JComponent.processMouseEvent(Unknown Source)
    	at java.awt.Component.processEvent(Unknown Source)
    	at java.awt.Container.processEvent(Unknown Source)
    	at java.awt.Component.dispatchEventImpl(Unknown Source)
    	at java.awt.Container.dispatchEventImpl(Unknown Source)
    	at java.awt.Component.dispatchEvent(Unknown Source)
    	at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    	at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    	at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    	at java.awt.Container.dispatchEventImpl(Unknown Source)
    	at java.awt.Window.dispatchEventImpl(Unknown Source)
    	at java.awt.Component.dispatchEvent(Unknown Source)
    	at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    	at java.awt.EventQueue.access$200(Unknown Source)
    	at java.awt.EventQueue$3.run(Unknown Source)
    	at java.awt.EventQueue$3.run(Unknown Source)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    	at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    	at java.awt.EventQueue$4.run(Unknown Source)
    	at java.awt.EventQueue$4.run(Unknown Source)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    	at java.awt.EventQueue.dispatchEvent(Unknown Source)
    	at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    	at java.awt.EventDispatchThread.run(Unknown Source)

    I imagine the issue lies within how I am handling the two-dimensional array in the CALCULATE event and/or converting the String[][] to String then parsing to an Integer.

    What are your thoughts? Would this be better done is separate arrays (not using one two-dimensional, but storing 34C, 45F ... in one. I think this would be difficult for me to parse for conversions). I've been working on this for a couple nights now and will be diving into this again tomorrow night and hopefully working past it. And help, guidance, and the like is appreciated!

  2. #2
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,392
    Rep Power
    5

    Default Re: Calculating values & passing from two-dimensional array. What am I doing wrong

    Well, I haven't run this but the following looks suspicious.

    Java Code:
    hold = sb.append(myTemperatures[i]).toString();
    You keep appending something to sb then passing it to "hold." Then you try parsing it as an integer. I suggest you sprinkle some print statements around your code to make certain things are what they seem. Doing that is one of the easiest debugging techniques there is.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    mallorz is offline Member
    Join Date
    Jan 2014
    Posts
    65
    Rep Power
    0

    Default Re: Calculating values & passing from two-dimensional array. What am I doing wrong

    Jim,

    You are correct.

    IN CALCULATE EVENT
    hold Printed: [Ljava.lang.String;@77435978

    This shows the values correctly:
    Java Code:
    			if (input !=null){
    				myTemperatures[0][btnClick] = input;
    				System.out.println(myTemperatures[0][btnClick]);
    				myTemperatures[1][btnClick] = view.getDesignator();
    				System.out.println(myTemperatures[1][btnClick] );
    Java Code:
    			if (btnClick == 9){
    System.out.println("begin test");				
    for (int x=0;x<myTemperatures.length;x++){
    					for (int y = 0; y < myTemperatures[x].length; y++){
    					System.out.println("printed");
    					System.out.println(myTemperatures[x][y]);
    				}}
    				view.disableButton(); // disable CONTINUE button
    			}
    Prints:
    XML Code:
    begin test
    54
    54
    24
    65
    35
    0
    78
    23
    54
    F
    F
    F
    C
    C
    C
    C
    F
    F
    Looks like my approach is all wrong. Could you answer the following?

    1. Did I create the two-dimensional array right? This is obviously the root of my problem. How do I access it correctly? Should split this instead into two separate arrays, or would you have created a two-dimensional array for this as well?

    2. How would I parse out the first row (temp values) for calculations? They need to be converted somewhere. This code may actually be semi-correct, hard to say now that I am very confused with this two-dimensional array thing!

    Any insight on how I can get this to work is appreciated.
    Last edited by mallorz; 01-29-2014 at 09:56 PM.

  4. #4
    mallorz is offline Member
    Join Date
    Jan 2014
    Posts
    65
    Rep Power
    0

    Default Re: Calculating values & passing from two-dimensional array. What am I doing wrong

    Possible 'AH-HA!' moment ..

    Java Code:
         inputAssembly[i] = myTemperatures[i][y] + " is " + hold + "./n";
    Is that the issue? Maybe the inputAssembly array doesn't believe it has the space? My intent was to join [i][y] into one.

    For example ...

    inputAssembly[i] = myTemperatures[107][C] is (F value).

  5. #5
    mallorz is offline Member
    Join Date
    Jan 2014
    Posts
    65
    Rep Power
    0

    Default Re: Calculating values & passing from two-dimensional array. What am I doing wrong

    FYI - I split them into three separate arrays. Consider this solved.

Similar Threads

  1. fibonacci iterative - calculating wrong
    By Gatsu in forum Advanced Java
    Replies: 1
    Last Post: 05-20-2013, 08:19 PM
  2. Need help calculating totals from values in a 2D array
    By scottdaking in forum New To Java
    Replies: 4
    Last Post: 03-02-2013, 08:22 PM
  3. Replies: 7
    Last Post: 02-04-2013, 06:27 PM
  4. Calculating values from a jtable column
    By victaino in forum New To Java
    Replies: 37
    Last Post: 05-24-2012, 11:16 AM
  5. calculating with values out of another class
    By senca in forum New To Java
    Replies: 3
    Last Post: 04-08-2010, 03:01 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •