• Post Reply Bookmark Topic Watch Topic
  • New Topic

Converting Letters to a phone number  RSS feed

 
Austin Hilten
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I had to write a program for class using the method definition "public static char getNumber(char upperCaseLetter)" It compiles and runs but wont print out my final answer. Any help would be great.






 
Junilu Lacar
Sheriff
Posts: 11490
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch!

Please UseCodeTags (←click and read) when posting code. It's easier to read. I've done it for you this time.

Are you sure you're returning the value you think you're returning? Notice your if-statements use literal char values. Are you returning a literal char value? Does return 1 return the char for the digit 1 or a char with a value of 1? See what this does:
 
Austin Hilten
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Basically I have to input a phone number: 1800ABCDEFG and output its corresponding numbers: 18002223334
 
Steve Fahlbusch
Bartender
Posts: 612
7
Mac OS X Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greetings,

Basically I have to input a phone number: 1800ABCDEFG and output its corresponding numbers: 18002223334


Basically i think we know this, please take a few minutes to answer Junilu's questions...... you may find that, that may help correct some of your code.

-steve
 
Austin Hilten
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry im super confused. I dont want the java assigned value of A(65) but the value I assigned to the letter A(2). The program compiles fine but wont print out the final output. All together this has confused me because I am used to seeing only syntax errors, not logic errors.
 
Paul Clapham
Sheriff
Posts: 22828
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Austin Hilten wrote:The program compiles fine but wont print out the final output.


So what does it do instead? Print nothing? Throw an exception? Print something else? It's hard to fix a problem when you aren't told what it is.

All together this has confused me because I am used to seeing only syntax errors, not logic errors.


So then you're making progress. Having a non-trivial piece of code work correctly the first time is always a pleasant surprise, even for professional programmers, and it's not surprising if it doesn't work correctly right away.
 
Junilu Lacar
Sheriff
Posts: 11490
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Austin Hilten wrote:Sorry im super confused. I dont want the java assigned value of A(65) but the value I assigned to the letter A(2).

Austin, that's ok. The way things work around here is we give you hints and tips. We try to avoid spoonfeeding the answer to you. We believe that if we help you find the answer yourself, the learning experience will be much better for you.

What's happening, and what you're not understanding, is Java type conversion (← see the first link in these search results). Specifically, you're seeing a narrowing conversion. You need to understand the type of the literals 65 (from my example) and 1, 2, 3, 4, etc (which you specify as return values in your code). Then you need to understand what happens when these literals undergo narrowing conversions to another type. The code that I gave you was an example of this kind of conversion -- line 2 forces a conversion by casting. If you understand this, and understand what you're actually returning (they're not what your now think they are), then you'll understand why you appear to not be getting any output.

BTW, you are in fact getting output; it's just that the values that you're outputting are not printable.

Edit: Here's more information about Java literals that you might want to check out. Pay close attention to what it says about the types that you're using in your program.
 
Junilu Lacar
Sheriff
Posts: 11490
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another hint for you -- study the ASCII table. Look at the first link in these search results and pay special attention to the "Dec" columns (Decimal values) and the "Char/Chr" (Character values). Look at the entries for the letters and numbers under the Char/Chr column and notice what their Decimal values are. Now try to figure out what your code is actually returning.

If you're working in Windows and you run your program and input "PRAISES", you might even hear your program make some sounds (make sure your speakers are not muted and volume settings are up), and if you count them carefully, you should expect four of them.

Edit: I tried it on my Mac and apparently Java is too fast so you may not be able to hear four distinct sounds after all; you should hear a sound though by inputting "PRAISES" to your program.
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think this is the table Junliu was referring to.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!