I like coding, though I'm really stupid, and not very good at it. I'm having trouble figuring out how to remove spaces from a String. I'm such a noobie.. if anyone wants to take a few min to educate me i'd appreciate it.
The thing i was attempting to do with this mess of a code, was basically have the user type in a phone number, and the program will take what they typed in, and remove all spaces and symbols that were typed, and turn the numbers into one string..
so say you type 569-678-4567 or 569 678 4567
the program returns 5696784567

public static void main(String[] args) throws IOException
	{
			String s;
			String number;
			int i, j, k;
			BufferedReader br = new
			BufferedReader(new InputStreamReader(System.in));
			System.out.print("Enter a phonenumber: ");
			number = br.readLine();
			System.out.print(number);
			rev(number);
		}

	public static String rev(String number)
	{

		StringTokenizer st;
		StringBuffer full = new StringBuffer();
		st = new StringTokenizer(number, " -,.?!:;");
		return st;

		}
	}

Sorry if it's really confusing..i just got a little help from a friend, and I had no idea what to do after they helped a little, so some things here probably don't make much sense..

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.

Alright, so that helped a bit. Thanks. Could anyone possibly help with the removing spaces from the string now?

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?

import java.io.*;
import java.util.StringTokenizer;

public class HW1
{
	public static void main(String[] args) throws IOException
	{
			String s;
			String number;
			int i, j, k;
			BufferedReader br = new
			BufferedReader(new InputStreamReader(System.in));
			System.out.print("Enter a phonenumber: ");
			number = br.readLine();
			rev(number);
			System.out.print(number);

		}

	public static StringTokenizer rev(String number)
	{

		StringTokenizer st;
		st = new StringTokenizer(number, " -,.?!:;");
		return st;

		}
	}

yea, i mean it's still not great, but it works. You can use the tokenizer command to achieve that effect correct?

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);

Well i think even this could help..
suppose we have the string in 's'
and copy the string into another variable 'str'

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
} 
//else if it is a digit
else
{
 str[j]=s.charAt[i];
 j++;
}

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++;
    }

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);
            }

Edited 4 Years Ago by DavidKroukamp: n/a

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++;
    }

yes this also works :)

@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.

Edited 4 Years Ago by stultuske: stupid typo

This question has already been answered. Start a new discussion instead.