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.


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

Results 1 to 3 of 3

Thread: Problem with split() method

  1. #1
    Join Date
    Jan 2012
    Thanked 59 Times in 57 Posts

    Default Problem with split() method

    Hello guys!
    My application reads a text file and creates the corresponding "Call". The "document.txt" keeps the data of a call in every line
    (callingNumber, dialedNumber, datetime, duration)
    (e.g. 4748832, 462346214, 01-01-2012 11:55:33, 126).

    This is my "Call" class (without the setters and getters).
    import java.util.ArrayList;
    public class Call { 
        private String callingNumber;
        private String dialedNumber;
        private String datetime;
        private int duration;
        static ArrayList <Call> calls = new ArrayList<Call>();
        public Call (String callingNumber , String dialedNumber, String datetime, int duration)
                this.callingNumber = callingNumber;
                this.dialedNumber = dialedNumber;
                this.datetime = datetime;
                this.duration = duration;

    public static void main (String args[]){
        try {
                File file1 = new File("C:\\...\\doc.txt");
                Scanner Filereader1 = new Scanner(file1);
                while (Filereader1.hasNextLine()) {
                    String file = Filereader1.next();
                    String[] x = null;
                    String caller = null;
                    String reciever = null;
                    String date = null;
                    String sDur = null;
                    int iDur = 0;
                    x = file.split(",");
                        for (int i = 0; i < x.length; i++) {
                            caller = x[0];
                            reciever = x[1];
                            date = x[2];
                            sDur = x[3];
                            iDur = Integer.parseInt(sDur);
                        Call call = new Call(caller, reciever, date, iDur);
            } catch (FileNotFoundException e) {
                System.out.println("error" + e);
    Here is my problem:
    (e.g. 4748832, 462346214, 01-01-201211:55:33, 126)
    (e.g. 4748832, 462346214, 01-01-2012 11:55:33, 126)
    For the first line (with no space between date and time in the datetime field) it works fine (a new call is created).
    For the second (which is the way my doc. should actually appear), it doesn't work (no call is created).
    Any thoughts why is this happening and how to solve it?

  2. #2
    Junior Member
    Join Date
    Jan 2012
    Thanked 0 Times in 0 Posts

    Default Re: Problem with split() method

    Instead of calling next() call nextLine():
    String file = filereader1.nextLine();

  3. #3
    Join Date
    Jan 2012
    Thanked 59 Times in 57 Posts

    Default Re: Problem with split() method

    Thanks a lot dencamel. It worked.

Similar Threads

  1. Using char at to split up a interger
    By mooncowtime in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 26th, 2011, 08:55 AM
  2. Java split problem..
    By arch in forum Java SE APIs
    Replies: 5
    Last Post: August 11th, 2011, 07:48 AM
  3. Help me split and then join a file
    By babbupandey in forum What's Wrong With My Code?
    Replies: 0
    Last Post: December 13th, 2010, 12:20 PM
  4. Thread Hangs at split method.
    By kailasvilaskore in forum Threads
    Replies: 1
    Last Post: November 26th, 2010, 12:13 PM
  5. MVC - Split the Java servlet(help needed)
    By kamweshi in forum Java Servlet
    Replies: 1
    Last Post: October 18th, 2010, 09:02 AM