11 Replies - 927 Views - Last Post: 08 April 2012 - 08:57 PM Rate Topic: -----

#1 Dansellbuddy  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 22
  • Joined: 08-April 12

charAt and ASCII equivelant printed from the same string

Posted 08 April 2012 - 08:37 AM

All,
I have a homework assignment that I'm having difficulties with. I read that it is a no-no to ask for someone to do the homework and I'm not looking for that. A little nudge in the right direction is what I'm looking for. Anyway, my problem is to take a variable, get it's first initial and the ASCII equivelant to print on the same line. Here is what I have so far: (for troubleshooting purposes, you should know that the variables already have values.)

char firstInitial = (first.charAt(0));		//This sets the value for the initial using the first name
char lastInitial = (last.charAt(0));		//This sets the value for the initial using the last name

System.out.println((char)firstInitial + '\t' + " " + ((int)firstInitial) );	//Trying to force the first part into a character
System.out.println((char)lastInitial + '\t' + " " + ((int) lastInitial) );	//Keep getting both variables as integers


When my code runs I get two ASCII values per line. The second ASCII value on the line is correct for my initial, the first ASCII value is nine values higher (ex. 109 100).

I asked my professor and he said I do not need the variables firstInitial and lastInitial. I should be able to get the value in the print statements. He said I also had too many parentheses around the ascii conversion of the first and last name initials...
So I modified my code to:

System.out.println("Inits ASCII Value");
System.out.println(first.charAt(0) + (int)first.charAt(0) );	//Trying to force the first part into a character
System.out.println(last.charAt(0) + (int)last.charAt(0) );	//Keep getting both variables as integers


Now my code is converting both variables to integers and adding the values. Any thoughts?

Is This A Good Question/Topic? 0
  • +

Replies To: charAt and ASCII equivelant printed from the same string

#2 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7621
  • View blog
  • Posts: 12,848
  • Joined: 19-March 11

Re: charAt and ASCII equivelant printed from the same string

Posted 08 April 2012 - 08:48 AM

System.out.println((char)lastInitial + '\t' + " " + ((int) lastInitial) );


A subtle bug, and complex. You've actually covered a lot of ground with this one! The key point is tha expressions evaluate in left to right order, and they don't know what's coming until they evaluate it.

What's happening here is this:

when you evaluate the expression

(char)lastInitial + '\t' + " " + ((int) lastInitial)


you begin by casting lastInitial to a char - it's already a char, so that's identity conversion, always works. Then you add to it the char value '\t', which happens to be 9. Since chars are basically ints, you now have lastInitial+9, which you now convert to a String by appending a String to it. Finally we append the int value of lastInitial.

Does that help?
Was This Post Helpful? 2
  • +
  • -

#3 Dansellbuddy  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 22
  • Joined: 08-April 12

Re: charAt and ASCII equivelant printed from the same string

Posted 08 April 2012 - 12:48 PM

View Postjon.kiparsky, on 08 April 2012 - 08:48 AM, said:

System.out.println((char)lastInitial + '\t' + " " + ((int) lastInitial) );


A subtle bug, and complex. You've actually covered a lot of ground with this one! The key point is tha expressions evaluate in left to right order, and they don't know what's coming until they evaluate it.

What's happening here is this:

when you evaluate the expression

(char)lastInitial + '\t' + " " + ((int) lastInitial)


you begin by casting lastInitial to a char - it's already a char, so that's identity conversion, always works. Then you add to it the char value '\t', which happens to be 9. Since chars are basically ints, you now have lastInitial+9, which you now convert to a String by appending a String to it. Finally we append the int value of lastInitial.

Does that help?

Was This Post Helpful? 0
  • +
  • -

#4 Dansellbuddy  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 22
  • Joined: 08-April 12

Re: charAt and ASCII equivelant printed from the same string

Posted 08 April 2012 - 01:03 PM

View Postjon.kiparsky, on 08 April 2012 - 08:48 AM, said:

System.out.println((char)lastInitial + '\t' + " " + ((int) lastInitial) );


A subtle bug, and complex. You've actually covered a lot of ground with this one! The key point is tha expressions evaluate in left to right order, and they don't know what's coming until they evaluate it.

What's happening here is this:

when you evaluate the expression

(char)lastInitial + '\t' + " " + ((int) lastInitial)


you begin by casting lastInitial to a char - it's already a char, so that's identity conversion, always works. Then you add to it the char value '\t', which happens to be 9. Since chars are basically ints, you now have lastInitial+9, which you now convert to a String by appending a String to it. Finally we append the int value of lastInitial.

