File fully populating - java

I have this piece of code that is supposed to insert data in a table and fill a txt file with the same data. However I am finding that the table is being filled with the appropriate 2019 rows but the file only contains 1639 with a [Incomplete last line] message at the bottom. What is causing this?
while(ora_rs.next()){
sql_stmt.executeUpdate("INSERT INTO SCHED_BUNDLES_TEMP_TEST VALUES (" +
ora_rs.getString("BUNDLE")+", " +
ora_rs.getString("DROPPER_ID")+", '" +
ora_rs.getString("SCHED_DT")+"')");
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
Date new_date = (Date)formatter.parse(ora_rs.getString("SCHED_DT"));
SimpleDateFormat newFormat = new SimpleDateFormat("MM/dd/yyyy");
String final_string = newFormat.format(new_date);
out.write(ora_rs.getString("BUNDLE")+"|"+ora_rs.getString("DROPPER_ID")+"|"+final_string+"\n");
ii++;
}

My guess is you have a buffered stream and you are not close()ing or flush()ing the stream which means the end of the file is not being written (as its still in memory)

Related

Parse string description of timezone

I get information about timezone in such string format.
(UTC+02:00) Helsinki, Kyiv, Riga, Sofia, Tallinn, Vilnius
Is it somehow possible to parse it into some TimeZone object in Java with standard library or external one?
Depending how you want to use the TimeZone afterwards you might either create a custom one
String input = "(UTC+02:00) Helsinki, Kyiv, Riga, Sofia, Tallinn, Vilnius";
// assuming the format is always fixed at the beginning
String timeZoneOffset = input.substring(4,10);
TimeZone timeZone = TimeZone.getTimeZone("GMT" + timeZoneOffset);
System.out.println("timeZone = " + timeZone);
output (line wrapped)
timeZone = sun.util.calendar.ZoneInfo[id="GMT+02:00",offset=7200000,dstSavings=0,\
useDaylight=false,transitions=0,lastRule=null]
You might get into trouble related to the daytime savings.
Or you create a lookup map with an entry for each offset (stripped down code snipped)
String input = "(UTC+02:00) Helsinki, Kyiv, Riga, Sofia, Tallinn, Vilnius";
// assuming the format is always fixed at the beginning
String timeZoneOffset = input.substring(4,10);
// needs to be initialized somewhere
Map<String, TimeZone> timeZones = new HashMap<>();
// you need to add all offsets
timeZones.put("+02:00", TimeZone.getTimeZone("EET"));
System.out.println("timeZone lookup = " + timeZones.get(timeZoneOffset));
output (line wrapped)
timeZone lookup = sun.util.calendar.ZoneInfo[id="EET",offset=7200000,dstSavings=3600000,\
useDaylight=true,transitions=123,lastRule=java.util.SimpleTimeZone[id=EET,offset=7200000,\
dstSavings=3600000,useDaylight=true,startYear=0,startMode=2,startMonth=2,\
startDay=-,startDayOfWeek=1,startTime=3600000,startTimeMode=2,endMode=2,\
endMonth=9,endDay=-1,endDayOfWeek=1,endTime=3600000,endTimeMode=2]]

Am i doing it in right wat? predict stock price

I prepared csv file with the input data for neural network, and csv file where i can test my neural network. The results are not satisfactory. I was trying increase/decrease size of input data. Probably i missing something and i would be glad if someone can some tips etc. Here is my encog code:
//input data
File file = new File("path to file");
CSVFormat format = new CSVFormat('.', ',');
VersatileDataSource source = new CSVDataSource(file, false, format);
VersatileMLDataSet data = new VersatileMLDataSet(source);
data.getNormHelper().setFormat(format);
ColumnDefinition wig20OpenN = data.defineSourceColumn("wig20OpenN", 0, ColumnType.continuous);
(...)
ColumnDefinition futureClose = data.defineSourceColumn("futureClose", 81, ColumnType.continuous);
data.analyze();
data.defineSingleOutputOthersInput(futureClose);
EncogModel model = new EncogModel(data);
//TYPE_RBFNETWORK, TYPE_SVM, TYPE_NEAT, TYPE_FEEDFORWARD <- this type of method i was trying
model.selectMethod(data, MLMethodFactory.TYPE_SVM);
model.setReport(new ConsoleStatusReportable());
data.normalize();
model.holdBackValidation(0.001, true, 10);
model.selectTrainingType(data);
MLRegression bestMethod = (MLRegression)model.crossvalidate(20, true);
// Display the training and validation errors.
System.out.println( "Training error: " + model.calculateError(bestMethod, model.getTrainingDataset()));
System.out.println( "Validation error: " + model.calculateError(bestMethod, model.getValidationDataset()));
NormalizationHelper helper = data.getNormHelper();
File testingData = new File("path to testing file");
ReadCSV csv = new ReadCSV(testingData, false, format);
String[] line = new String[81];
MLData input = helper.allocateInputVector();
while(csv.next()) {
StringBuilder result = new StringBuilder();
for(int i = 0; i <81; i++){
line[i] = csv.get(i);
}
String correct = csv.get(81);
helper.normalizeInputVector(line,input.getData(),false);
MLData output = bestMethod.compute(input);
String irisChosen = helper.denormalizeOutputVectorToString(output)[0];
result.append(Arrays.toString(line));
result.append(" -> predicted: ");
result.append(irisChosen);
result.append("(correct: ");
result.append(correct);
result.append(")");
System.out.println(result.toString());
}
// Delete data file and shut down.
filename.delete();
Encog.getInstance().shutdown();
What i was trying so far is to change the MLMethodFactory, but had problems here, only TYPE_RBFNETWORK, TYPE_SVM, TYPE_NEAT, TYPE_FEEDFORWARD this type works fine, for example if i changed it to TYPE_PNN i had following exception:
Exception in thread "main" org.encog.EncogError: Please call selectTraining first to choose how to train.
Ok i know from documentation that i should use this method:
selectTraining(VersatileMLDataSet dataset, String trainingType, String trainingArgs)
But the string type for traningtype and triningArgs is confusing.
And last question what about saving the neural after traning to file, and loading it to check on the traning data? As i would like to have this separately.
Edit: I forgot the size of the input data is 1500.
I see that you not satisfied with your results, but it is relatively fine. I propose you to consider adding scaling to your training. You have 81 columns, and in your input row I see data like 16519.1600, also 2315.94, and even -0.6388282285709328. For neural network it is hard to adjust weights correctly for such different inputs.
P.S. scaling is also normalizing of columns!. As usually in books is described normalizing of rows, but normalizing of columns is also important.

