you have a method that should return a String Object, yet is returning a StringTokenizer, which should give you a compile time error.
also, either your indentation of your rev method is bad, or you have one closing bracket to many.
last tip: don't have your main method throw any exceptions. the main method is the very last place where you can catch and gracefully handle exceptions.
stultuske
Posting Sensei
3,137 posts since Jan 2007
Reputation Points: 1,114
Solved Threads: 433
well ... writing something like that is actually very easy, and there are various ways to do so. what is your code right now? have you made it in a way that you can compile and run the code?
stultuske
Posting Sensei
3,137 posts since Jan 2007
Reputation Points: 1,114
Solved Threads: 433
well ... yes, but you have to actually extract the tokens to get that result.
try
StringTokenizer st;
st = new StringTokenizer(number, " -,.?!:;");
String a = "";
while ( st.hasMoreTokens())
a += st.nextToken();
System.out.println(a);
stultuske
Posting Sensei
3,137 posts since Jan 2007
Reputation Points: 1,114
Solved Threads: 433
as I said, there are various ways to do this. but if you want to do it like this, why add the:
for (int i = 0,j=0; i < s.length(); i++) {
//If we find a non-digit character
if (!Character.isDigit(s.charAt(i)))
{
//do nothing
}
part? you could have just as easy do it like this:
for (int i = 0,j=0; i < s.length(); i++) {
if (Character.isDigit(s.charAt(i)))
{
str[j]=s.charAt[i];
j++;
}
stultuske
Posting Sensei
3,137 posts since Jan 2007
Reputation Points: 1,114
Solved Threads: 433
as I said, there are various ways to do this. but if you want to do it like this, why add the:
part? you could have just as easy do it like this:
for (int i = 0,j=0; i < s.length(); i++) {
if (Character.isDigit(s.charAt(i)))
{
str[j]=s.charAt[i];
j++;
}
@stultuske Think you ment:
str[j] = "" + s.charAt(i);
reason for the ""+ is because you are adding a char to a string[] which if you use
str[j] = s.charAt(i);
will give you an error also charAt is a method and must have parenthesis i.e (). Also you forgot a closing curly brace in your for statement.
So the final code might be similar to:
for (int i = 0, j = -1; i < s.length(); i++)
if (Character.isDigit(s.charAt(i))) {
str[++j] = "" + s.charAt(i);
}
DavidKroukamp
Practically a Master Poster
693 posts since Dec 2011
Reputation Points: 282
Solved Threads: 169
@stultuske Think you ment:
str[j] = "" + s.charAt(i);
reason for the ""+ is because you are adding a char to a string[] which if you use
str[j] = s.charAt(i);
will give you an error also charAt is a method and must have parenthesis i.e (). Also you forgot a closing curly brace in your for statement.
So the final code might be similar to:
for (int i = 0, j = -1; i < s.length(); i++)
if (Character.isDigit(s.charAt(i))) {
str[++j] = "" + s.charAt(i);
}
I didn't really mean anything by it :)
I just copied the code from the previous post and adapted the logic. but you're right, in this way, the j is not needed and would need a char array. I just assumed the code to be tested before it was posted here, didn't look at every line.
stultuske
Posting Sensei
3,137 posts since Jan 2007
Reputation Points: 1,114
Solved Threads: 433