Does that help?

Well yes and no. The /t was supposed to insert a tab. So it should help me understand why the ASCII was off by 9. That one really threw me for a loop. I'm not going to get a chance to try more until tonight. I've got a four hour drive ahead of me. I'll probably post again tonight when I get home.
Was This Post Helpful? 0
  • +
  • -

#5 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7621
  • View blog
  • Posts: 12,848
  • Joined: 19-March 11

Re: charAt and ASCII equivelant printed from the same string

Posted 08 April 2012 - 01:28 PM

Okay, so now you know what it's doing wrong: it's evaluating the first subexpression
(char)lastInitial + '\t'


as integer arithmetic. You want it to be String concatenation.

So to force an int to be a String, just cat it onto a String:
""+ (char)lastInitial + '\t' etc

Was This Post Helpful? 1
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: charAt and ASCII equivelant printed from the same string

Posted 08 April 2012 - 01:51 PM

lastInitial + "\t" etc


Will do
Was This Post Helpful? 1
  • +
  • -

#7 Dansellbuddy  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 22
  • Joined: 08-April 12

Re: charAt and ASCII equivelant printed from the same string

Posted 08 April 2012 - 08:06 PM

""+ (char)lastInitial + '\t' etc

This worked very well for me. I thought by casting it with (char) I would be setting at least that part as a String. Is there a way to do this without starting with a blank space? I'm just trying to understand this a little better. Thank you all very much for the help.
Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: charAt and ASCII equivelant printed from the same string

Posted 08 April 2012 - 08:19 PM

Sure that

""+ (char)lastInitial + '\t' etc

will work but it means, make String, add (cast useless why casting a char to a char ?) a char and another char

lastInitial + "\t" etc

means: at a char, append a String so convert to a String. Seems much clearer to me
But it is your choice

This post has been edited by pbl: 08 April 2012 - 08:25 PM

Was This Post Helpful? 0
  • +
  • -

#9 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7621
  • View blog
  • Posts: 12,848
  • Joined: 19-March 11

Re: charAt and ASCII equivelant printed from the same string

Posted 08 April 2012 - 08:20 PM

Quote

I thought by casting it with (char) I would be setting at least that part as a String.


A char is not a String, they're very different sorts of things. It's actually more like an int than like a String. It's a primitive, which means the variable points to a single actual value, while a String is an object: the String variable is simply the address an address of an object which has zero, one, or many "values", ie, fields, associated with it, in addition to methods.

Quote

Is there a way to do this without starting with a blank space?


Use the empty String: "", with no space in between. This is really the easiest way, and the accepted idiom. (that is, it's what everyone does, because it works)

Or you could use sort of roundabout route, like Character.toString(lastInitial), but that's just a little much. Probably something of that sort if being called behind the scenes, but you want to keep your code simple - leave that for the back end to handle.

EDIT: pbl's method is also good. As long as there's a String in the expression, + will be treated as String concatentation and not addition. But just remember that it's done on a by expression basis. So

'c'+'d'+"e"
will not get you what you want, because

'c'+'d'
will be evaluated first, which adds up to 199!

This post has been edited by jon.kiparsky: 08 April 2012 - 08:24 PM

Was This Post Helpful? 0
  • +
  • -

#10 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: charAt and ASCII equivelant printed from the same string

Posted 08 April 2012 - 08:20 PM

and less code to type
Was This Post Helpful? 0
  • +
  • -

#11 Dansellbuddy  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 22
  • Joined: 08-April 12

Re: charAt and ASCII equivelant printed from the same string

Posted 08 April 2012 - 08:54 PM

Thanks guys. As I'm sure you can tell I'm pretty new to this. I'm currently finishing week 3 of an 8 week course. I'm sure I'll be here a lot over the remaining weeks. Online courses tend to be tedious and don't provide the clarification that I often need to truly grasp the concept. Anyway, I'm glad I've found this site. I've been interested in learning programming (any language really) for years and it just so happens my degree plan brought me to JAVA. I hope to be motivated enough to continue learning after the course. You two have succeeded make a newcomer feel welcomed and your speedy responses are quite impressive.
Was This Post Helpful? 1
  • +
  • -

#12 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7621
  • View blog
  • Posts: 12,848
  • Joined: 19-March 11

Re: charAt and ASCII equivelant printed from the same string

Posted 08 April 2012 - 08:57 PM

Glad to be of assistance. Stick around and listen to pbl and you'll be know everything I know in no time.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1