• Post Reply Bookmark Topic Watch Topic
  • New Topic

String method copyValueOf(char[])  RSS feed

 
Rubbal Bhusri
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have copied this code from some website into my eclipse IDE, the website has shown correct output using same code, but my eclipse IDE found errors in lines where it called the function copyValueOf(char[]) method saying that the method is undefined for the class I created. The code is :

import java.util.*;
public class Test {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
char[] Str1 = {'T','h','i','s', 'i','s', 'r','e','a','l','l','y', 'n','o','t', 'i','m','m','u','t','a','b','l','e', '!','!'};
String Str2;
Str2 = copyValueOf(Str1);
System.out.println("Returned String " + Str2);
Str2 = copyValueOf( Str1, 5, 10 );
System.out.println("Returned String " + Str2);
}

}

Please help me in removing the error.
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What the error is telling you is that the JDK expects your Test class does not have a copyValueOf(char[]) method
1) Either provide one
2) OR go back to where you copied the code from and see if there are any errors in your version of the code
Hint: The copyValueOf(char[]) method is a method for an existing java class
 
Rubbal Bhusri
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Isn't this an inbuilt method of String class ?
Do we have to define such inbuilt methods too ?

I shall be thankful , if you could solve it for me, because I didn't get your point.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, it's an inbuilt method of the String class. But you aren't calling it on the String class. In fact, you aren't calling it on any class at all. So the compiler is looking for it in the current class, which is Test.

Do you remember how to call static methods in other classes?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please start using code tags...after 60 posts, you should have an idea how to do it, but if not, read this: UseCodeTags.

Here is your code, properly formatted with them. See how much easier it is to read?

 
Rubbal Bhusri
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got your point, since copyOfValue() is a static method of String class, therefore I have to invoke it like this :
String.copyOfValue(char[]);
I corrected this code as follows :




But one more thing, I have to place spaces while assigning value to Str1. Otherwise the output (which is returned as a string) came as follows :
Thisisreallynotimmutabe!!

I mean, isn't there any way to get output in proper format, including spaces.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure I get your drift, but are you really saying that you want the program to guess where you want spaces and insert them for you? How are you expecting that to happen?
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rubbal Bhusri wrote:I corrected this code as follows :

And I've corrected it too.

Please DontWriteLongLines. It makes your thread very hard to read. I've broken yours (and Fred's) up this time, but for future reference, please remember:
80 characters max.

Thanks.

Winston
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rubbal Bhusri wrote:I mean, isn't there any way to get output in proper format, including spaces.

Yes, but as Matthew says, you have to put them there.

Simplest to me - if you really need it in a char[] - would be:
char[] str1 = "This is really not immutable!!".toCharArray();

And BTW, it should be "str1", not "Str1".

Winston
 
Rubbal Bhusri
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
char[] str1 = "This is really not immutable!!".toCharArray();

I didn't know about this way of invoking function, like you did above ( .toCharArray()).
I read few books but never saw such type of invocation.
Thank you for doing it in a different way.
 
Rubbal Bhusri
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Brown wrote:I'm not sure I get your drift, but are you really saying that you want the program to guess where you want spaces and insert them for you? How are you expecting that to happen?


no-no, not really.
But if it can do so, the program becomes so magical.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!