• Post Reply Bookmark Topic Watch Topic
  • New Topic

Method returning String[] from String in Java  RSS feed

 
Frank Poupard
Ranch Hand
Posts: 65
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, as there is not equivalent to "explode" method in Java I wrote the following and get some errors in line 8 : "The method explode(String) is undefined for the type
Test".
I don't really get it...Here is the little code. Thanks for your help !

 
Tony Docherty
Bartender
Posts: 3268
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are calling an instance method from a static context.
You can either make the method static or create an instance of the Test class and call the explode method via that.
 
Liutauras Vilda
Marshal
Posts: 4650
318
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome. Probably you have in mind PHP function. However, even in PHP that function works slightly different I think, you need to explode it with some kind of delimiter. Single word I don't think you can, but I don't use PHP, I can be wrong.

In Java equivalent is split method.

So you'd get s1 ["Java", "Programming", "Language"]
 
Junilu Lacar
Sheriff
Posts: 11155
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since you are trying to call explode from a static context (the main() method is static) your explode method also needs to be static. The way you have it now, explode is an instance method which means it can only be invoked on an instance of test, like this:

Also, it should be source.length, not source.length() - I was thinking array.length, just realized this is String.length().
 
Frank Poupard
Ranch Hand
Posts: 65
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony Docherty wrote:You are calling an instance method from a static context.
You can either make the method static or create an instance of the Test class and call the explode method via that.


Thanks, so I rewrote the main methode like this :

But it doest not change anything...
 
Ajinkya Ghonge
Greenhorn
Posts: 8
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Frank Poupard wrote:
Tony Docherty wrote:You are calling an instance method from a static context.
You can either make the method static or create an instance of the Test class and call the explode method via that.


Thanks, so I rewrote the main methode like this :

But it doest not change anything...


Hi Frank, your explode method has two return types void and String[]. That's why the compiler is giving error. remove the void and it will work fine
 
Frank Poupard
Ranch Hand
Posts: 65
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just realised I had to delete "void" in my explode method, my bad...
 
Frank Poupard
Ranch Hand
Posts: 65
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ajinkya Ghonge wrote:. . . Hi Frank, your explode method has two return types void and String[]. That's why the compiler is giving error. remove the void and it will work fine


Thanks, I've juste seen your post aftewards
 
Liutauras Vilda
Marshal
Posts: 4650
318
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd suggest you to change method name to something different. It confused me, because I assumed you had in mind PHP function explode, which is very similar to Java split.
Your explode method does slightly different thing. That could confuse your reader who ever had something to do with PHP and might other languages.
 
Frank Poupard
Ranch Hand
Posts: 65
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are right, I should have renamed it.
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Frank Poupard wrote:I just realised I had to delete "void" in my explode method, my bad...

Actually, presuming that you want to split a String into its constituent characters (and assuming that they don't include "surrogate pairs"), there IS a way to do it:

  whatever.split("(?<=.)")

but you have to know a little bit about regular expressions first.

Winston
 
Junilu Lacar
Sheriff
Posts: 11155
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's also String.toCharArray()
 
Campbell Ritchie
Marshal
Posts: 55759
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is usually unnecessary to quote the whole of an old post; I have removed most of one quote because it simply makes the thread longer.

When you said return String[], I thought you wanted this method rather than toCharArray.
 
Junilu Lacar
Sheriff
Posts: 11155
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:When you said return String[], I thought you wanted this method rather than toCharArray.

As is often the case even with business users, what they say they want is different from what they actually want. The method return type declaration says he wants a String[] but the method implementation strongly suggests that he actually needs an array of single characters.
 
Campbell Ritchie
Marshal
Posts: 55759
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Obviously they've taken lessons from Humpty Dumpty.
 
Frank Poupard
Ranch Hand
Posts: 65
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, it makes part of a program intending to split one string into multiple parts according to separators...Regex forbidden here ;)
 
Liutauras Vilda
Marshal
Posts: 4650
318
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Frank Poupard wrote:Actually, it makes part of a program intending to split one string into multiple parts according to separators...Regex forbidden here ;)
But that is different from what you are doing currently. What you're doing, is taking a string and putting its individual characters to an array.

What you most likely want to do is to get an effect of the method split, which is splitting string by certain delimiter. Please clarify what you exactly trying to do here, so we wouldn't mislead you.
See my first post, is it what you want to do, just with your own written method?
 
Campbell Ritchie
Marshal
Posts: 55759
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:. . . the method split, which is splitting string by certain delimiter. . . .
Unfortunately OP has been told not to use a regex, and split splits on a regex.

Nothing like making assignments easy, is there?
 
Liutauras Vilda
Marshal
Posts: 4650
318
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Unfortunately OP has been told not to use a regex

I got that, but I think OP's most recent post means, that he needs to mimic split method's behavior with his own implemented.
... program intending to split one string into multiple parts according to separators

Currently what he's doing is just adding singular string characters to an array, but that is it. I'm not certainly sure what the actual task is.
 
Campbell Ritchie
Marshal
Posts: 55759
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From his other post, it would appear it is to split an input String into an array of one‑character Strings. Not difficult, but we need to know what is really required. If the length of the array is equal to the length() of the input text, then there is no need for a delimiter to separate the characters.
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Frank Poupard wrote:Actually, it makes part of a program intending to split one string into multiple parts according to separators...Regex forbidden here ;)

???
Why?

This sounds like either:
(a) A classroom exercise.   or
(b) A PHB at work.

because if neither of the above is true - and actually, in the latter case, even if it's true - the restriction is ridiculous.
Let me say that once again for the cheap seats ... RIDICULOUS.

Winston
 
Junilu Lacar
Sheriff
Posts: 11155
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As a classroom exercise, it's not too bad. I would guess that the instructor didn't bother to give any context to the problem though and simply said, "You're not allowed to use regular expressions to solve this problem!" If that were the case, it's deplorable but not surprising. I would at least say something like "You can easily do this with a regular expression but I want you to challenge yourselves and see if you can use only what you know about loops, the String class and its methods, arrays, and conditional statements to solve this."

If this were for work, then yes, it's ridiculous. In fact, it would be ridiculously absurd.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!