Results 1 to 11 of 11
  1. #1
    Join Date
    Feb 2013
    Posts
    33
    Rep Power
    0

    Default Need help on removing spaces and punctuations from string.

    I need to remove spaces and punctuations from a text of the bible. I want to remove them because I need to create a program to find codes in that text.

    I tried searching for a solution or something to help me create this but I'm not having any luck.

    this is the text gc1.txt

    I think this is used to remove spaces from a string but I don't know where to apply it.
    Do I make a new class to use this?
    Java Code:
    .trim();

    This are the instructions the teacher gave us:
    1. You have to decide on what size recangular array
    to use for collecting your code matrix
    You can refer to web references or try different
    sizes.
    2. You have to decide on how to extract characters.
    For example, equidistant extraction means to
    select, for example, every 5th character but
    ignore spaces and punctuation (".", "!", etc.)



    This is my code for reading the file

    Java Code:
    import java.io.*;
    class FileRead {
    
        public static void main(String args[]) {
    
            try {
                FileInputStream fs;
                fs = new FileInputStream("gc1.txt");
                DataInputStream in;
                in = new DataInputStream(fs);
                BufferedReader br;
                br = new BufferedReader(new InputStreamReader(in));
                String s;
    
                while ((s = br.readLine()) != null) {
      		System.out.printf("%s\n",s);
                }
                in.close();
             } 
    
            catch (Exception e) {
                System.err.println("Err: " + e.getMessage());
            }
    
        }
    }

  2. #2
    jbarke12 is offline Member
    Join Date
    May 2013
    Location
    Charleston, South Carolina USA
    Posts
    82
    Rep Power
    0

    Default Re: Need help on removing spaces and punctuations from string.

    Basically thus far you have it printing the file. I would add each line to an ArrayList<String> I would then would pass that array to a method to parse out the spaces and symbols and add what you want to your matrix. .trim() does get rid of space.

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,520
    Blog Entries
    7
    Rep Power
    20

    Default Re: Need help on removing spaces and punctuations from string.

    You might give regular expressions a try; read the API for the Pattern class for starters.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    Join Date
    Feb 2013
    Posts
    33
    Rep Power
    0

    Default Re: Need help on removing spaces and punctuations from string.

    So i have to take the whole text and put every line to an ArrayList?
    If so how would I do this?
    My teacher doesn't teach very good so its hard for me to learn in that class. He hasn't talked about ArrayList<String> or file reader. He just gave us that code and told us to use web to work on this project.

  5. #5
    jbarke12 is offline Member
    Join Date
    May 2013
    Location
    Charleston, South Carolina USA
    Posts
    82
    Rep Power
    0

    Default Re: Need help on removing spaces and punctuations from string.

    There are many ways to do this. It depends on how you want to approach it. ArrayLists is one way of doing it, but like jos said regular expressions is also another way to do it. The next question I have in this matrix are you suppose to have a single character or word in each position?

  6. #6
    Join Date
    Feb 2013
    Posts
    33
    Rep Power
    0

    Default Re: Need help on removing spaces and punctuations from string.

    I don't know. Like I said he doesn't teach good. When he is giving lectures he googles half the time about random things which distracts the whole class. I regret taking his class.
    Well anyways thanks for your guys help. I'll just try to figure this out by myself or might not even do it since its only worth 10 points.

  7. #7
    jbarke12 is offline Member
    Join Date
    May 2013
    Location
    Charleston, South Carolina USA
    Posts
    82
    Rep Power
    0

    Default Re: Need help on removing spaces and punctuations from string.

    Well I was just asking because it matters in which approach you would do. 10 points could be a letter grade by the end of the course depending on how he has it structured

  8. #8
    Join Date
    Feb 2013
    Posts
    33
    Rep Power
    0

    Default Re: Need help on removing spaces and punctuations from string.

    Okay so how would you approach it.
    Can't I just take the whole text file and remove the spaces and punctuations?
    Then use a loop to get every nth letter in the text?
    I'm not sure what a matrix is in java
    Does it have to do something with this?

    Java Code:
    int array[][]= {{1,2,3},{4,5,6}};

  9. #9
    Join Date
    Apr 2013
    Posts
    41
    Rep Power
    0

    Default Re: Need help on removing spaces and punctuations from string.

    You can also cycle through it with a loop; which looks as each letter individually and checks if its a space or punctuation. Then simply just make it not print it if it is.

  10. #10
    Join Date
    Feb 2013
    Posts
    33
    Rep Power
    0

    Exclamation Re: Need help on removing spaces and punctuations from string.

    Okay I got a working code

    I'm wonder if it possible to have a matrix of this string.
    I want it to pick every nth letter of the file.

    This is what I need to do:

    Write a mini Bible Codes program.
    1. Use the attached file gc1.txt (Genesis, Chapter 1) as input
    2. Open the file and read the data into a String.
    3. Use equidistant characters (i.e., every
    5th character ignoring spaces/newlines), and place
    into an array.
    4. Print out the array and look for any codes.
    Is it something like this?

    Java Code:
     char [][] M ={ String s3 };
     for(int r=0;r<7;r++){
      for(int c=0;c<13;c++){
       System.out.print(M[r][c]+"\t ");
       }

    file code

    Java Code:
    import java.io.*;
    import java.util.ArrayList;
    
    class File {
    
        public static void main(String args[]) {
    
            try {
                FileInputStream fs;
                fs = new FileInputStream("gc1.txt");
                DataInputStream in;
                in = new DataInputStream(fs);
                BufferedReader br;
                br = new BufferedReader(new InputStreamReader(in));
                String s = null;
    	    
    	    ArrayList<String[]> al = new ArrayList<String[]>();
    		
                while ((s = br.readLine()) != null) {
    		String s1 = s.trim();
    		String s2 = s1.replaceAll("\\s", "");
    		String s3 = s2.replaceAll("\\W", "");
    
      		System.out.printf("%s\n",s3);
                }
                in.close();
             } 
    
            catch (Exception e) {
                System.err.println("Err: " + e.getMessage());
            }
    
        }
    }
    this is the output of the code above:

    InthebeginningGodcreatedtheheavenandtheearth
    Andtheearthwaswithoutformandvoidanddarknesswasupon thefaceofthedeepAndtheSpiritofGodmoveduponthefaceo fthewaters
    AndGodsaidLettherebelightandtherewaslight
    AndGodsawthelightthatitwasgoodandGoddividedtheligh tfromthedarkness
    AndGodcalledthelightDayandthedarknesshecalledNight Andtheeveningandthemorningwerethefirstday
    AndGodsaidLettherebeafirmamentinthemidstofthewater sandletitdividethewatersfromthewaters
    AndGodmadethefirmamentanddividedthewaterswhichwere underthefirmamentfromthewaterswhichwereabovethefir mamentanditwasso
    AndGodcalledthefirmamentHeavenAndtheeveningandthem orningwerethesecondday
    AndGodsaidLetthewatersundertheheavenbegatheredtoge theruntooneplaceandletthedrylandappearanditwasso
    AndGodcalledthedrylandEarthandthegatheringtogether ofthewaterscalledheSeasandGodsawthatitwasgood
    AndGodsaidLettheearthbringforthgrasstheherbyieldin gseedandthefruittreeyieldingfruitafterhiskindwhose seedisinitselfupontheearthanditwasso
    Andtheearthbroughtforthgrassandherbyieldingseedaft erhiskindandthetreeyieldingfruitwhoseseedwasinitse lfafterhiskindandGodsawthatitwasgood
    Andtheeveningandthemorningwerethethirdday
    AndGodsaidLettherebelightsinthefirmamentoftheheave ntodividethedayfromthenightandletthembeforsignsand forseasonsandfordaysandyears
    Andletthembeforlightsinthefirmamentoftheheaventogi velightupontheearthanditwasso
    AndGodmadetwogreatlightsthegreaterlighttoruletheda yandthelesserlighttorulethenighthemadethestarsalso
    AndGodsettheminthefirmamentoftheheaventogivelightu pontheearth
    Andtoruleoverthedayandoverthenightandtodividetheli ghtfromthedarknessandGodsawthatitwasgood
    Andtheeveningandthemorningwerethefourthday
    AndGodsaidLetthewatersbringforthabundantlythemovin gcreaturethathathlifeandfowlthatmayflyabovetheeart hintheopenfirmamentofheaven
    AndGodcreatedgreatwhalesandeverylivingcreaturethat movethwhichthewatersbroughtforthabundantlyafterthe irkindandeverywingedfowlafterhiskindandGodsawthati twasgood
    AndGodblessedthemsayingBefruitfulandmultiplyandfil lthewatersintheseasandletfowlmultiplyintheearth
    Andtheeveningandthemorningwerethefifthday
    AndGodsaidLettheearthbringforththelivingcreatureaf terhiskindcattleandcreepingthingandbeastoftheearth afterhiskindanditwasso
    AndGodmadethebeastoftheearthafterhiskindandcattlea ftertheirkindandeverythingthatcreepethupontheearth afterhiskindandGodsawthatitwasgood
    AndGodsaidLetusmakemaninourimageafterourlikenessan dletthemhavedominionoverthefishoftheseaandoverthef owloftheairandoverthecattleandoveralltheearthandov ereverycreepingthingthatcreepethupontheearth
    SoGodcreatedmaninhisownimageintheimageofGodcreated hehimmaleandfemalecreatedhethem
    AndGodblessedthemandGodsaiduntothemBefruitfulandmu ltiplyandreplenishtheearthandsubdueitandhavedomini onoverthefishoftheseaandoverthefowloftheairandover everylivingthingthatmovethupontheearth
    AndGodsaidBeholdIhavegivenyoueveryherbbearingseedw hichisuponthefaceofalltheearthandeverytreeinthewhi chisthefruitofatreeyieldingseedtoyouitshallbeforme at
    Andtoeverybeastoftheearthandtoeveryfowloftheairand toeverythingthatcreepethupontheearthwhereinthereis lifeIhavegiveneverygreenherbformeatanditwasso
    AndGodsaweverythingthathehadmadeandbeholditwasvery goodAndtheeveningandthemorningwerethesixthday

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,006
    Rep Power
    20

    Default Re: Need help on removing spaces and punctuations from string.

    You need to finish step 2 first.
    You aren't saving the stuff into a String yet.

    Initialise your String 's' to "" (empty string) then in the loop append 's3' to it:
    s += s3;

    That gives you your single String with all spaces and stuff removed.

    Now, not sure why they want an array for the "every x characters" part, and not simply build up another string...
    But just construct a char[] of a suitable size (you know how long the String 's' is, so you should be able to calculate how long the array needs to be).
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. How do you scan a string with spaces?
    By Magnesium in forum New To Java
    Replies: 1
    Last Post: 11-10-2012, 06:06 AM
  2. Removing spaces in String ArrayList
    By dougie1809 in forum New To Java
    Replies: 6
    Last Post: 03-13-2012, 09:25 PM
  3. Replies: 1
    Last Post: 09-15-2011, 01:51 PM
  4. How to display a String that contains more than one white spaces
    By thachun in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 09-27-2009, 09:16 AM
  5. Replies: 3
    Last Post: 08-20-2009, 03:57 PM

Posting Permissions

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