error during grouping files based on the date field

I have a large file which has 10,000 rows and each row has a date appended at the end. All the fields in a row are tab separated. There are 10 dates available and those 10 dates have randomly been assigned to all the 10,000 rows. I am now writing a java code to write all those rows with the same date into a separate file where each file has the corresponding rows with that date.
I am trying to do it using string manipulations, but when I am trying to sort the rows based on date, I am getting an error while mentioning the date and the error says the literal is out of range. Here is the code that I used. Please have a look at it let me know if this is the right approach, if not, kindly suggest a better approach. I tried changing the datatype to Long, but still the same error. The row in the file looks something like this:
Each field is tab separated and the fields are:
business id, category, city, biz.name, longitude, state, latitude, type, date
**
qarobAbxGSHI7ygf1f7a_Q ["Sandwiches","Restaurants"] Gilbert Jersey
Mike's Subs -111.8120071 AZ 3.5 33.3788385 business 06012010
**
The code is:
File f=new File(fn);
if(f.exists() && f.length()>0)
{
BufferedReader br=new BufferedReader(new FileReader(fn));
BufferedWriter bw = new BufferedWriter(new FileWriter("FilteredDate.txt"));
String s=null;
while((s=br.readLine())!=null){
String[] st=s.split("\t");
if(Integer.parseInt(st[13])==06012010){
Thanks a lot for your time..
Try this,
List<String> sampleList = new ArrayList<String>();
sampleList.add("06012012");
sampleList.add("06012013");
sampleList.add("06012014");
sampleList.add("06012015");
//
//
String[] sampleArray = s.split(" ");
if (sampleArray != null)
{
String sample = sampleArray[sampleArray.length - 1];
if (sampleList.contains(sample))
{
stringBuilder.append(sample + "\n");
}
}
i suggest not to use split, but rather use
String str = s.subtring(s.lastIndexOf('\t'));
in any case, you try to take st[13] when i see you only have 9 columns. might be you just need st[8]
one last thing, look at this post to learn what 06012010 really means

Java Rename file - always append same info

i use this simple code to rename a file when an event happens:
String newFileName = oldFileName + "_" + new Date().getTime();
if the event happens more and more time i will have a string like:
myfile_1372933712717_1372933715279_1372933716234
while i would like to have only the last timestamp...
Of course i could do a substring to remove the string after "_" and replace it with the new timestamp, but let's suppose i will have a file like: myfile_mycomment...mycomment will be replaced and it's not a good thing...
So how could i recognize if there is already a filestamp in the name of the file?!?
You can try to approach this with RegEx, as the timestamps will always have the same pattern. By this, you can differ between comments and timestamps and remove only the timestamps.
This code
String test = "Hallo_Comment_1372933712717_1372933712717";
test = test.replaceAll("_1[0-9_]{12}", "");
System.out.println(test);
generated this output
Hallo_Comment
Assuming your original file name does'nt contains "_"
Before appending split file name with "_" and get always the 0th element from the string array and append the timestamp

Writing in excel file with poi

I am getting a phone number from one excel file and write into another excel file using the following code
cellph = row.getCell(3);
Object phone = cellph.getNumericCellValue();
String strphone = phone.toString();
cellfour.setCellType(cellfour.CELL_TYPE_STRING);
cellfour.setCellValue("0"+strphone);
It writes the phone number as 09.8546586. I want to write it as 098546586(without precision value). How to do that?
Your problem isn't with the write. Your problem is with the read, that's what's giving you the floating point number
From your code and description, it looks like your phone numbers are stored in Excel as number cells, with an integer format applied to it. That means that when you retrieve the cell, you'll get a double number, and a cell format that tells you how to format it like Excel does.
I think what you probably want to do is something more like:
DataFormatter formatter = new DataFormatter();
cellph = row.getCell(3);
String strphone = "(none available)";
if (cellph.getCellType() == Cell.CELL_TYPE_NUMERIC) {
// Number with a format
strphone = "0" + formatter.formatCellValue(cellph);
}
if (cellph.getCellType() == Cell.CELL_TYPE_STRING) {
// String, eg they typed ' before the number
strphone = "0" + cellph.getStringCellValue();
}
// For all other types, we'll show the none available message
cellfour.setCellType(cellfour.CELL_TYPE_STRING);
cellfour.setCellValue(strphone);

Categories