Java text not formatting properly - java

I just can't seem to figure out how to get java to format my output properly. The code snipet for output is: System.out.format("\n %1s%20s%25s", intDf.format(i), df.format(monthlyPayment), df.format(totalPayment)); the output appears like this:
Interest Rate Monthly Payment Total Payment
5 188.71 11322.74
5.125 189.29 11357.13
5.25 189.86 11391.59
5.375 190.44 11426.11
5.5 191.01 11460.7
5.625 191.59 11495.35
5.75 192.17 11530.06
5.875 192.75 11564.84
6 193.33 11599.68
6.125 193.91 11634.59
6.25 194.49 11669.56
6.375 195.08 11704.59
6.5 195.66 11739.69
6.625 196.25 11774.85
6.75 196.83 11810.08
6.875 197.42 11845.37
7 198.01 11880.72
7.125 198.6 11916.14
7.25 199.19 11951.62
7.375 199.79 11987.16
7.5 200.38 12022.77
7.625 200.97 12058.44
7.75 201.57 12094.18
7.875 202.17 12129.97
8 202.76 12165.84
I want all the values to line up at the first letter in their corresponding line. Any help is appreciated, thanks!

Clearly your first column is more then one character wide. So when you say
System.out.format("\n %1s
That throws it off. The column name "Interest Rate" is 13 characters. So try
System.out.format("\n %14s
Edit
Based on your comment, I would use a DecimalFormat and a NumberFormat like
String[] headings = { "Interest Rate", "Monthly Payment",
"Total Payment" };
for (String heading : headings) {
System.out.print(heading);
System.out.print("\t");
}
System.out.println();
double[] interestRates = { 5, 5.125 };
double[] monthlyPayments = { 188.71, 189.29 };
double[] totalPayments = { 11322.74, 11357.13 };
NumberFormat interestFormat = new DecimalFormat("#.000'%'");
NumberFormat currencyFormat = NumberFormat.getCurrencyInstance();
for (int i = 0; i < interestRates.length; i++) {
System.out.printf("%-13s\t%-15s\t%-13s%n",
interestFormat.format(interestRates[i]),
currencyFormat.format(monthlyPayments[i]),
currencyFormat.format(totalPayments[i]));
}
Output is
Interest Rate Monthly Payment Total Payment
5.000% $188.71 $11,322.74
5.125% $189.29 $11,357.13

I changed it from
System.out.format("\n %1s%20s%25s", intDf.format(i), df.format(monthlyPayment), df.format(totalPayment));
to System.out.format("\n %-5s%20s%25s", intDf.format(i), df.format(monthlyPayment), df.format(totalPayment));
then changed the DecimalFormat to: "'$'0.00"

Related

Apply LOOCV in java splitting with a specific condition

I have a csv file containing 24231 rows. I would like to apply LOOCV based on the project name instead of the observations of the whole dataset.
So if my dataset contains information for 15 projects, I would like to have the training set based on 14 projects and the test set based on the other project.
I was relying on weka's API, is there anything that automates this process?
For non-numeric attributes, Weka allows you to retrieve the unique values via Attribute.numValues() (how many are there) and Attribute.value(int) (the -th value).
package weka;
import weka.core.Attribute;
import weka.core.Instance;
import weka.core.Instances;
import weka.core.converters.ConverterUtils;
public class LOOByValue {
/**
* 1st arg: ARFF file to load
* 2nd arg: 0-based index in ARFF to use for class
* 3rd arg: 0-based index in ARFF to use for LOO
*
* #param args the command-line arguments
* #throws Exception if loading/processing of data fails
*/
public static void main(String[] args) throws Exception {
// load data
Instances full = ConverterUtils.DataSource.read(args[0]);
full.setClassIndex(Integer.parseInt(args[1]));
int looCol = Integer.parseInt(args[2]);
Attribute looAtt = full.attribute(looCol);
if (looAtt.isNumeric())
throw new IllegalStateException("Attribute cannot be numeric!");
// iterate unique values to create train/test splits
for (int i = 0; i < looAtt.numValues(); i++) {
String value = looAtt.value(i);
System.out.println("\n" + (i+1) + "/" + full.attribute(looCol).numValues() + ": " + value);
Instances train = new Instances(full, full.numInstances());
Instances test = new Instances(full, full.numInstances());
for (int n = 0; n < full.numInstances(); n++) {
Instance inst = full.instance(n);
if (inst.stringValue(looCol).equals(value))
test.add((Instance) inst.copy());
else
train.add((Instance) inst.copy());
}
train.compactify();
test.compactify();
// TODO do something with the data
System.out.println("train size: " + train.numInstances());
System.out.println("test size: " + test.numInstances());
}
}
}
With Weka's anneal UCI dataset and the surface-quality for leave-one-out, you can generate something like this:
1/5: ?
train size: 654
test size: 244
2/5: D
train size: 843
test size: 55
3/5: E
train size: 588
test size: 310
4/5: F
train size: 838
test size: 60
5/5: G
train size: 669
test size: 229

Retrive textcontent by matching start word and end word

I am getting text file with contents like below. I want to retrieve the data present between start_word=Tax% and end_word="ErrorMessage".
ParsedText:
Tax%
63 2 .90 0.00 D INTENS SH 80ML(48) 9.00% 9.00%
23 34013090 0.0 DS PURE WHIT 1 COG (24) 9.00% 9.00%
"ErrorMessage":"","ErrorDetails":""
After retreiving the output would be
63 2 .90 0.00 D INTENS SH 80ML(48) 9.00% 9.00%
23 34013090 0.0 DS PURE WHIT 1 COG (24) 9.00% 9.00%
Please help.
I am using camel to read the text then i want to retrive the data to process further as per my requiement.
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
public class DataExtractor implements Processor{
#Override
public void process(Exchange exchange) throws Exception {
String textContent=(String) exchange.getIn().getBody();
System.out.println("TextContents >>>>>>"+textContent);
}
}
In the text content I am getting the content that i have given above.I need help regarding retreiving the the data in java.
Below is the code snippet to extract the desired output:
String[] strArr = textContent.split("\\r?\\n");
StringBuilder stringBuilder = new StringBuilder();
boolean appendLines = false;
for(String strLines : strArr) {
if(strLines.contains("Tax%")) {
appendLines = true;
continue;
}
if(strLines.contains("\"ErrorMessage\"")) {
break;
}
if(appendLines){
stringBuilder.append(strLines);
stringBuilder.append(System.getProperty("line.separator"));
}
}
textContent = stringBuilder.toString();

Trained neural network outputs the same results for all evaluation rows

There seems to be no problem when training my network because it converges and falls below 0.01 error. However when I load my trained network, and introduce the evaluation set, it outputs the same results for all the evaluation set rows (the actual prediction, not the training phase). I trained my network with resilient propagation with 9 inputs, 1 hidden layer with 7 hidden neurons and 1 output neuron. UPDATE: My data is normalized using min-max. i am trying to predict an electric load data.
Here is the sample data, first 9 rows are the inputs while the 10th is the ideal value:
0.5386671932975533, 1100000.0, 0.0, 1.0, 40.0, 1.0, 30.0, 9.0, 2014.0 , 0.5260616667545941
0.5260616667545941, 1100000.0, 0.0, 1.0, 40.0, 2.0, 30.0, 9.0, 2014.0, 0.5196499668339777
0.5196499668339777, 1100000.0, 0.0, 1.0, 40.0, 3.0, 30.0, 9.0, 2014.0, 0.5083828048375548
0.5083828048375548, 1100000.0, 0.0, 1.0, 40.0, 4.0, 30.0, 9.0, 2014.0, 0.49985462144799725
0.49985462144799725, 1100000.0, 0.0, 1.0, 40.0, 5.0, 30.0, 9.0, 2014.0, 0.49085956670499675
0.49085956670499675, 1100000.0, 0.0, 1.0, 40.0, 6.0, 30.0, 9.0, 2014.0, 0.485008112408512
Here's the full code:
public class ANN
{
//training
//public final static String SQL = "SELECT load_input, day_of_week, weekend_day, type_of_day, week_num, time, day_date, month, year, ideal_value FROM sample WHERE (year,month,day_date,time) between (2012,4,1,1) and (2014,9,29, 96) ORDER BY ID";
//testing
public final static String SQL = "SELECT load_input, day_of_week, weekend_day, type_of_day, week_num, time, day_date, month, year, ideal_value FROM sample WHERE (year,month,day_date,time) between (2014,9,30,1) and (2014,9,30, 92) ORDER BY ID";
//validation
//public final static String SQL = "SELECT load_input, day_of_week, weekend_day, type_of_day, week_num, time, day_date, month, year, ideal_value FROM sample WHERE (year,month,day_date,time) between (2014,9,30,93) and (2014,9,30, 96) ORDER BY ID";
public final static int INPUT_SIZE = 9;
public final static int IDEAL_SIZE = 1;
public final static String SQL_DRIVER = "org.postgresql.Driver";
public final static String SQL_URL = "jdbc:postgresql://localhost/ANN";
public final static String SQL_UID = "postgres";
public final static String SQL_PWD = "";
public static void main(String args[])
{
Mynetwork();
//train network. will add customizable params later.
//train(trainingData());
//evaluate network
evaluate(trainingData());
Encog.getInstance().shutdown();
}
public static void evaluate(MLDataSet testSet)
{
BasicNetwork network = (BasicNetwork)EncogDirectoryPersistence.loadObject(new File("directory"));
// test the neural network
System.out.println("Neural Network Results:");
for(MLDataPair pair: testSet ) {
final MLData output = network.compute(pair.getInput());
System.out.println(pair.getInput().getData(0) + "," + pair.getInput().getData(1) + "," + pair.getInput().getData(2) + "," + pair.getInput().getData(3) + "," + pair.getInput().getData(4) + "," + pair.getInput().getData(5) + "," + pair.getInput().getData(6) + "," + pair.getInput().getData(7) + "," + pair.getInput().getData(8) + "," + "Predicted=" + output.getData(0) + ", Actual=" + pair.getIdeal().getData(0));
}
}
public static BasicNetwork Mynetwork()
{
//basic neural network template. Inputs should'nt have activation functions
//because it affects data coming from the previous layer and there is no previous layer before the input.
BasicNetwork network = new BasicNetwork();
//input layer with 2 neurons.
//The 'true' parameter means that it should have a bias neuron. Bias neuron affects the next layer.
network.addLayer(new BasicLayer(null , true, 9));
//hidden layer with 3 neurons
network.addLayer(new BasicLayer(new ActivationSigmoid(), true, 5));
//output layer with 1 neuron
network.addLayer(new BasicLayer(new ActivationSigmoid(), false, 1));
network.getStructure().finalizeStructure() ;
network.reset();
return network;
}
public static void train(MLDataSet trainingSet)
{
//Backpropagation(network, dataset, learning rate, momentum)
//final Backpropagation train = new Backpropagation(Mynetwork(), trainingSet, 0.1, 0.9);
final ResilientPropagation train = new ResilientPropagation(Mynetwork(), trainingSet);
//final QuickPropagation train = new QuickPropagation(Mynetwork(), trainingSet, 0.9);
int epoch = 1;
do {
train.iteration();
System.out.println("Epoch #" + epoch + " Error:" + train.getError());
epoch++;
} while((train.getError() > 0.01));
System.out.println("Saving network");
System.out.println("Saving Done");
EncogDirectoryPersistence.saveObject(new File("directory"), Mynetwork());
}
public static MLDataSet trainingData()
{
MLDataSet trainingSet = new SQLNeuralDataSet(
ANN.SQL,
ANN.INPUT_SIZE,
ANN.IDEAL_SIZE,
ANN.SQL_DRIVER,
ANN.SQL_URL,
ANN.SQL_UID,
ANN.SQL_PWD);
return trainingSet;
}
}
Here is my result:
Predicted=0.4451817588640455, Actual=0.5260616667545941
Predicted=0.4451817588640455, Actual=0.5196499668339777
Predicted=0.4451817588640455, Actual=0.5083828048375548
Predicted=0.4451817588640455, Actual=0.49985462144799725
Predicted=0.4451817588640455, Actual=0.49085956670499675
Predicted=0.4451817588640455, Actual=0.485008112408512
Predicted=0.4451817588640455, Actual=0.47800504210686795
Predicted=0.4451817588640455, Actual=0.4693212349328293
(...and so on with the same "predicted")
Results im expecting (I changed the "predicted" with something random for demonstration purposes, indicating that the network is actually predicting):
Predicted=0.4451817588640455, Actual=0.5260616667545941
Predicted=0.5123312331212122, Actual=0.5196499668339777
Predicted=0.435234234234254365, Actual=0.5083828048375548
Predicted=0.673424556563455, Actual=0.49985462144799725
Predicted=0.2344673345345544235, Actual=0.49085956670499675
Predicted=0.123346457544324, Actual=0.485008112408512
Predicted=0.5673452342342342, Actual=0.47800504210686795
Predicted=0.678435234423423423, Actual=0.4693212349328293
The first reason to consider when you get weird results with neural networks is normalization. Your data must be normalized, otherwise, yes, the training will result in skewed NN which will produce the same outcome all the time, it is a common symptom.
Always normalize your data before feeding it into a neural network. This is important because if you consider the sigmoid activation function it is basically flat for larger values (positive and negative), resulting in a constant behavior of your neural net. Try normalizing as such input = (input-median(input)) / std(input)

Mainframe comp-3 field reading using JRecord

I am trying to read mainframe file but all are working other than comp 3 file.Below program is giving strange values.It is not able to read the salary value which is double also it is giving 2020202020.20 values. I don't know what am missing.Please help me to find it.
Program:
public final class Readcopybook {
private String dataFile = "EMPFILE.txt";
private String copybookName = "EMPCOPYBOOK.txt";
public Readcopybook() {
super();
AbstractLine line;
try {
ICobolIOBuilder iob = JRecordInterface1.COBOL.newIOBuilder(copybookName)
.setFileOrganization(Constants.IO_BINARY_IBM_4680).setSplitCopybook(CopybookLoader.SPLIT_NONE);
AbstractLineReader reader = iob.newReader(dataFile);
while ((line = reader.read()) != null) {
System.out.println(line.getFieldValue("EMP-NO").asString() + " "
+ line.getFieldValue("EMP-NAME").asString() + " "
+ line.getFieldValue("EMP-ADDRESS").asString() + " "
+ line.getFieldValue("EMP-SALARY").asString() + " "
+ line.getFieldValue("EMP-ZIPCODE").asString());
}
reader.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new Readcopybook();
}
}
EMPCOPYBOOK:
001700 01 EMP-RECORD.
001900 10 EMP-NO PIC 9(10).
002000 10 EMP-NAME PIC X(30).
002100 10 EMP-ADDRESS PIC X(30).
002200 10 EMP-SALARY PIC S9(8)V9(2) COMP-3.
002200 10 EMP-ZIPCODE PIC 9(4).
EMPFILE:
0000001001suneel kumar r bangalore e¡5671
0000001002JOSEPH WHITE FIELD rrn4500
Output:
1001 suneel kumar r bangalore 20200165a10 5671
2020202020.20
2020202020.20
2020202020.20
2020202020.20
2020202020.20
2020202020.20
2020202020.20
2020202020.20
0.00
1002 JOSEPH WHITE FIELD 202072726e0 4500
One problem is you have done a Ebcdic to Ascii conversion on the file.
The 2020... is a dead give away x'20' is the ascii space character.
This Answer deals with problems with doing an Ebcdic to ascii conversion.
You need to do a Binary transfer from the Mainframe and read the file using Ebcdic. You will need to check the RECFM on the Mainframe. If the RECFM is
FB - problems just transfer
VB - either convert to FB on the mainframe of include the RDW (Record Descriptor Word) option in the transfer.
Other - Convert to FB/VB on the mainframe
Updated java Code
int fileOrg = Constants.IO_FIXED_LENGTH_RECORDS; // or Constants.IO_VB
ICobolIOBuilder iob = JRecordInterface1.COBOL
.newIOBuilder(copybookName)
.setFileOrganization(fileOrg)
.setFont("Cp037")
.setSplitCopybook(CopybookLoader.SPLIT_NONE);
Note: IO_BINARY_IBM_4680 is for IBM 4690 Registers
There is a wiki entry here
or this Question
How do you generate java~jrecord code fror a Cobol copybook

Java Program Runs in One Machine and not on Another

I'm studying Java and I just wrote a program that turns CME quotes of soybeans, wheat or corn to "price per 60kg bag" in BRL(Brazilian Currency). I used JSmooth to wrap it and it runs perfectly on my machine. I tried sending it to my wife's PC in a Zip file and I also tried to execute it from the command line directly calling the Main class. In both cases, the program goes until the part where it asks the USD/BRL quote and after one enters it and presses "Enter", the program shows an aparent Runtime error:
Options: soybean / wheat / corn
Type name of commodity(lowercase):
soybean
Type current exchange rate(USD/BRL):
3.11
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextDouble(Unknown Source)
at PriceCME.getExchange_Rate(PriceCME.java:26)
at PriceCMEExecute.main(PriceCMEExecute.java:8)
I thought that maybe the version of Java on her pc was outdated, so I removed all previous versions, installed the JDK 111 and tried to run it again.
The same issue happened.
I then tried to recompile the .java files on her PC and there was no compile time error. When I tried to execute it, again the same issue.
The code is the following:
import java.util.Scanner;
public class PriceCME {
private String nameOfCommodity;
private final double BUSHEL_SOY_WHEAT = 27.2155;
private final double BUSHEL_CORN = 25.4012;
private final int KGPERSACA = 60;
private double quote;
private double exchRate;
private double pricePerSaca;
public void getNameOfComodity()
{
System.out.println("Options: soybean / wheat / corn");
System.out.print("Type name of commodity(lowercase): ");
System.out.println();
Scanner commodity = new Scanner(System.in);
nameOfCommodity = commodity.next();
}
public void getExchange_Rate()
{
System.out.print("Type current exchange rate(USD/BRL): ");
System.out.println();
Scanner exchangeRate = new Scanner(System.in);
exchRate = exchangeRate.nextDouble();
}
public void getQuote()
{
System.out.println("Source for quotes:
http://www.cmegroup.com/trading/agricultural/");
System.out.print("Type quote of commodity on CME: ");
System.out.println();
Scanner getQuote = new Scanner(System.in);
quote = getQuote.nextDouble();
}
public void CalculatePricePerSaca()
{
switch(nameOfCommodity)
{
case "soybean":
pricePerSaca = (((quote * KGPERSACA) / 100) /
BUSHEL_SOY_WHEAT) * exchRate;
break;
case "wheat":
pricePerSaca = (((quote * KGPERSACA) / 100) /
BUSHEL_SOY_WHEAT) * exchRate;
break;
case "corn":
pricePerSaca = (((quote * KGPERSACA) / 100) /
BUSHEL_CORN) * exchRate;
break;
}
}
public void getPricePerSaca()
{
System.out.printf("The price of %s \"Por Saca\" is:\nR$ %.2f ",
nameOfCommodity, pricePerSaca);
System.out.println();
System.out.println("Type \"false\" to exit.");
Scanner end = new Scanner(System.in);
boolean theEnd = end.hasNext();
}
}
public class PriceCMEExecute {
public static void main(String[] args)
{
PriceCME myPriceCME = new PriceCME();
myPriceCME.getNameOfComodity();
myPriceCME.getExchange_Rate();
myPriceCME.getQuote();
myPriceCME.CalculatePricePerSaca();
myPriceCME.getPricePerSaca();
}
}
How can this be happening? Would it be a problem on the JVM on her machine?

Categories

Resources