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).
Code Java:
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;
calls.add(this);
}
Code Java:
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?
Thanks.
Re: Problem with split() method
Instead of calling next() call nextLine():
String file = filereader1.nextLine();
Re: Problem with split() method
Thanks a lot dencamel. It worked.