• Post Reply Bookmark Topic Watch Topic
  • New Topic

"-" symbol wont print from "for-loop"  RSS feed

 
Austin Hilten
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On line 45 i am trying to add "-" symbol to my output phone number. As of now the out prints like "1800*45*3569377. I want it to print 1800-3569377, or even more ideal: 1-800-356-9377. When printing normally (system.out.println) I can print symbols, but when I try to print from a for-loop it says "unclosed character literal."
 
K. Tsang
Bartender
Posts: 3648
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Line 45 is inside getDigit. "-" is a string not an int. Changing the return type to string will accomplish this.

By the way, the getDigit() method missed the checking on 'Y'

Given your logic to the program, if I input "1800UGOJAVA", the output will be 180085282

For phone numbers, why don't you use regular expression to check if it's valid?
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Austin,

IMO, you went the wrong way in changing the return type of getNumber to int instead of char as you had it before. The code would be more straightforward and you wouldn't be asking the question you're asking now if understood the difference between the literals you have in these lines of code that you wrote:

Also, I don't understand what you're trying to do with the last line that says return '45'. I don't think that even compiles, does it?
 
Austin Hilten
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for noticing the 'Y'! How would i go about changing the return type? My teachers requirement when writing the program has thrown me off (must use for-loop, and must use method header: public static char getNumber(char upperCaseLetter))
 
Austin Hilten
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu, the 45 is my attempt at putting a "-" in my printed output. My printed output without the 45 is 18003569377. My teacher is requiring me to have my printed output look like 1-800-356-9377.
 
