7 Replies - 462 Views - Last Post: 05 April 2013 - 06:42 PM Rate Topic: -----

#1 ballisticpendulum  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 06-March 13

Sum of Array Elements (String)

Posted 05 April 2013 - 04:45 PM

Hey, I have an array of strings that are numbers and I want to be able to add the numbers together then divide by how much numbers there are (average). But I do not know how I would do that because..well they are strings.

I have a 2D array of JLabels that reveal a number and a 1D array of the money values and 1D array of the possible number values that are left.

Everytime they get a number, the possibleNumber label
possNum.setIcon(null)
. So I want to get the values of ONLY what is left

So far, I know I need a for loop, but my issue is to get the numbers/elements added together and getting how much is left in the array. Can anyone point me into the right direction?

Is This A Good Question/Topic? 0
  • +

Replies To: Sum of Array Elements (String)

#2 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10662
  • View blog
  • Posts: 39,590
  • Joined: 27-December 08

Re: Sum of Array Elements (String)

Posted 05 April 2013 - 05:46 PM

You can parse the Strings to numeric values using Integer.parseInt(), Double.parseDouble(), etc., for the appropriate type.
Was This Post Helpful? 0
  • +
  • -

#3 ballisticpendulum  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 06-March 13

Re: Sum of Array Elements (String)

Posted 05 April 2013 - 06:07 PM

Thanks that got rid of an error, but now I get a Native Method error.

I did
int remaining = 0;
			
			
			for(int x = 0; x < money.length; x++)
			{
				remaining = Integer.parseInt(money[i]); //error is here
			}
			
			int avg = remaining / money.length;

Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10662
  • View blog
  • Posts: 39,590
  • Joined: 27-December 08

Re: Sum of Array Elements (String)

Posted 05 April 2013 - 06:09 PM

And the error is...?
Was This Post Helpful? 0
  • +
  • -

#5 ballisticpendulum  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 06-March 13

Re: Sum of Array Elements (String)

Posted 05 April 2013 - 06:23 PM

View Postmacosxnerd101, on 05 April 2013 - 06:09 PM, said:

And the error is...?


Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "0.01"
	at java.lang.NumberFormatException.forInputString(Unknown Source)
	at java.lang.Integer.parseInt(Unknown Source)
	at java.lang.Integer.parseInt(Unknown Source)
	at DoND.actionPerformed(DoND.java:273)


At line 273 is what I posted previously
Was This Post Helpful? 0
  • +
  • -

#6 Flukeshot  Icon User is offline

  • A little too OCD
  • member icon

Reputation: 417
  • View blog
  • Posts: 1,030
  • Joined: 14-November 12

Re: Sum of Array Elements (String)

Posted 05 April 2013 - 06:28 PM

Have you tried Double.parseDouble()? 0.01 is not an integer.
Was This Post Helpful? 0
  • +
  • -

#7 ballisticpendulum  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 06-March 13

Re: Sum of Array Elements (String)

Posted 05 April 2013 - 06:35 PM

View PostFlukeshot, on 05 April 2013 - 06:28 PM, said:

Have you tried Double.parseDouble()? 0.01 is not an integer.


Oh now the error is gone, but I still don't know how to add the string values in the array together.

I tried using
remaining += Double.parseDouble(money[i]); //error on this line
int avg = remaining / money.length;


But it seems like it is not correct because now there is an error for that line
Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: empty String
	at sun.misc.FloatingDecimal.readJavaFormatString(Unknown Source)
	at java.lang.Double.parseDouble(Unknown Source)
	at DoND.actionPerformed(d.java:279)
	at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)

Was This Post Helpful? 0
  • +
  • -

#8 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10662
  • View blog
  • Posts: 39,590
  • Joined: 27-December 08

Re: Sum of Array Elements (String)

Posted 05 April 2013 - 06:42 PM

The error message is pretty self-explanatory. Your String is empty, so it's not a valid number, meaning you can't parse it.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1