I would need to create java bean class from strings which are read from .docx file. The docx file looks like:
Comments:
20.6.2018 16:18-16:25 problem: first problem, action first action
20.6.2018 16:20-16:45 problem: second problem, action: second action
20.6.2018 16:25-16:30 problem: third problem, action: third action
Check list based on data from 24.6.2018.
I created a CheckList Class to read the docx where I used the instance of FAData.class POJO to set its instant variables according to How to parse a csv file and store datails in java bean class
public class CheckList {
String Check;
ArrayList<String> ActionG = new ArrayList<String>();
ArrayList<String> ProblemG = new ArrayList<String>();
ArrayList<String> DateG = new ArrayList<String>();
ArrayList<String> BeginTimeG = new ArrayList<String>();
ArrayList<String> EndTimeG = new ArrayList<String>();
public void readDocxFile(String fileName) {
FAData data = new FAData(); // instance of FAData POJO
try {
File file = new File(fileName);
FileInputStream fis = new FileInputStream(file.getAbsolutePath());
XWPFDocument document = new XWPFDocument(fis);
List<XWPFParagraph> paragraphs = document.getParagraphs();
String Comments = "";
for (XWPFParagraph para : paragraphs) {
Comments = Comments.concat(para.getText() + "\n");
}
Check = Comments.substring(Comments.lastIndexOf("Check"), Comments.length()); // split the last sentence
String CommentsG = Comments.substring(Comments.indexOf(":") + 1, Comments.lastIndexOf("Check")); // split the sentences between „Comments:” and the last sentence
data.setCheck(Check); // add the Check instant variable to FAData POJO
if (!CommentsG.equals("")) { // check if sentences exist between „Comments:” and the last sentence
// add the substrings to ArrayLists
String[] FAG = CommentsG.split("\n");
for (int i = 1; i < FAG.length; i++) {
ActionG.add(FAG[i].substring(FAG[i].indexOf("action") + 7, FAG[i].length()));
ProblemG.add(FAG[i].substring(FAG[i].indexOf("problem") + 9, FAG[i].indexOf("action") - 2));
DateG.add(FAG[i].substring(0, 10));
BeginTimeG.add(FAG[i].substring(FAG[i].indexOf(":") - 2, FAG[i].indexOf("-")));
EndTimeG.add(FAG[i].substring(FAG[i].indexOf("-") + 1, FAG[i].indexOf("-") + 6));
}
}
// add the ArrayList instant variables to FAData POJO
data.setActionG(ActionG);
data.setProblemG(ProblemG);
data.setDateG(DateG);
data.setBeginTimeG(BeginTimeG);
data.setEndTimeG(EndTimeG);
fis.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
public class FAData {
String Check;
ArrayList<String> ActionG = new ArrayList<String>();
ArrayList<String> ProblemG = new ArrayList<String>();
ArrayList<String> DateG = new ArrayList<String>();
ArrayList<String> BeginTimeG = new ArrayList<String>();
ArrayList<String> EndTimeG = new ArrayList<String>();
public String getCheck() {
return Check;
}
public void setCheck(String Check) {
this.Check = Check;
}
public ArrayList<String> getActionG() {
return ActionG;
}
public void setActionG(ArrayList<String> ActionG) {
this.ActionG = ActionG;
}
....
//Getter and Setter for the rest variables
}
I tested if the variables are available from FAData.class:
public class test {
public static void main(String[] args) {
CheckList TC = new CheckList();
String fileName = "c:\\path\\to\\docx";
try {
TC.readDocxFile(fileName);
FAData datas = new FAData();
String testCheck = datas.getCheck();
ArrayList<String> testDate = datas.getDateG();
System.out.println(testCheck);
System.out.println(testDate);
} catch (Exception e) {
System.out.println(e);
}
}
}
but I got null values. I do not know what I did wrong or how I should thest FAData.class. Could someone give me a suggestion?
Meantime I figured out the solution. I added the List<FAData> datas = new ArrayList<FAData>(); inside readDocxFile method and added to it the data values:
datas.add(data);
If I create a new instant of CheckList object in an other class I can get the data e.g:
CheckList TC = new CheckList();
List<FAData> D = TC.readDocxFile(fileName);
String DateG = D.get(0).getDateG(0);
Related
Tab-Separated File:
2019-06-06 10:00:00 1.0
2019-06-06 11:00:00 2.0
I'd like to iterate over the file once and add the value of each column to a list.
My working approach would be:
import java.util.*;
import java.io.*;
public class Program {
public static void main(String[] args)
{
ArrayList<Double> List_1 = new ArrayList<Double>();
ArrayList<Double> List_2 = new ArrayList<Double>();
String[] values = null;
String fileName = "File.txt";
File file = new File(fileName);
try
{
Scanner inputStream = new Scanner(file);
while (inputStream.hasNextLine()){
try {
String data = inputStream.nextLine();
values = data.split("\\t");
if (values[1] != null && !values[1].isEmpty() == true) {
double val_1 = Double.parseDouble(values[1]);
List_1.add(val_1);
}
if (values[2] != null && !values[2].isEmpty() == true) {
double val_2 = Double.parseDouble(values[2]);
List_2.add(val_2);
}
}
catch (ArrayIndexOutOfBoundsException exception){
}
}
inputStream.close();
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
System.out.println(List_1);
System.out.println(List_2);
}
}
I get:
[1.0]
[2.0]
It doesn't work without the checks for null, ìsEmpty and the ArrayIndexOutOfBoundsException.
I would appreciate any hints on how to save a few lines while keeping the scanner approach.
One option is to create a Map of Lists using column number as a key. This approach gives you "unlimited" number of columns and exactly the same output than one in the question.
public class Program {
public static void main(String[] args) throws Exception
{
Map<Integer, List<Double>> listMap = new TreeMap<Integer, List<Double>>();
String[] values = null;
String fileName = "File.csv";
File file = new File(fileName);
Scanner inputStream = new Scanner(file);
while (inputStream.hasNextLine()){
String data = inputStream.nextLine();
values = data.split("\\t");
for (int column = 1; column < values.length; column++) {
List<Double> list = listMap.get(column);
if (list == null) {
listMap.put(column, list = new ArrayList<Double>());
}
if (!values[column].isEmpty()) {
list.add(Double.parseDouble(values[column]));
}
}
}
inputStream.close();
for(List<Double> list : listMap.values()) {
System.out.println(list);
}
}
}
You can clean up your code some by using try-with resources to open and close the Scanner for you:
try (Scanner inputStream = new Scanner(file))
{
//your code...
}
This is useful because the inputStream will be closed automatically once the try block is left and you will not need to close it manually with inputStream.close();.
Additionally if you really want to "save lines" you can also combine these steps:
double val_2 = Double.parseDouble(values[2]);
List_2.add(val_2);
Into a single step each, since you do not actually use the val_2 anywhere else:
List_2.add(Double.parseDouble(values[2]));
Finally you are also using !values[1].isEmpty() == true which is comparing a boolean value to true. This is typically bad practice and you can reduce it to !values[1].isEmpty() instead which will have the same functionality. Try not to use == with booleans as there is no need.
you can do it like below:
BufferedReader bfr = Files.newBufferedReader(Paths.get("inputFileDir.tsv"));
String line = null;
List<List<String>> listOfLists = new ArrayList<>(100);
while((line = bfr.readLine()) != null) {
String[] cols = line.split("\\t");
List<String> outputList = new ArrayList<>(cols);
//at this line your expected list of cols of each line is ready to use.
listOfLists.add(outputList);
}
As a matter of fact, it is a simple code in java. But because it seems that you are a beginner in java and code like a python programmer, I decided to write a sample code to let you have a good start point. good luck
Hi I'm essentially trying to copy an element of one array into another array. I have the array lineOfData which holds data from a csv file (id, size, custom label) and I want a new array ids that just holds the ids. I have tried ids = lineOfData[0]; but to no avail. what is the best way to do this?
Code:
public class Merge {
String filePath;
public Merge() {
}
public static void main(String[] args) throws FileNotFoundException {
Frame myFrame = new Frame();
FileDialog fileBox = new FileDialog(myFrame, "Open", FileDialog.LOAD);
fileBox.setVisible(true);
String filename = fileBox.getFile();
String directoryPath = fileBox.getDirectory();
if ( filename !=null )
{
String filePath = directoryPath + filename;
//testFile(filePath);
mergeFile(filePath);
}
}
public static void mergeFile(String... filePaths) throws FileNotFoundException {
String[] lineOfData = null;
String[] ids = null;
for(String filePath : filePaths)
{
String path = filePath;
Scanner scanner = new Scanner(new File(path));
String columnTitle = scanner.nextLine();
while(scanner.hasNextLine())
{
//scanner.useDelimiter(",");
String line = scanner.nextLine();
lineOfData = line.split(",");
ids[0] = lineOfData[0];
System.out.println(Arrays.toString(ids));
}
scanner.close();
}
}
You mentioned that you're getting that "The value ids can only be null at this location". This is because you haven't initialized the ids array. Right now, ids isn't pointing to any array. In order to fix this, you
String line = scanner.nextLine();
lineOfData = line.split(",");
ids = new String[lineOfData.length];
ids[0] = lineOfData[0];
if you're going to be adding all the values of lineOfData to ids, or
String line = scanner.nextLine();
lineOfData = line.split(",");
ids = new String[1];
ids[0] = lineOfData[0];
if you only want to copy one element of lineOfData into ids.
Also, notice that lineOfData also starts out by not pointing to any array. However, the lineOfData = line.split(","); line fixes that, and makes it point to an array of Strings. Ask if you have any questions.
Ok, so I have this issue that I can't seem to wrap my head around.
what I'm trying to do, is the following:
read in a CSV file line by line, split it up at the comma and pass it into a hashmap and then carry out some operations.
I'm effectively trying to replicate some of the behaviours of map reduce in java.
Now, what I have so far is:
public class mapper {
public static void main(String[] args) {
//file reading - here.
Scanner filePathInput = new Scanner(System.in);
String filePath = filePathInput.nextLine();
File file = new File(filePath);
if (file.isFile()) {
Scanner fileInput = null;
try {
fileInput = new Scanner(file);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return;
}
ArrayList<String> lineBuffer = new ArrayList<>();
while (fileInput.hasNextLine()) {
String line = fileInput.nextLine();
// char ch = line.charAt(0);
lineBuffer.add(line);
//String[] values = line.split(",");
// Map<String, Integer> reducer = new HashMap<String, Integer>();
// parse the line here
//System.out.println(values);
}
HashMap<String, ArrayList<FlightData>> test = mapper(lineBuffer);
}
}
I then have the mapper to the hash map down:
public static HashMap<String, ArrayList<FlightData>> mapper(ArrayList<String> lineBuffer) {
HashMap<String, ArrayList<FlightData>> mapdata = new HashMap<>();
for (String flightData: lineBuffer) {
String[] str = flightData.split(",");
FlightData flight = new FlightData(str[0], str[1], str[2].toCharArray(),str[3].toCharArray(), new Date(Long.valueOf(str[4])), Long.valueOf(str[5]).longValue());
mapdata.get(flight.getFlightID());
if(mapdata.containsKey(flight.getFlightID())){
mapdata.get(flight.getFlightID()).add(flight);
}
else {
ArrayList<FlightData> noID = new ArrayList<>();
noID.add(flight);
mapdata.put(flight.getFlightID(), noID);
}
}
System.out.println(mapdata);
return mapdata;
}
I have my flight data object defined here with the getters etc:
public class FlightData {
private String passengerID;
private String flightID;
private char[] fromID = new char[3];
private char[] tooID = new char[3];
public Date departTime;
public long flightTimeMins;
public Date arrivalTime;
//Constucter;
public FlightData(String passengerID, String flightID, char[] fromID, char[] tooID, Date departTime, long flightTimeMins) {
setPassengerID(passengerID);
setFlightID(flightID);
setFromID(fromID);
setTooID(tooID);
setFlightTimeMins(flightTimeMins);
setDepartTime(departTime);
setArrivalTime(arrivalTime);
However, I am having an issue is, how do I carry out the validation:
presumably I need to create a class that contains all of my patterns and all of the logic for that right? and just call it when needed?
I have set up a basic class for this:
public class Validation {
public static void validate(String theReg, String str2Check) {
final Pattern PtnPassenger = Pattern.compile(theReg);
final Pattern PtnFlight = Pattern.compile(theReg);
final Pattern PtnFrom = Pattern.compile(theReg);
final Pattern PtnToo = Pattern.compile(theReg);
Matcher regexMatcher = PtnPassenger.matcher(str2Check);
while (regexMatcher.find()) {
if (regexMatcher.group().length() != 0) {
System.out.println(regexMatcher.group().trim());
}
}
}
But, how to I do the following:
set it up so that as each line is read it it checks, it is empty?
set it up so that then, if its not it checks that "cell" against the pattern, moves onto the next and repeats step 1
so for example, each line should contain following comma separated data:
PID, FID, FromID, TooID, time(linux epoch) minutes e.g:
BWI0520BG0, MOO1786A, MAD, FRA, 1420563408, 184
so, for example, for pID I would need a regex like this:
[A-Z]{3}[0-9]{4}[A-Z]{2}[0-9]{1}
but, how do I check each element? should I do this before I pass them into the hash map? or?
Any help would be great.
Cheers
This question already has answers here:
Why does the foreach statement not change the element value?
(6 answers)
Closed 5 years ago.
Im new to Java-programming and I just got an assignment at school I'm struggling a bit with. The code you see below is the only code I'm allowed to edit.
The assignment is to find the word "ADJEKTIV" in a txt file and replace it with a random adjective from another txt document containing only adjectives. This part I think I nailed. But when I try to use the write-method from another class called OutputWriter, it seems like it won't take the new updates to the Strings containing "ADJEKTIV". Do I need to "update" the ArrayList somehow to keep the changes?
import java.util.*;
/**
* Class documentation needed!
*/
public class StoryCreator
{
private InputReader reader;
private OutputWriter writer;
private Random random;
public StoryCreator()
{
reader = new InputReader();
writer = new OutputWriter();
random = new Random();
}
public String randomAdjective(String adjectivesFilename)
{
ArrayList<String> adjectives = reader.getWordsInFile(adjectivesFilename);
int index = random.nextInt(adjectives.size());
return adjectives.get(index);
}
public void createAdjectiveStory(String storyFilename, String adjectivesFilename, String outputFilename)
{
ArrayList<String> story = reader.getWordsInFile(storyFilename);
for(String s : story)
{
if(s.contains("ADJEKTIV."))
{
s = randomAdjective(adjectivesFilename) + ". ";
}
if(s.contains("ADJEKTIV"))
{
s = randomAdjective(adjectivesFilename);
}
}
writer.write(story, outputFilename);
}
}
This is the method from the OutputWriter-class:
public void write(ArrayList<String> output, String filename)
{
try {
FileWriter out = new FileWriter(filename);
for(String word : output) {
out.write(word + " ");
}
out.close();
}
catch(IOException exc) {
System.out.println("Error writing output file: " + exc);
}
}
You are not updating the list with
s = randomAdjective(adjectivesFilename);
You instanciate a new String, this is not the instance in the List anymore.
You need to set that value into the list. For that, you need to keep track of the index and use List.set(int, E) to update the list at a specific place.
The easiest at your level. Change the loop to :
for( int i = 0; i < story.size(); i++){
String s = story.get(i);
if(s.contains("ADJEKTIV."))
{
//replace the value with a new one.
s = randomAdjective(adjectivesFilename) + ". ");
story.set(i, s);
/* OR shorter
story.set(i, randomAdjective(adjectivesFilename) + ". ");
*/
}
...
}
Instead of iterating over the ArrayList, you can also replace all the occurrences of once using replaceAll method.
public void createAdjectiveStory(String storyFilename, String adjectivesFilename, String outputFilename)
{
ArrayList<String> story = reader.getWordsInFile(storyFilename);
story.replaceAll(new UnaryOperator<String>() {
public String apply(String original) {
if(original.contains("ADJEKTIV."))
return randomAdjective(adjectivesFilename) + ". ";
if(original.contains("ADJEKTIV"))
return randomAdjective(adjectivesFilename);
return original;
}
});
writer.write(story, outputFilename);
}
I’m working with java Scanner trying to extract product information from a text file called Inventory.txt.
This file contains data on products in this format:
“Danelectro|Bass|D56BASS-AQUA|336177|395.00Orange|Amplifier|BT1000-H|319578|899.00Planet Waves|Superpicks|1ORD2-5|301075|4.50Korg|X50 Music Synthesizer|X50|241473|735.00Alpine|Alto Sax|AAS143|198490|795.00”
I am trying to parse the strings and add them into an arraylist such that each element in the arraylist would look something like this:
"Danelectro|Bass|D56BASS-AQUA|336177|395.00"
"Orange|Amplifier|BT1000-H|319578|899.00"
"KorPlanet Waves|Superpicks|1ORD2-5|301075|4.50"
"g|X50 Music Synthesizer|X50|241473|735.00"
"Alpine|Alto Sax|AAS143|198490|555.00”
Following is my code:
public class ItemDao {
public ItemDao() {
scanFile();
}
public void scanFile() {
Scanner scanner;
ArrayList <String> content = new ArrayList <String>();
try {
Pattern p1 = Pattern.compile("\\.[0-9]{2}$");
scanner = new Scanner(new File("Inventory.txt"));
while (scanner.hasNext(p1)) {
content.add(scanner.next(p1));
}
for (String item : content) {
System.out.println("Items:" + item);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
When I tested this code I found that the arraylist is empty. Any help would be much appreciated.
java -jar A00123456Lab5.jar
Create an ItemDAO class in a dao package
This class will contain an static inner class which implements Comparator
(DAO = Data Access Object)
You can define a Scanner on a String, and a delimiter.
Since the | is used in regex as OR combinator, you have to mask it with (double)-backslash:
sc = new java.util.Scanner ("Danelectro|Bass|D56BASS-AQUA|336177|395.00");
sc.useDelimiter ("\\|");
String name = sc.next ();
// name: java.lang.String = Danelectro
String typ = sc.next ();
// typ: java.lang.String = Bass
String model = sc.next
// model: java.lang.String = D56BASS-AQUA
int id = sc.nextInt ();
// id: Int = 336177
val d = sc.nextDouble ();
// d: Double = 395.0
I see you're using a pattern, those can come in handy--but I'd just take each line and substring it.
while(scanner.hasNextLine()){
String temp = scanner.nextLine();
while(temp.indexOf("|") != -1){
content.add(temp.substring(temp.indexOf("|"));
temp.substring(temp.indexOf("|")+1);
}
}
Just a thought--might be easier to debug with this way.