Reputation: 257
Given a text file here:
Date Opening Closing
6-Mar-2012 500 1000
9-Jun-2011 110 970
7-Dec-2015 1090 980.69
28-Feb-2010 500 800
I need to build the dates in GregorianCalendar and have the date constructors in String. The date is shown Null in my output, any idea on a fix?
Output:
The date is null, the opening is 500, and the closing is 800
The date is declared GregorianCalendar, however, The constructors are taking date in String, and should expect the form "dd-MM-yyyy".
Class Djia:
public class Djia implements Comparable<Djia> {
// instance variables
private GregorianCalendar date;
private double opening;
private double closing;
public Djia(String dt, double opening, double closing) throws ParseException {
DateFormat stringDate = new SimpleDateFormat("dd-MM-yyyy");
Date date = stringDate.parse(dt);
Calendar gCal = new GregorianCalendar();
gCal.setTime(date);
this.opening = opening;
this.closing = closing;
}
public String date() {
String date;
SimpleDateFormat dateformat = new SimpleDateFormat("dd-MM-yyyy");
date = dateformat.format(this.date.getTime());
return date;
}
public double opening() {
return opening;
}
public double closing() {
return closing;
}
//...
}
Main Method:
I have done parsing and formatting here, because months are of String representation in the given text file. First, I parsed months to Integer representation and then formatted from Date to String because Djia Class only take Date as String.
Scanner input = new Scanner(new File("file.txt"));
while (input.hasNextLine()) {
String line = input.nextLine();
String[] fields = line.split(" ");
String date = fields[0];
SimpleDateFormat simpleDate = new SimpleDateFormat("d-MMM-yyyy");
Date nDate = simpleDate.parse(date);
Format stringDate = new SimpleDateFormat("dd-MM-yyyy");
String newDate = stringDate.format(nDate);
double closing = Double.parseDouble(fields[2]);
double opening = Double.parseDouble(fields[1]);
Djia newDjia = new Djia(newDate, opening, closing);
//...
}
Upvotes: 0
Views: 145
Reputation: 1001
You´ve got to do this in your main:
Here is the result:
import java.io.File;
import java.io.FileNotFoundException;
import java.text.DateFormat;
import java.text.Format;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.LinkedList;
import java.util.Locale;
import java.util.Scanner;
public class Djia {
private GregorianCalendar date;
private double opening;
private double closing;
public Djia(String dt, double opening, double closing)
throws ParseException {
DateFormat stringDate = new SimpleDateFormat("dd-mm-yyyy");
Date date = stringDate.parse(dt);
GregorianCalendar gCal = new GregorianCalendar();
gCal.setTime(date);
this.opening = opening;
this.closing = closing;
this.date = gCal;
}
public String date() {
String date;
SimpleDateFormat dateformat = new SimpleDateFormat("MM/dd/YYYY");
date = dateformat.format(this.date.getTime());
return date;
}
public static void main(String[] args) {
try {
Scanner input = new Scanner(new File("file.txt"));
while (input.hasNextLine()) {
String line = input.nextLine();
LinkedList fields=new LinkedList<String>(Arrays.asList(line.trim().split(" ")));
for (int i=fields.size()-1; i>=0; i--) {
if (fields.get(i).equals("")) {
fields.remove(i);
}
}
String date = (String) fields.get(0);
SimpleDateFormat simpleDate = new SimpleDateFormat("d-MMM-yyyy", Locale.US);
Date nDate = simpleDate.parse(date);
Format stringDate = new SimpleDateFormat("dd-MM-yyyy", Locale.US);
String newDate = stringDate.format(nDate);
double closing = Double.parseDouble((String) fields.get(2));
double opening = Double.parseDouble((String) fields.get(1));
Djia newDjia = new Djia(newDate, opening, closing);
System.out.println("The date is " + newDate + ", the opening is " + opening + ", and the closing is " + closing);
}
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Upvotes: 1
Reputation: 6414
Maybe you have space characters at the beginnig of the line:
try changing the line:
String[] fields=line.split(" ");
to
String[] fields=line.trim().split(" ");
by adding the trim() call, that will remove any space charcters form the beginning and the end of your line.
or do:
String dateString = line.substring(0, 12);
if there is always the same length and align of the date part of the line.
Upvotes: 1