Junilu Lacar
Sheriff
Posts: 11477
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:How would i go about changing the return type? My teachers requirement when writing the program has thrown me off (must use for-loop, and must use method header: public static char getNumber(char upperCaseLetter)

Use of for-loop. Check.

Use of method header: public static char getNumber(char upperCaseLetter). No check.

You already had the method signature right before. The only thing you needed to do was reconcile your understanding of what you were returning with what Java interprets from the way your wrote the literals being returned. In the other thread you started, I gave you a few links to documentation that will help you clear up your confusion. Did you go and read those? What did you understand from them or what has you still confused?

Let's try this one more time. Point out the two literal values you have in the following code:
 
Austin Hilten
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes I read them but my conceptual grasp of computer languages isnt very high and I constantly find myself being confused by concepts that should be easy. I wish i could tell you what I am confused about. But from my understanding the string literals they are the '0' and the 0?
 
Junilu Lacar
Sheriff
Posts: 11477
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:But from my understanding the string literals they are the '0' and the 0?

Yes! That's right.

Now, what is the type of '0'?

What is the type of 0?

Hint: They have different types. And they are NOT string literals. A String literal is if you write something like "1-800-JAVA-RKS" or "This is literally a String"
 
Austin Hilten
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
'0' is a string. 0 is an int?
 
Junilu Lacar
Sheriff
Posts: 11477
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:'0' is a string

No, a String literal is surrounded by double quotes (")

0 is an int?

Yes!
 
Austin Hilten
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry '0' = char literal?
 
Junilu Lacar
Sheriff
Posts: 11477
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:'0' = char literal?

Yes!

Now, what do you think this will print out, true or false?

Try it and see if you guessed correctly.
 
Austin Hilten
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
False because an integer cant equal a char
 
Junilu Lacar
Sheriff
Posts: 11477
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:False because an integer cant equal a char

Ok then, what about this, do you think this should display "false" as well?

Try it.
 
Austin Hilten
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
True because 48 on the ascii table corresponds to 0
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, then given that you understand that '0' char is the same as int 48, and given that this is because of a conversion from one type to another, if you have code like this:

what kind of conversion happens between the literal you're returning (an int, right?) and the declared return value type in the method signature (char)? Should you expect to see a char '0' return by getNumber or something else? If something else, which char value did you get by returning int 0?
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Finally, if you declare a char return type, what type of literal value should you return? Are you doing that in your code now?
 
Austin Hilten
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think I understand exactly what youre saying. Ill apply to my code
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great. Looking forward to seeing you get it working.
 
Austin Hilten
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It worked! You are the first person to help me understand the concept of literals. Thank you so much!
 
Junilu Lacar
Sheriff
Posts: 11477
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:True because 48 on the ascii table corresponds to 0

Java is a stickler for precision. To be precise here, 48 corresponds to '0' in the ASCII table (not 0).

I've told this story before but this is kind of what happened with you and Java: An American who was learning Chinese decided to try out his new language skills at a Chinese restaurant. He decided to order some soup so he asked the waitress in the best Chinese he could muster "How much for one bowl?" and he promptly got slapped. As he watched the waitress storm off, the man at the next table told him, "She was insulted that you asked her 'How much for one night?'"

He had used the wrong tone for the Chinese word "wan" and ended up saying "night" instead of "bowl".

 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One last note before we put this to rest: In the other thread, I told you that your program would make some sounds if you gave it the input of "PRAISES". That's because P, R, and S would cause the previous version of your program to return 7 (int), which is the BEL character. When you print it out, the BEL character doesn't show anything on the console but it does make a sound that's supposed to be a bell. It actually sounds more like a beep to me but anyway, that's what happens.
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Austin, I'm curious about one thing: what did you return when the character passed to getNumber() was not a letter?

You might want to discuss with your professor the appropriateness of the name "upperCaseLetter" in the method signature. IMO, this name does not fit well with its usage and is misleading because the value passed in may not necessarily be an uppercase letter: it could be a '-' or '(' or ')' or even just a plain ' ' (space character). By using the name upperCaseLetter, the code you wrote is conceptually misleading and doesn't tell the "story" of the getNumber() method quite the way it should be told.

This is how I would explain it in plain English: Given some character ??? (pick a better name) passed to the getNumber method, if ??? is 'A' or 'B' or ... then translate it to '1', otherwise if ??? is 'D' or 'E' or ... then translate it to '2', yada yada yada, and if it hasn't been translated after all that, then just return the same character ??? that was passed in.

If this is to make any sense at all in English, then "upperCaseLetter" would not be a good choice to replace ???, right?
 
s prajay
Greenhorn
Posts: 2
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have slightly modified your code,now it is printing as expected.Please check whether it is helpful to you.


public static char getNumber(char uppercaseLetter){

if (uppercaseLetter == 'A' || uppercaseLetter == 'B' || uppercaseLetter == 'C')
return '2';
else if (uppercaseLetter == 'D' || uppercaseLetter == 'E' || uppercaseLetter == 'F')
return '3';
else if (uppercaseLetter == 'G' || uppercaseLetter == 'H' || uppercaseLetter == 'I')
return '4';
else if (uppercaseLetter == 'J' || uppercaseLetter == 'K' || uppercaseLetter == 'L')
return '5';
else if (uppercaseLetter == 'M' || uppercaseLetter == 'N' || uppercaseLetter == 'O')
return '6';
else if (uppercaseLetter == 'P' || uppercaseLetter == 'Q' || uppercaseLetter == 'R' || uppercaseLetter == 'S')
return '7';
else if (uppercaseLetter == 'T' || uppercaseLetter == 'U' || uppercaseLetter == 'V')
return '8';
else if (uppercaseLetter == 'W' || uppercaseLetter == 'X' || uppercaseLetter == 'Y' || uppercaseLetter == 'Z')
return '9';
else if (uppercaseLetter == '0')
return '0';
else if (uppercaseLetter == '1')
return '1';
else
return '-';
}
 
Carey Brown
Saloon Keeper
Posts: 3310
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The same, only different...

 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
s prajay wrote:I have slightly modified your code,now it is printing as expected.Please check whether it is helpful to you.

Carey Brown wrote:The same, only different...

Nice gesture, guys, but your programs still don't work very well. What about when the input includes spaces or ( ), as in "1(800) SPO-ILER"? You might argue that the OPs instructions didn't cover these cases but that's a cop out, IMO. Also, you're OK this time but in the future, please be careful not to give away spoilers like this until the OP has had a chance to figure out a solution for themselves.

Edit: For example, if you give these programs an input of "(888)-234-DASH", you'll get "----------3274". That doesn't look right, does it?
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!