View Full Version : Deleting Spaces/Digits

02-11-2008, 07:45 PM
I was wondering how you would delete a space in java language.
For example if i promted someone to enter a telephone number and they entered "271 0981", how would i delete that space and save the number to an int?

If you don't want to give me the answer guidance would also be appreviated.

Thanks a million,


02-11-2008, 08:40 PM
You could do one of two things. You could either use the string replaceall method to replace " " with "".

Or, you could split the string by " " and then concatenate the array that spawns from it.

ReplaceAll (http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html#replaceAll(java.lang.String,%20java.lang.String))
Split (http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html#split(java.lang.String))

If you're looking for a more efficient method I can give you one that is O(n) rather then these two which probably are somewhere closer to O(n^2). But, I don't think you're too concerned about that!

Philip M
02-12-2008, 09:33 AM
I have the idea that the questioner has in mind JavaSCRIPT as he posted in the Javascript forum. I suspect that he does not realise that Java and Javascript are entirely different programming languages, in spite of the confusingly similar names.

<script type = "text/javascript">

num = prompt ("Please Enter your Telephone Number", "");
num = num.replace(/\s+/g,""); // delete spaces
// OR num = num.replace([^0-9]/g,""); delete anything but digits
alert (num);


02-12-2008, 05:01 PM
Thanks Aradon i think the link "Split" is exactly what im looking for. I don't have time to work on this question until my spare in a couple hours but it seems like good help.
Thanks Phil for your post even though i think i am a bit confused. I must be in the wrong section of the forum because Im using Dr.Java to program orJDKand textpad or whatever.

Whats the difference between JavaSCRIPT and Java?

Sorry im pretty new to programming.

Thanks a million guys,


Philip M
02-12-2008, 06:43 PM
OK, you really are coding in Java.

For a note on the differences see:-


02-12-2008, 08:26 PM
You can think of it this way. Java is an application programming language.

javascript is a client-side html scripting language.

02-12-2008, 09:20 PM
Ohh ok thanks for the info.
Ill stick to the java forum section. Thanks!!!

02-12-2008, 11:00 PM
Now that this has been diverted to the JAVA section then ill ask here instead of making a new topic.

If i were just isolating the digits i can do this.

import javax.swing.*;

public class TestProgram{

public static void main (String[]args) {

String input;
int stuNum;
int digit;
final int NUM_DIGITS = 9;

input = JOptionPane.showInputDialog("Enter student number.");
stuNum = Integer.parseInt(input);

for (int i=0; i<NUM_DIGITS; i++){
digit = stuNum&#37;10;
System.out.println("The isolated digit is: " + digit);
stuNum = stuNum/10;


System.out.println("Programmed by me");



This code worked perfect for isolating each digit. I just dont know how i incorporate my code to delete any spaces if the user enteres a number with spaces:

From my understanding of deleting spaces i wrote the code:

input = JOptionPane.showInputDialog("Please enter a 9 digit SIN number");

input = JOptionPane.showInputDialog("Don't Press Cancel! Please enter a 9 digit SIN number");

for( int i = 0; i < input.length(); i++ ){
if ( input.charAt(i) != ' ' ){
inputNoSpaces = inputNoSpaces + input.charAt(i);

And my algorhithm being

digit = (((digit * 2) - 1)%9) + 1;

I am truly unsure how to put all this code together. Im confused how i delete the spaces.....save the new number after the spaces are deleted, isolate every second digit, and apply my algorhithm to every second digit. I'm more confused now then when i started. Please help?

02-13-2008, 06:13 AM
at the end of the day.....im still confused...more so than when the day started ughg.

02-13-2008, 05:10 PM
Could you post your entire code so that we could take a look at it in full?

Here is an example of deleting a space:

public class Test
public static void main(String[] args)
String test = "blah blah";
String newString = test.replaceAll(" ", "");

In this code, the String test is initialized to a String with a space in it. Then another String is initialized to the value returned by the replaceAll() method of class String. The replace method takes 2 arguments, the first is the string that you are replacing, in which I entered a space character. The second argument is the character you want to replace it with, for which I entered the empty string. This is the first option that Aradon mentioned in his post.

The output of the program looks like this:

blah blah

02-13-2008, 07:05 PM
Thanks for everyones help but finally i finished my project. It looks like this:

Not sure why this took so long and was so confusing to me.....but it did. Thanks for all the help people.

How does the code look?

import javax.swing.*;
import java.util.Date;

public class This_TOOK_Forever {

public static void main (String[]args) { //main method here

//Variables here

String input;
String inputNoSpaces = "";
int sin;
int total = 0;
int digit;

input = JOptionPane.showInputDialog(null, "Please enter a 9 digit SIN number!"); //prompts user for an input SIN number
System.out.println("The SIN number you entered was " + input); //Displays what the user typed in

if (input==null)
input = "0";

for( int i = 0; i < input.length(); i++ ){
if ( input.charAt(i) != ' ' )
inputNoSpaces = inputNoSpaces + input.charAt(i);


System.out.println("After removing the blanks the input is " + inputNoSpaces);

if (inputNoSpaces.length() != 9)
System.out.println("Warning: The SIN number you entered is: " + inputNoSpaces + " This is not 9 digits long.");

sin = Integer.parseInt (inputNoSpaces);

for (int i = 0; i < inputNoSpaces.length(); i++ ){
digit = sin % 10;
if (i % 2 == 1)


digit = (((digit * 2) - 1)%9) + 1;


total = total + digit;

System.out.println(digit + "Is added for a running total of " + total);

sin = sin / 10;


System.out.println("The check digit is " + (total % 10));

if (total % 10 == 0)
System.out.println("This is a valid SIN number");

System.out.println("This is not a valid SIN number");

System.out.println ("Programmed by COMP 1010 student ALEX WISHART");
System.out.println ("Date: " + new Date());
System.out.println ("***End of Processing***");




The SIN number you entered was 123 456 789
After removing the blanks the input is 123456789
9 Is added for a running total of 9
7 Is added for a running total of 16
7 Is added for a running total of 23
3 Is added for a running total of 26
5 Is added for a running total of 31
8 Is added for a running total of 39
3 Is added for a running total of 42
4 Is added for a running total of 46
1 Is added for a running total of 47
The check digit is 7
This is not a valid SIN number
Programmed by Simplistic2099
Date: Wed Feb 13 11:49:43 CST 2008
***End of Processing***

See anything wrong in the syntax or anything?

Thanks everyone,


02-13-2008, 07:22 PM
Looks ok at a glance to me. I would suggest using one of the more standard methods of removing the spaces from the input, simply because they are written for you already. It's always good to know how to accomplish the task yourself, but it's also a good idea to use the tools that are provided for you. Not to mention it makes the code less complex as you can change:

for( int i = 0; i < input.length(); i++ ){
if ( input.charAt(i) != ' ' )
inputNoSpaces = inputNoSpaces + input.charAt(i);

into this:

inputNoSpaces = input.replaceAll(" ","");
These kind of changes allow people reading your code to focus on what the goal of your program is, instead of being distracted by trivial tasks that only contribute to a portion of what your application does. In the future, you can find information on the String class and its methods, along with any other standard classes in the Java API documentation here (http://java.sun.com/j2se/1.5.0/docs/api/index.html).

02-13-2008, 07:55 PM
Thanks Brad. I used that instead and it also worked perfectly. It definitely eliminates distracting clutter.

A lot of my code is written pretty prematurely because my java language vocabulary is almost nothing. I just started taking computer science.

BUT that link is awesome. I definitely saved that to my favourites and will be using it in the future.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum