• Post Reply Bookmark Topic Watch Topic
  • New Topic

Cannot Find Symbol  RSS feed

 
Ray Anderson
Ranch Hand
Posts: 98
Java Oracle Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Code Aim: Given a string, return a new string where the last 3 chars are now in upper case. If the string has less than 3 chars, uppercase whatever is there. Note that str.toUpperCase() returns the uppercase version of a string.



My following code:



Gives the error:

EndUp.java:12: error: cannot find symbol
System.out.println(result.toUpperCase());
^
symbol: method toUpperCase()
location: variable result of type StringBuilder
1 error
 
Tapas Chand
Ranch Hand
Posts: 614
9
BSD Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
StringBuilder class does not have a toUpperCase() method. As simple as that.
 
Knute Snortum
Sheriff
Posts: 4288
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But it does have a toString() method. How would you use that to get what you want?
 
Ray Anderson
Ranch Hand
Posts: 98
Java Oracle Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Override toString() will do i guess.
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you will find StringBuilder is a final class, so you cannot override any methods.
No, overriding is not the way to go.
 
Ray Anderson
Ranch Hand
Posts: 98
Java Oracle Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:I think you will find StringBuilder is a final class, so you cannot override any methods.
No, overriding is not the way to go.


I have removed StringBuilder class and ran the code below and it works,

 
Ray Anderson
Ranch Hand
Posts: 98
Java Oracle Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And one more question how do i find out how much memory my program takes at run time ? I am using Windows 7 32 bit.

when i run task manager i think under process tab it only shows the command prompt usage in RAM.
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, that code looks as if it will work
But there are all sorts of stylistic improvements you can make to it.
 
Ray Anderson
Ranch Hand
Posts: 98
Java Oracle Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok i'll see what I can do
 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that your method doesn't do what you told us it should do. It doesn't return the string modified as you described. It returns the same string as passed as the method parameter.
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Damn! I should learn to read the post properly. I was mistaken there
 
Ray Anderson
Ranch Hand
Posts: 98
Java Oracle Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paweł Baczyński wrote:Note that your method doesn't do what you told us it should do. It doesn't return the string modified as you described. It returns the same string as passed as the method parameter.


No, I am getting what I expect as answer no matter whatever string I send as argument.

Please give me a string I will verify with answer.
 
Tapas Chand
Ranch Hand
Posts: 614
9
BSD Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
RahulRise Das wrote:No, I am getting what I expect as answer no matter whatever string I send as argument.

You did not get what Pawel is saying.
if you call your method like
The result will be "anyString". But you are expecting "anyStrING".
So your latest code is not generating the expected output.
 
Ray Anderson
Ranch Hand
Posts: 98
Java Oracle Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you talking about this code:
This is my original code (which is wrong)



And if your talking about this code:



This code gives correct answer.

I am not allowed post a screenshot in this forum, is there any way to show my result ?
 
Ray Anderson
Ranch Hand
Posts: 98
Java Oracle Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I appreciate your concern guys, I am getting the correct answer, if not ? I like to help even if I am new in java I like to see what's wrong ?
 
Ray Anderson
Ranch Hand
Posts: 98
Java Oracle Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tapas and Pawel can you guys post your main method ?
 
Ray Anderson
Ranch Hand
Posts: 98
Java Oracle Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tapas Chand wrote:You did not get what Pawel is saying.


ok guys try this one:



I understand now what Pawel was saying.
 
Tapas Chand
Ranch Hand
Posts: 614
9
BSD Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bingo!!! Now you got it right.
Still there are few places for improvement.
1. There should be only 1 return statement.
2. and 3. You try to find. If you will not find them we will help.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tapas Chand wrote:
1. There should be only 1 return statement.

That is not necessarily true. It has been debated here many times.
 
Ray Anderson
Ranch Hand
Posts: 98
Java Oracle Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
considering two return statements :



Is this ok ?
 
Knute Snortum
Sheriff
Posts: 4288
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looks great! One thing, the class with main in it needs to be public and the other class should have default access, the opposite of what you have.
 
Ray Anderson
Ranch Hand
Posts: 98
Java Oracle Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:the class with main in it needs to be public and the other class should have default access, the opposite of what you have.




I get it.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!