I have specific text file looking like this:
name: meeting_name1
description:
04/18/2012 00:00:00
05/18/2012 00:00:00
... (more dates)
07/18/2012 00:00:00
name: meeting_name2
description: some_desc
04/18/2012 00:00:00
05/18/2012 00:00:00
... (more dates)
07/18/2012 00:00:00
(etc)
I have java object looking like this:
class Meeting {
String name;
String description;
List<Date> dates;
}
My point is to read the file, parse values, create objects and save them to database.
I can read the file line by line and convert it to List<String>, ie. all data together.
`I can make and fill one java object with values and save it to database.
My issue here is how to find out that I'm at the end of dates and lines (name: meeting_name2) of new object begin.
So I could make something like List<List<String>> where List<String> would be equal to one object, ie. List<Meeting>?
Not sure if its understandable, sorry for formatting.
Assumption that you could read the file data to List variable. (See above answer)
List<String> lines = Files.readAllLines(Paths.get("FILE_NAME"));
Now, you can see below code as a demo. It is the simple loop and if else statament.
Hope it will help you.
public static void main(String[] args) throws ParseException {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
List<String> data = new ArrayList<>();
data.add("name: meeting_name1");
data.add("description: some_desc");
data.add("07/18/2012 00:00:00");
data.add("07/18/2012 00:00:00");
data.add("name: meeting_name2");
data.add("description: some_desc");
data.add("07/18/2012 00:00:00");
List<Meeting> result = new ArrayList<>();
Meeting temp = null;
for (String line : data) {
if (line.startsWith("name:")) {
temp = new Meeting(line.split(":")[1].trim());
result.add(temp);
} else if (line.startsWith("description:")) {
temp.setDescription(line.split(":")[1].trim());
} else {
temp.getDates().add(simpleDateFormat.parse(line)); // Use date for
}
}
System.out.println(result.get(0).getName() + ": " + result.get(0).getDates().size()); // meeting_name1: 2
System.out.println(result.get(1).getName() + ": " + result.get(1).getDates().size()); // meeting_name2: 1
}
static class Meeting {
String name;
String description;
List<Date> dates;
public String getName() {
return name;
}
public List<Date> getDates() {
return dates;
}
Meeting(String name) {
this.name = name;
this.dates = new ArrayList<>();
}
public void setDescription(String description) {
this.description = description;
}
}
One possibility would be to read all lines first. You would not need to worry about the end of lines with:
List<String> lines = Files.readAllLines(Paths.get("FILE_NAME"));
then iterarate through the array,
if a line starts with "name:" you make a new object and add the data like that:
List<Meeting> meetings = new ArrayList();
Meeting currentMeeting;
for (String line : lines) {
if(line.startsWith("name:"))
{
currentMeeting = new Meeting();
meetings.add(currentMeeting);
//...add data (name)
}
//...add more data (description and dates)
}
Related
How add Local date list to myList in java
Please find the below code and check where I made a mistake to add dates to myList
I declare List as Local date now need to convert to myModel
error occurs in // myList.addAll(employeeReportsModel.getReportFromDt());
#RequestMapping(value = "/getMusterRollDateBased", method = RequestMethod.GET)
public ModelAndView getMusterRollDateBased(EmployeeReportsModel employeeReportsModel) {
ModelAndView mv = new ModelAndView();
String tablePrefix = "at_hr_logs_";
try {
System.out.println("refort from date.."+employeeReportsModel.getReportFromDt());
System.out.println("refort to date.."+employeeReportsModel.getReportToDt());
String fromdate = employeeReportsModel.getReportFromDt();
String todate = employeeReportsModel.getReportToDt();
Date date1 = new SimpleDateFormat("yyyy-MM-dd").parse(fromdate);
Date date2 = new SimpleDateFormat("yyyy-MM-dd").parse(todate);
System.out.println(date1 + "-----" + date2);
// parse the date into another format
SimpleDateFormat sdfDestination = new SimpleDateFormat("yyyy-MM-dd");
String fromdate1 = sdfDestination.format(date1);
String todate1 = sdfDestination.format(date2);
employeeReportsModel.setReportFromDt(fromdate1);
// get dates between two dates
String startString = fromdate1;
String endString = todate1;
LocalDate incrementingDate = LocalDate.parse(startString);
LocalDate endDate = LocalDate.parse(endString);
List<LocalDate> allDates = new ArrayList<>();
while (!incrementingDate.isAfter(endDate)) {
allDates.add(incrementingDate);
incrementingDate = incrementingDate.plusDays(1);
}
System.err.println(allDates);
List<EmployeeReportsModel> myList = null;
mv.addObject("allDates",allDates);
for (LocalDate date : allDates) {
//System.out.println();
System.out.println("dates is..." + date);
employeeReportsModel.setReportFromDt(date.toString());
String[] parts = date.toString().split("-");
String part1 = parts[0]; // 004
String part2 = parts[1]; // 034556
System.out.println("year ....." + part1);
System.out.println("Month....." + part2);
String tableName = tablePrefix + part1 + '_' + part2;
System.out.println("hks table name is.." + tableName);
employeeReportsModel.setDynamicTableName(tableName);
myList.addAll(employeeReportsModel.getReportFromDt());
//here we get a problem to add
}
} catch (Exception e) {
e.printStackTrace();
}
mv.setViewName("/" + moduleName + "/employeeMusterRollBasedOnDateInter");
return mv;
}
First thing is that you call myList.addAll() on a null object. Your list should be initialized properly like myList = new ArrayList().
Then you are trying to store a String type in your list which expects an object of type EmployeeReportsModel.
So you either change your myList to look like List<String> or change your return type of the getReportFromDt() method.
I suggest you to rethink your current code.
Currently you are basically doing the following:
get a local date > create a String out of it > store it with setReportFromDt > call getReportFromDt > try to store LocalDate in string form into a List of type EmployeeReportsModel
Regex master needed!
I have an variable timestamp coming from the server and I need to find which format is used everytime. I've tried implementing a regex formats but they don't work. I'm fairly new to regex patterns but still I've tried working them up my self or else look for a specific example but couldn't find so I'm asking you.
The formats from the server can look like this:
"2015-02-23 15:27:31 UTC"
or
"2015-01-22T19:38:40Z"
here is the code to find the formats:
private static String getFormat(String time) {
String firstRegEx = "^\\d{4}-\\d{2}-\\d{2}\'T+\'\\d{2}:\\d{2}:\\d{2}\'Z\'$";
String secondRegEx = "^\\d{4}-\\d{2}-\\d{2}\\s\\d{2}:\\d{2}:\\d{2}\\s\\w{3}$";
if (time.toLowerCase().matches(firstRegEx)) {
return firstRegEx;
} else if (time.toLowerCase().matches(secondRegEx)) {
return secondRegEx;
}
return null;
}
Can you look at my regex patterns and tell me what am I doing wrong?
First you have to remove the single quotes arround the char Tand Zand second you call toLowercase() wich will canvert T to t and Z to z. remove it:
private static String getFormat(String time) {
String firstRegEx = "^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}Z$";
String secondRegEx = "^\\d{4}-\\d{2}-\\d{2}\\s\\d{2}:\\d{2}:\\d{2}\\s\\w{3}$";
if (time.matches(firstRegEx)) {
return firstRegEx;
} else if (time.toLowerCase().matches(secondRegEx)) {
return secondRegEx;
}
return null;
}
^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}Z$
Your first regex should be simply this.This will match 2015-01-22T19:38:40Z.See demo.
https://regex101.com/r/aI4rA5/4
Your second regex works fine.
I believe this is the alternative solution suggested in the comments...
public static void main(String[] args) {
System.out.println(getFormat("2015-02-23 15:27:31 UTC"));
System.out.println(getFormat("2015-01-22T19:38:40Z"));
}
private static DateFormat getFormat(String time) {
DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssX");
DateFormat format2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z");
if (isFormat(format1, time)) {
return format1;
} else if (isFormat(format2, time)) {
return format2;
} else {
return null;
}
}
private static boolean isFormat(DateFormat format, String candidate) {
return format.parse(candidate, new ParsePosition(0)) != null;
}
If you were using the regex to decide how to parse later on you could bundle this into a single method capable of consuming multiple formats...
public static void main(String[] args) {
System.out.println(getDate("2015-02-23 15:27:31 UTC"));
System.out.println(getDate("2015-01-22T19:38:40Z"));
}
private static Date getDate(String time) {
DateFormat[] formats = { new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssX"),
new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z") };
Date date = null;
for (DateFormat format : formats) {
if ((date = format.parse(time, new ParsePosition(0))) != null) {
break;
}
}
return date;
}
If these are the only possible formats then it is enough to test if date.charAt(10) == 'T'
I am have trouble creating an array or object(with multiple fields) and sending it to an array-list. Any help would be greatly appreciated. I have spent hours looking through every video on YouTube with the words object and array-list in them and have been unable to find much help.
The program needs to prompt the user to pick a option (1. AddItem) then prompt the user for the name and format (dvd, vhs) and save multiple objects with these variables in an array-list. I either keep having the location where it is saved in memory returned to me or instead of multiple objects one large object is created.
Library:
import java.util.Scanner;
import java.util.ArrayList;
public class Library {
static ArrayList<Object> items = new ArrayList<Object>();
static int menuOption;
static Scanner scan = new Scanner(System.in);
public static void main(String args[]) {
String title, format;
boolean right = false;
do{
displayMenu();
if (menuOption == 1){
System.out.println("Enter Title: ");
title = scan.next();
System.out.println("Enter format: ");
format = scan.next();
addNewItem(title, format);
} else {System.out.println(items);
}
} while (!right);
}
static int displayMenu(){
System.out.println("Menu: ");
System.out.println("1. Add New Item");
menuOption = scan.nextInt();
return menuOption;
}
static void addNewItem(String title, String format){
MediaItem b = new MediaItem();
b.setTitle(title);
b.setFormat(format);
items.add(b);
}
}
MediaItem:
public class MediaItem {
String title;
String format;
MediaItem(){
title = null;
format = null
}
MediaItem(String title, String format){
title = new String();
format = new String();
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getFormat() {
return format;
}
public void setFormat(String format) {
this.format = format;
}
}
The program will run if you:
1 - Change the line
static ArrayList<Object> items = new ArrayList<Object>();
to
static ArrayList<MediaItem> items = new ArrayList<MediaItem>();
2 - Change the line
System.out.println( items );
to
for ( MediaItem mi : items )
{
System.out.println( mi.getTitle() + ", " + mi.getFormat() );
}
3 - Insert a ";" at the end of the line
format = null
I did it here and it worked.
I either keep having the location where it is saved in memory returned to me
I am guessing you ran into this when you tried to either use System.out.println() to print a MediaItem, or you otherwise tried to automatically convert an object to a string. Whatever approach you took when you were seeing the memory addresses is probably the right way to do it, your problem was only in your displaying of the data.
Consider:
MediaItem item = ...;
System.out.println(item);
By default, Java doesn't know how to convert arbitrary objects to strings when you do stuff like that, and so it just spits out the class name and memory address. You either need to print the fields separately (e.g. Java knows how to display a String already), like:
System.out.println("Title: " + item.getTitle() + " Format: " + item.getFormat());
Or you can override toString() (declared in Object) to provide a custom string conversion:
class MediaItem {
...
#Override public String toString () {
return "Title: " + title + " Format: " + format;
}
}
And then you can print it directly:
System.out.println(item);
It is the default base implementation of Object.toString() that produces those strings with the memory address in them.
Based on your description, I'm guessing you had a roughly working implementation but ran into this issue and ended up changing around (and breaking) a bunch of other unrelated things to try and fix it.
I add objects to my arraylist from a database table like this:
private void fillArray() {
for (People temp : peopleList) {
nameT = temp.getFirstName();
IDT = temp.getUserid();
users.add((new User(IDT, nameT) {
#Override
public String toString() {
return ID + "," + name;
}
}));
}
}
Wanting to display the elements of the objects in the arraList I separate them by overriding toString() and then using Scanner with the delimiter ",".
private void fieldSplitter(String object) {
Scanner inline = new Scanner(object).useDelimiter(",");
IDT = inline.next();
nameT = inline.next();
JOptionPane.showMessageDialog(null, IDT + " " + nameT, "Success", JOptionPane.INFORMATION_MESSAGE);
IDT = null;
nameT = null;
}
This should display all the different elements of each object. All I get is the last entry in my database table displayed every time even though there are 4 other entries.
What have I done wrong?
You should use the getters of your class User to have something cleaner :
users.add((new User(IDT, nameT) {
#Override
public String toString() {
return getId() + "," + getName();
}
}));
I want to convert a string into a date, this is simple. But what I'd like to do it without knowing the date format.
Here is a situation: say I have 100 dates and all are in the same format but I'd like to write a Java program to find out this format for me. The result of this program should give me a list of all the possible formats.
For example:
06-06-2006
06-06-2009
...
06-13-2001 <- 99th record
the result of this will give me date format can be mm-dd-yyyy
If the 99th record also was 06-06-2006 the result should be mm-dd-yyyy and dd-mm-yyyy.
Can someone please help me with an example?
Seems sensible to create a set of formats you know about (DATE_FORMATS) and then test each line to see which formats understand every line. You should end up with a set of possibilities.
public class DateFormatDetector {
private static final Set<String> DATE_FORMATS = new HashSet<String>();
static {
DATE_FORMATS.add("yyyy-MM-dd");
DATE_FORMATS.add("dd-MM-yyyy");
DATE_FORMATS.add("MM-dd-yyyy");
}
public static Set<String> getPossibleDateFormats(List<String> dates) {
Set<SimpleDateFormat> candidates = new HashSet<SimpleDateFormat>();
for (String df : DATE_FORMATS) {
SimpleDateFormat candidate = new SimpleDateFormat(df);
candidate.setLenient(false);
candidates.add(candidate);
}
for (String date : dates) {
Iterator<SimpleDateFormat> it = candidates.iterator();
while (it.hasNext()) {
SimpleDateFormat candidate = it.next();
try {
// try to parse the string as a date
candidate.parse(date);
}
catch (ParseException e) {
// failed to parse, so this format is not suitable
it.remove();
}
}
}
Set<String> results = new HashSet<String>();
for (SimpleDateFormat candidate : candidates)
results.add(candidate.toPattern());
return results;
}
}
Try to use SimpleDateFormat prepare all possible formats and calculate parsed result.
The solution could be functional Java as described for example in the stack overflow