Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 13 of 13
  1. #1
    New Coder
    Join Date
    Jan 2008
    Location
    Winnipeg, Canada
    Posts
    13
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Deleting Spaces/Digits

    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,

    Alex

  • #2
    Regular Coder Aradon's Avatar
    Join Date
    Jun 2005
    Location
    USA
    Posts
    734
    Thanks
    0
    Thanked 20 Times in 19 Posts
    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
    Split

    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!
    "To iterate is human, to recurse divine." -L. Peter Deutsch

  • Users who have thanked Aradon for this post:

    simplistic2099 (02-12-2008)

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    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);

    </script>

  • Users who have thanked Philip M for this post:

    simplistic2099 (02-12-2008)

  • #4
    New Coder
    Join Date
    Jan 2008
    Location
    Winnipeg, Canada
    Posts
    13
    Thanks
    4
    Thanked 0 Times in 0 Posts
    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,

    --Alex

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    OK, you really are coding in Java.

    For a note on the differences see:-

    http://www.howtocreate.co.uk/tutoria...t/introduction

  • #6
    Regular Coder Aradon's Avatar
    Join Date
    Jun 2005
    Location
    USA
    Posts
    734
    Thanks
    0
    Thanked 20 Times in 19 Posts
    You can think of it this way. Java is an application programming language.

    javascript is a client-side html scripting language.
    "To iterate is human, to recurse divine." -L. Peter Deutsch

  • #7
    New Coder
    Join Date
    Jan 2008
    Location
    Winnipeg, Canada
    Posts
    13
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Ohh ok thanks for the info.
    Ill stick to the java forum section. Thanks!!!
    -Alex

  • #8
    New Coder
    Join Date
    Jan 2008
    Location
    Winnipeg, Canada
    Posts
    13
    Thanks
    4
    Thanked 0 Times in 0 Posts
    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");



    while(input==null){
    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);
    break;



    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?

  • #9
    New Coder
    Join Date
    Jan 2008
    Location
    Winnipeg, Canada
    Posts
    13
    Thanks
    4
    Thanked 0 Times in 0 Posts

    ughgh

    at the end of the day.....im still confused...more so than when the day started ughg.

  • #10
    Regular Coder brad211987's Avatar
    Join Date
    Sep 2005
    Location
    Ohio
    Posts
    631
    Thanks
    10
    Thanked 50 Times in 50 Posts
    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:

    Code:
    public class Test 
    {
        public static void main(String[] args)
        {
            String test = "blah blah";
            String newString = test.replaceAll(" ", "");
            
            System.out.println(test);
            System.out.println(newString);
        }
    }
    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:
    Code:
    blah blah
    blahblah

  • #11
    New Coder
    Join Date
    Jan 2008
    Location
    Winnipeg, Canada
    Posts
    13
    Thanks
    4
    Thanked 0 Times in 0 Posts

    finally

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

    else
    {
    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***");

    }

    }




    *_MY OUT PUT WAS_*

    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,

    --Alex

  • #12
    Regular Coder brad211987's Avatar
    Join Date
    Sep 2005
    Location
    Ohio
    Posts
    631
    Thanks
    10
    Thanked 50 Times in 50 Posts
    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:
    Code:
        for( int i = 0; i < input.length(); i++ ){
            if ( input.charAt(i) != ' ' )
                inputNoSpaces = inputNoSpaces + input.charAt(i);
    
        }
    into this:
    Code:
    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.
    Last edited by brad211987; 02-13-2008 at 06:25 PM.

  • Users who have thanked brad211987 for this post:

    simplistic2099 (02-13-2008)

  • #13
    New Coder
    Join Date
    Jan 2008
    Location
    Winnipeg, Canada
    Posts
    13
    Thanks
    4
    Thanked 0 Times in 0 Posts
    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.

    Thanks,


    --Alex


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •