Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Java .split not working?

  1. #1
    Junior Member
    Join Date
    Mar 2014
    Location
    Spain
    Posts
    3
    My Mood
    Confused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Java .split not working?

    First of all, hello! I am new here, and I come with a very newbish question. For some reason, this decided not to work, and I spent a couple of hours trying to realize why, and I just can't manage to.
    Here is the code:
    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.ArrayList;
     
    public class Learn {
     
    	public static void readFile(String file) {
    		String[] arr;
    		BufferedReader br = null;
    		ArrayList<String> str = new ArrayList<String>();
    		try {
    			String currentLine;
    			br = new BufferedReader (new FileReader(file));
    			while((currentLine = br.readLine()) != null) {
    				if(currentLine.contains(".")) {
    					arr = currentLine.split(".");
    					System.out.println(arr.length);
    					for(int i = 0; i < arr.length; i++) {
    						System.out.println(arr[i]);
    						if(!(arr[i].equals("")))
    							str.add(arr[i]);
    					}
    				}
    				for(String st : str) {
    					System.out.println(st);
    				}
    			}
    		}
    		catch(IOException e) {
    			e.printStackTrace();
    		}
    		finally {
    			try {
    				if( br != null )
    					br.close();
    			}
    			catch(IOException e) {
    				e.printStackTrace();
    			}
    		}
    	}
    }

    and the file it is reading is
    This is a test for sentences. This sentence is one.
    This is another
    The curentLine.contains(".") expression evaluates to true, but the code stops there (I mean, just skips the rest). I added that println there, and the output is 0; so basically, something goes wrong in the split().
    Any help is greatly appreciated!
    Thank you for your time!
    Best regards,
    BrreaKer
    Last edited by BrreaKer; March 15th, 2014 at 08:02 AM. Reason: Solved


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java .split not working?

    The . is a regex special character. Add the escape character: \ to use it as a character. There will need to be two \\s because \ is also a compiler special character.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following 2 Users Say Thank You to Norm For This Useful Post:

    BrreaKer (March 15th, 2014), Zavael (April 7th, 2014)

  4. #3
    Member
    Join Date
    Feb 2014
    Posts
    180
    Thanks
    0
    Thanked 48 Times in 45 Posts

    Default Re: Java .split not working?

    See String (Java Platform SE 7 ) for the API of the split method, and Pattern (Java Platform SE 7 ) for regular expression constructs as well as the use of backslashes.

    An alternative to using the split method is to use String's indexOf and substring methods.

  5. The Following User Says Thank You to jashburn For This Useful Post:

    BrreaKer (March 15th, 2014)

  6. #4
    Junior Member
    Join Date
    Mar 2014
    Location
    Spain
    Posts
    3
    My Mood
    Confused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Java .split not working?

    Thank you! I totally forgot about regex!

Similar Threads

  1. Replies: 4
    Last Post: October 3rd, 2012, 07:40 AM
  2. java code to split text documents into paragraphs and sentences
    By draksha in forum Java Theory & Questions
    Replies: 5
    Last Post: August 17th, 2011, 05:06 AM
  3. Java split problem..
    By arch in forum Java SE APIs
    Replies: 5
    Last Post: August 11th, 2011, 07:48 AM
  4. MVC - Split the Java servlet(help needed)
    By kamweshi in forum Java Servlet
    Replies: 1
    Last Post: October 18th, 2010, 09:02 AM
  5. Replies: 4
    Last Post: January 27th, 2009, 12:03 AM