Wrapper classes

  • (2 Pages)
  • +
  • 1
  • 2

20 Replies - 648 Views - Last Post: 10 March 2013 - 12:36 PM Rate Topic: -----

#16 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2194
  • View blog
  • Posts: 5,222
  • Joined: 10-September 10

Re: Wrapper classes

Posted 10 March 2013 - 03:43 AM

I don't see that error in what you've posted. What I do see is the use of type char that will not provide the results you're expecting. For example,
String numString = "12345";
int num = numString.charAt( 2 );

will not assign the value of 3 to num. It will instead assign the decimal ASCII value of the character '3' to num.

You might try something like:
int num1 = Integer.parseInt( "" + num.charAt(0) );
Was This Post Helpful? 1
  • +
  • -

#17 louisianapadawan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 28-February 13

Re: Wrapper classes

Posted 10 March 2013 - 11:41 AM

View PostGregBrannon, on 10 March 2013 - 03:43 AM, said:

I don't see that error in what you've posted. What I do see is the use of type char that will not provide the results you're expecting. For example,
String numString = "12345";
int num = numString.charAt( 2 );

will not assign the value of 3 to num. It will instead assign the decimal ASCII value of the character '3' to num.

You might try something like:
int num1 = Integer.parseInt( "" + num.charAt(0) );


Thank you, that works. But I don't understand why. How does adding the "" + inside the parenthesis get it to read it as an int? Just curious. I'm trying to understand what I'm doing and not just "copy code to get an assignment done." I understand the ASCII part but I don't get how the "" + helps convert it to an int.
Was This Post Helpful? 0
  • +
  • -

#18 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10183
  • View blog
  • Posts: 37,596
  • Joined: 27-December 08

Re: Wrapper classes

Posted 10 March 2013 - 11:56 AM

The Integer.parseInt() method accepts a String parameter. The String charAt(0) method returns a char, so the num.charAt(0) + "" concatenates the char with a String, to return a String. Thus, the Integer.parseInt() method now has a String parameter.

Alternatively, you could do:
int value = num.charAt(0) - '0';



Remember that chars have equivalent numerical values, and '0' (as a char) != 0 (as an int), so subtracting '0' from the char will return the appropriate int. So '2' - '0' == 2.
Was This Post Helpful? 1
  • +
  • -

#19 louisianapadawan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 28-February 13

Re: Wrapper classes

Posted 10 March 2013 - 12:15 PM

View Postmacosxnerd101, on 10 March 2013 - 11:56 AM, said:

The Integer.parseInt() method accepts a String parameter. The String charAt(0) method returns a char, so the num.charAt(0) + "" concatenates the char with a String, to return a String. Thus, the Integer.parseInt() method now has a String parameter.

Alternatively, you could do:
int value = num.charAt(0) - '0';



Remember that chars have equivalent numerical values, and '0' (as a char) != 0 (as an int), so subtracting '0' from the char will return the appropriate int. So '2' - '0' == 2.



So any char - 0 = the appropriate int ?
Was This Post Helpful? 0
  • +
  • -

#20 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10183
  • View blog
  • Posts: 37,596
  • Joined: 27-December 08

Re: Wrapper classes

Posted 10 March 2013 - 12:20 PM

If you have the chars '0' through '9' and you subtract '0', you will get the appropriate int. Be advised that '0' != 0, so quotes matter. The ASCII table will tell you the int value of '0', as a char.
Was This Post Helpful? 1
  • +
  • -

#21 louisianapadawan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 28-February 13

Re: Wrapper classes

Posted 10 March 2013 - 12:36 PM

Thanks mac and everyone else!
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2