How can i split text file using string.endwith("Page 1") and store in arraylist?
Can anyone help me to do this ?
Following thing I tried to do this.
while (readerSplit.hasNextLine()) {
String sLineDataSplit = readerSplit.nextLine();
if (!sLineDataSplit.endsWith("Page 1")) {
sbSplitData.append(sLineDataSplit);
sbSplitData.append(newline);
} else {
sbSplitData.append(sLineDataSplit);
sbSplitData.append(newline);
if (sbSplitData.length() > 100) {
System.out.println("Inside");
stringList.add(i, sbSplitData);
System.out.println(sbSplitData);
i++;
sbSplitData.setLength(0);
}
System.out.println("Outside");
System.out.println(sbSplitData);
}
}
Here is some text from my file.
007 Date 1/04/13 Page 1
1 Account Number 1000000000
1 Enclosures
013 Test S.A.(Test)
1 URB. MARBELLA CALLE AQUILINO DE LA
1 GUARDIA CON CALLE 47 TORRE BANESCO
1 PISO 27 Y 28 CIUDAD DE PANAMA
1 PANAMA
024 As of 1/1/2013, funds in a noninterest-bearing transaction account (including an
1 IOLTA) will no longer receive unlimited deposit insurance coverage. They will be
1 insured to the legal maximum of $250,000 for each ownership category.
2 ................................ ACCOUNT DETAIL ...............................
0 ................................ ACCOUNT DETAIL ...............................
2 CHECKING ACCOUNT FRG AFFL BANK
0 CHECKING ACCOUNT FRG AFFL BANK Number of Enclosures 0
1 Account Number 1000007904 Statement Dates 1/04/13 thru 1/06/13
1 Previous Balance 50,226.18 Days in the statement period 3
1 10 Deposits/Credits 338,888.67 Average Ledger 50,768.40
1 30 Checks/Debits 338,246.00 Average Collected 50,768.40
1 Total Fees .00
1 Interest Paid .00
1 Current Balance 50,868.85
3 Deposits and Additions
0 Deposits and Additions
1 Date Description Amount Refe
0 Date Description Amount Refe
1 1/04 WIRE-IN 20130040042300 ATLANTI 300,000.00
1 OBI:
1 1/04 WIRE-IN 20130040038800 1/CARVA 4,295.00
1 OBI:
1 1/04 WIRE-IN 20130030004900 NEW CHA 1,563.67
1 OBI:PENSION MES DE DICIEMBRE D
1 1/04 INTERNAL TRF 20130040011800 13,995.00
007 Date 1/04/13 Page 2
1 Account Number 1000000000
1 Enclosures
024 CHECKING ACCOUNT FRG AFFL BANK 1000007904 (Continued)
2 Checks and Withdrawals
0 Checks and Withdrawals
1 Date Description Amount Refe
0 Date Description Amount Refe
1 1/04 Transfer to DDA 292,000.00-
1 Acct No. 1000007896-D
1 1/04 WIRE-IN 20130040024900 CARLOS 5,000.00-
1 OBI:REINTEGRO NESTMARY
1 1/04 WIRE-IN 20130040025100 ELIZABE 5,000.00-
1 OBI:PAGO POR ACTIVIDADES DEPOR
1 1/04 WIRE-IN 20130040027100 COOPERA 2,622.00-
1 OBI:PAGO
1 1/04 WIRE-IN 20130040025900 INGENIA 2,550.00-
1 OBI:PRESTAMO EFECTIVO
1 1/04 WIRE-IN 20130040013100 JULIANO 2,500.00-
1 OBI:
1 1/04 WIRE-IN 20130040013000 MARIA I 2,089.57-
1 OBI:
1 1/04 WIRE-IN 20130040013300 JESUS A 2,012.00-
1 1/04 WIRE-IN 20130040018000 JOSE FR 200.00-
1 OBI:
1 1/04 WIRE-IN 20130040027800 EVELYN 100.00-
1 OBI:
2 Daily Balance Information
0 Daily Balance Information
1 Date Balance
0 Date Balance
1 1/04 50,868.85
2 For more information about FDIC insurance coverage, visit
1 http://www.fdic.gov/deposit/deposits/unlimited/expiration.html
007 Date 1/04/13 Page 1
1 Account Number 1000000000
1 Enclosures 25
013 Test
1 7A AVENIDA 11-59 ZONA 9
1 EDIFICIO GALERIAS ESPAÑA
1 GUATEMALA CITY
1 GUATEMALA
024 As of 1/1/2013, funds in a noninterest-bearing transaction account (including an
1 IOLTA) will no longer receive unlimited deposit insurance coverage. They will be
1 insured to the legal maximum of $250,000 for each ownership category.
2 ................................ ACCOUNT DETAIL ...............................
0 ................................ ACCOUNT DETAIL ...............................
2 FINANCIAL INSTITUTIONS
0 FINANCIAL INSTITUTIONS Number of Enclosures 25
1 Account Number 1000059038 Statement Dates 1/01/13 thru 1/06/13
1 Previous Balance 2,290,044.74 Days in the statement period 6
1 7 Deposits/Credits 952,853.71 Average Ledger 1,836,411.23
1 33 Checks/Debits 1,522,979.73 Average Collected 1,836,411.23
1 Total Fees .00
1 Interest Paid .00
1 Current Balance 1,719,918.72
3 Deposits and Additions
0 Deposits and Additions
1 Date Description Amount Refe
0 Date Description Amount Refe
1 1/02 WIRE-IN 20130020001800 ONMOBIL 2,500.00
1 OBI:FOR THE FINAL CREDIT OF AN
1 1/02 WIRE-IN 20130020009400 VIGO RE 114.00
1 OBI:VIGO SETTLEMENT GTV14
1 1/03 WIRE-IN 20130030030100 BANCO I 800,000.00
1 OBI:TRASLADO DE FONDOS DE SU C
1 1/03 WIRE-IN 20130030013700 MR BEYN 1,324.90
1 OBI:C/C 17026009260 ANA ANGELI
1 1/03 Remote Deposit Capture Deposit 70,725.00
1 1/04 WIRE-IN 20130040010500 VIGO RE 10,826.73
1 OBI:VIGO SETTLEMENT GTV14
1 1/04 Remote Deposit Capture Deposit 67,363.08
2 Checks and Withdrawals
0 Checks and Withdrawals
1 Date Description Amount Refe
0 Date Description Amount Refe
1 1/02 WIRE-IN 20130020023200 SAN LAZ 394,020.00-
1 OBI:/RFB/ F/F/C TO ACCOUNT 569
1 1/02 WIRE-IN 20130020029300 UNISOUR 22,589.50-
1 OBI:/RFB/UNIVERSITY EXPENSES S
1 1/02 WIRE-IN 20130020029700 UNISOUR 10,020.00-
1 OBI:/RFB/DEPOSIT TO ACCOUNT
1 1/02 7367* 17,000.00 1/03 7397 15,000.00
1 1/04 7370* 100,000.00 1/04 7403* 20,000.00
1 1/03 7371 20,000.00
1 * Denotes missing check numbers
2 Daily Balance Information
0 Daily Balance Information
1 Date Balance
0 Date Balance Date Balance
0 Date Balance
1 1/01 2,290,044.74 1/03 1,816,672.91
1 1/02 1,751,993.61 1/04 1,719,918.72
2 For more information about FDIC insurance coverage, visit
1 http://www.fdic.gov/deposit/deposits/unlimited/expiration.html
007 Date 1/04/13 Page 1
1 Account Number 1000000000
1 Enclosures
013 Test
1 (SETTELMENT VISA ACCOUNT)
1 7A AVENIDA 11-59 ZONA 9
1 EDIFICIO GALERIAS ESPAÑA
1 GUATEMALA CITY
1 GUATEMALA
024 As of 1/1/2013, funds in a noninterest-bearing transaction account (including an
1 IOLTA) will no longer receive unlimited deposit insurance coverage. They will be
1 insured to the legal maximum of $250,000 for each ownership category.
2 ................................ ACCOUNT DETAIL ...............................
0 ................................ ACCOUNT DETAIL ...............................
2 FINANCIAL INSTITUTIONS
0 FINANCIAL INSTITUTIONS Number of Enclosures 0
1 Account Number 1000062412 Statement Dates 1/01/13 thru 1/06/13
1 Previous Balance 385,002.47 Days in the statement period 6
1 Deposits/Credits .00 Average Ledger 374,620.47
1 7 Checks/Debits 15,367.65 Average Collected 374,620.47
1 Total Fees .00
1 Interest Paid .00
1 Current Balance 369,634.82
3 Checks and Withdrawals
0 Checks and Withdrawals
1 Date Description Amount Refe
0 Date Description Amount Refe
1 1/02 WIRE-OUT 20130020006601 6,293.81-
1 OBI:4423210000 VSS SETTLEMENT
1 1/02 WIRE Out Fee 20.00-
1 1/03 WIRE-OUT 20130030011401 3,541.38-
1 OBI:4423210000 VSS SETTLEMENT
1 1/03 WIRE Out Fee 20.00-
1 1/04 Account Service Fee Corresp.Bk 80.00-
1 1/04 WIRE-OUT 20130040010401 5,392.46-
1 OBI:4423210000 VSS SETTLEMENT
1 1/04 WIRE Out Fee 20.00-
2 Daily Balance Information
0 Daily Balance Information
1 Date Balance
0 Date Balance Date Balance
0 Date Balance
1 1/01 385,002.47 1/03 375,127.28
1 1/02 378,688.66 1/04 369,634.82
2 For more information about FDIC insurance coverage, visit
1 http://www.fdic.gov/deposit/deposits/unlimited/expiration.html
Waiting for any suggestion or help.
Thanks in advance...
Simple basic idea is create a ArrayList.
Create a StringBuffer add string from newLine method if its not contain "Page 1".
If string returned by newLine method contain "Page 1" then add StringBuffer into arraylist and create new StringBuffer . and do same step 2 again .
For some suggestion: lastIndexOf and also you can take a try for contains method.
List<StringBuffer> list = new ArrayList<StringBuffer>();
StringBuffer sb = new StringBuffer();
int i = 1; // I think page no is changing form 1 to ...
while (readerSplit.hasNextLine()) {
String sLineDataSplit = readerSplit.nextLine().trim();
// take a look for lastIndexOf function.
if (sLineDataSplit.lastIndexOf("Page "+i)==9 && sb.length()>0) {
list.add(sb);
sb = new StringBuffer();
sb.append(sLineDataSplit);
i++; // to get the next page
}
else{
sb.append(sLineDataSplit);
}
}
Related
I have the following .csv file:
Company ABC
"Jan 1, 2020 - Sep 30, 2020"
Product Country Avg. monthly clients Avg. month charge Parts change Impact In stock Clients in list City
Nissan Maxima USA 6600 0% -18% Low 18
BMW X7 M50i USA 18100 22% 0% Low 28
Volvo XC90 USA 880 0% -12% Low 10
Opel Insignia USA 320 -34% -34% Low 23
Renult Triber USA 140 -18% -36% Low 8
Toyota Yaris USA 880 0% -28% Low 30
Ford Mondeo USA 70 -20% -71% Low 1
for delimiter I have empty space(Tab). I tried to use this code in order to read the file using Opencsv:
#Getter
#Setter
public class CsvLine {
#CsvBindByPosition(position = 1)
private String model;
#CsvBindByPosition(position = 2)
private String country;
}
String fileName = "C:\\in_progress\\zzz.csv";
List<CsvLine> beans = new CsvToBeanBuilder(new FileReader(fileName))
.withType(CsvLine.class)
.withSeparator(' ')
.withSkipLines(1)
.build()
.parse();
for(CsvLine item: beans){
System.out.println(item.getModel());
}
But I get this output:
X C 9 0
null
I n s i g n i a U S A 3 2 0 - 3 4 % - 3 4 % L o w 2 3
null
T r i b e r
null
Y a r i s U S A 8 8 0 0 % - 2 8 % L o w 3 0
null
M o n d e o U S A 7 0 - 2 0 % - 7 1 % L o w 1
null
null
Do you know how I can the file properly with Java preferably with OpenCSV?
Test file https://www.dropbox.com/s/7jo4i3bs6h8at25/zzz.csv?dl=0
If your CSV file really uses the Tab character as field delimitier, it should be sufficient to change to:
List<CsvLine> beans = new CsvToBeanBuilder(new FileReader(fileName))
.withType(CsvLine.class)
.withSeparator('\t')
.withSkipLines(2)
.build()
.parse();
I changed withSeparator argument and increased the number of lines to skip to 2
I'm using itextpdf to display the sql query
SELECT prod_id, prod_name, amt, sum(amt) over(partition by prod_name) as total_amt
FROM items
ORDER BY prod_name;
I was able separate the table according to PROD_NAME, and calculate the total amount of each products by getting the sum(amt) over(partition by prod_name) as total_amt from the sql query. Now, I've been trying to display the total amount of each products below like this:
APPLE
PROD_ID | AMT
11111 12.75
22222 13.75
33333 14.75
Total: 41.25
ORANGE
PROD_ID | AMT
44444 15.75
55555 16.75
Total: 32.5
However, this is the output of my code. The Total amount is displayed after each row.
APPLE
PROD_ID | AMT
11111 12.75
Total: 41.25
22222 13.75
Total: 41.25
33333 14.75
Total: 41.25
ORANGE
PROD_ID | AMT
44444 15.75
Total: 32.5
55555 16.75
Total: 32.5
Here is the snippet of my code:
List<String> prod_Names = new ArrayList<>();
while(rs.next()){
String prodName = rs.getString(2);
if (!prod_Names.contains(prodName)){
prod_Names.add(prodName);
// Displays the Product Name on top of the table
PdfPCell name = new PdfPCell(new Phrase(prodname, bold));
name.setColspan(2);
prod_Table.addCell(name);
// Displays the Row Header
prod_Table.addCell(new Phrase("PROD_ID", header_Bold));
prod_Table.addCell(new Phrase("AMT", header_Bold));
}
String prodId_Values = result.getInt(1);
int amt_Values = result.getInt(3); // amount
int totalAmt = result.getInt(4); // total amount of each products
//Displays the Values
prod_Table.addCell(new Phrase(Integer.toString(prodId_Values), normalFont));
prod_Table.addCell(new Phrase(Integer.toString(amt_Values), normalFont));
// Display Total
prod_Table.addCell(new Phrase("TOTAL:", normalFont));
prod_Table.addCell(new Phrase(Integer.toString(totalAmt), normalFont));
}
I tried putting the Display Total lines inside an if condition just like how I did with the Product Name. I also added another ArrayList called prod_Names2.
if(!prod_Names2.contains(prodName)){
prod_Names2.add(prodName);
// Display Total
prod_Table.addCell(new Phrase("TOTAL:", normalFont));
prod_Table.addCell(new Phrase(Integer.toString(totalAmt), normalFont));
}
The total amount is now only displayed one time, but it's displayed after one row of each products like this. This is the best that I could do:
APPLE
PROD_ID | AMT
11111 12.75
Total: 41.25
22222 13.75
33333 14.75
ORANGE
PROD_ID | AMT
44444 15.75
Total: 32.5
55555 16.75
In Standard SQL you can do what you want using grouping sets:
SELECT prod_id, prod_name, SUM(amt) as amt
FROM items
GROUP BY GROUPING SETS ( (prod_id, prod_name), () )
ORDER BY prod_name NULLS LAST;
Not all databases support exactly this syntax, but most support some variation of it. Then your java code can just read the results from the query.
This is for something that could save me about 10 minutes at work, I am not getting paid for it. This is Java. Its been a while since I touched Java. I'm searching a PDF for just numbers that use USD currency form via pdfBox. This is a what the document looks a lot like.
Activity Report
Business Date: 10/9/2019 Property Code: me.ra777 Shift: 9 User: me.ra777
Reserve
Account Person Name Start End Days Status Money TypeOfCode Type Location Source GTD Date User
077071543 Smith's, John Middle 9/25/19 9/26/19 1 O 55.50 BAR SNQQ 211 WI MC 9/25/19 me.ra777
877075375 45Lisa, Jo.nes Mid 9/25/19 9/26/19 1 I 99.00 SEG SNKE 138 WI VI 9/25/19 me.ra777
677256813 Jo^hn Wi.ck Ed 9/26/19 9/27/19 1 O 129.00 TRQ SNQQ 132 WI VI 9/26/19 me.ra777
477007406 Guys, Are 9/26/19 9/27/19 1 O 129.00 BAR SNQQ 133 WI VI 9/26/19 me.ra777
977495887 Last, First 9/27/19 9/28/19 1 O 165.00 BAR SNKE 438 WI VI 9/27/19 me.ra777
677472246 Po.or, Rich 9/27/19 9/28/19 1 O 165.00 BAR SNKE 138 WI MC 9/27/19 me.ra777
677457228 Dude, Isn't Here 9/27/19 9/28/19 1 I 180.00 BAR SNQQ 433 WI MC 9/27/19 me.ra777
Date/Time of Printing: 10/10/2019 1:42 PM Software Version: ssrs7x67 Page 1 of 1
If I used a a method like this......
public static void oneLine(Scanner sc){
while (sc.hasNextLine()) {
String line = sc.nextLine();
if(line.contains(" WI ")){
displayArea.append("\n"+line + "\n");
break;
}else{}
}
sc.close();
}
I would only get this for my output.
077071543 Smith's, John Middle 9/25/19 9/26/19 1 O 55.50 BAR SNQQ 211 WI MC 9/25/19 me.ra777
My desired out put would be just
55.50
Maybe even all the USD amounts like this
55.50
99.00
129.00
129.00
165.00
165.00
180.00
Okay a little bit more data about this document. I only need the data in these lines
077071543 Smith's, John Middle 9/25/19 9/26/19 1 O 55.50 BAR SNQQ 211 WI MC 9/25/19 me.ra777
877075375 45Lisa, Jo.nes Mid 9/25/19 9/26/19 1 I 99.00 SEG SNKE 138 WI VI 9/25/19 me.ra777
677256813 Jo^hn Wi.ck Ed 9/26/19 9/27/19 1 O 129.00 TRQ SNQQ 132 WI VI 9/26/19 me.ra777
477007406 Guys, Are 9/26/19 9/27/19 1 O 129.00 BAR SNQQ 133 WI VI 9/26/19 me.ra777
977495887 Last, First 9/27/19 9/28/19 1 O 165.00 BAR SNKE 438 WI VI 9/27/19 me.ra777
677472246 Po.or, Rich 9/27/19 9/28/19 1 O 165.00 BAR SNKE 138 WI MC 9/27/19 me.ra777
677457228 Dude, Isn't Here 9/27/19 9/28/19 1 I 180.00 BAR SNQQ 433 WI MC 9/27/19 me.ra777
Everything in the those lines can change EXCEPT under source where it says "WI" AND Under User where it says "me.ra777" People can mess up names like where you see "45Lisa, Jo.nes" and "Jo^hn Wi.ck"
Ultimately I still have more work to do after this. Where I need to add all the USD amounts and actually, still a little more where I divide them by 100; which, in this example I believe would give me 9.225 if I did my math right.....
I'm really hoping I can just change part of this code like here ....
if(line.contains(" WI ")){
So then I could at least get an output of only the lines I need and I could work a little on my own from there and try to figure the rest out on my own.
Solved it. In short I had two major methods ----> find() & getUSD(Final String)
find() Used
1. A for Loop
2. A while(String.astNextLine)
3. If line contains("WI" && ! linecontains "Software Version " )
4. varable rate = getUSD(String)
5. doSumMathStuffs'andComplainAboutWhyJavacan'tTellThisIsa#WithoutParseing
;;
6. print ("\n"+rate);
getUSD(Final String) used
1. If/else "Matcher m = Pattern.compile("-?\d+(\.\d+)").matcher(strings);"
2. while(m.find)
3 return m.group
4. There's Actualy some parseing and some other "transfer this variable tYpE to that TyPe " too
I'm trying to get my output to print to only 1 decimal place.I've tried using the DecimalFormat but it is messing up the alignment of my output.
Here is what my output was before I used the DecimalFormat:
ID Programs Midterm Final Weighted Average
-- -------- ------- ----- -------- -------
1212 90.0 85.0 92.0 89.30000000000001 Pass
6666 60.0 80.0 90.0 78.0 Pass
7777 90.0 90.0 90.0 90.0 Pass
8888 95.0 87.0 93.0 91.8 Pass
9999 75.0 77.0 73.0 74.8 Pass
And here is what it looks like after I used the DecimalFormat:
ID Programs Midterm Final Weighted Average Programs grade
-- -------- ------- ----- ---------------- --------------
1212 90.0 85.0 92.0 89.3 Pass
6666 60.0 80.0 90.0 78 Pass
7777 90.0 90.0 90.0 90 Pass
8888 95.0 87.0 93.0 91.8 Pass
9999 75.0 77.0 73.0 74.8 Pass
Here is the code I am working with:
DecimalFormat df = new DecimalFormat("#.#");
double Program = inputFile.nextDouble(); //Programs
double Midterm = inputFile.nextDouble(); //Midterm
double Final = inputFile.nextDouble(); //Final
double WAverage = (Program * WProgram) + (Midterm * WMidterm)+
(Final * WFinal);
classAve = classAve +WAverage;
NumStudents++;
String ResultsString = new String(" Fail");
if (WAverage >= 70)
ResultsString = " Pass";
System.out.println(ID +" " + Program +" "+ Midterm +" "+ Final +
" "+ df.format(WAverage) +" " +ResultsString );
Use
String.format("%.1f", WAverage);
Instead of
df.format()
No need for DecimalFormat
You have to set the minimum amount of decimals:
df.setMinimumFractionDigits(1);
You can accomplish what you want by changing one character: Use .0 instead of .# in your format string if you want to always display one decimal place, even if that digit is zero. Here's the documentation: http://docs.oracle.com/javase/7/docs/api/java/text/DecimalFormat.html
Then the first line of your code snippet would be:
DecimalFormat df = new DecimalFormat("#.0");
I have to get my output to line up beneath a heading. No matter what I do, I cannot get to line up. The item name is very long also, and the words end up wrapping to the next line when I open my outfile. Here is my current output:
8 items are currently available for purchase in Joan's Hardware Store.
----------Joan's Hardware Store-----------
itemID itemName pOrdered pInStore pSold manufPrice sellPrice
1111 Dish Washer 20 20 0 250.50 550.50
2222 Micro Wave 75 75 0 150.00 400.00
3333 Cooking Range 50 50 0 450.00 850.00
4444 Circular Saw 150 150 0 45.00 125.00
5555 Cordless Screwdriver Kit 10 10 0 250.00 299.00
6666 Keurig Programmable Single-Serve 2 2 0 150.00 179.00
7777 Moen Chrome Kitchen Faucet 1 1 0 90.00 104.00
8888 Electric Pressure Washer 0 0 0 150.00 189.00
Total number of items in store: 308
Total inventory: $: 48400.0
Here is my code:
public void endOfDay(PrintWriter outFile)
{
outFile.println (nItems + " items are currently available for purchase in Joan's Hardware Store.");
outFile.println("----------Joan's Hardware Store-----------");
outFile.printf("itemID, itemName, pOrdered, pInStore, pSold, manufPrice, sellPrice");
for (int index = 0; index < nItems; index++)
{
outFile.printf("%n %-5s %-32s %d %d %d %.2f %.2f%n", items[index].itemID , items[index].itemName , items[index].numPord ,items[index].numCurrInSt , items[index].numPSold , items[index].manuprice , items[index].sellingprice);
}
outFile.println("Total number of items in store: " + getTotalOfStock());
outFile.println("Total inventory: $: " + getTotalDollarValueInStore());
} // end endOfDay
Thanks for any help! I have tried many things for hours!!
Basically, you need to format your header the same way you format your lines, for example...
System.out.println("----------Joan's Hardware Store-----------");
System.out.printf("%-6s %-32s %-8s %-8s %-5s %-10s %-8s%n", "itemID", "itemName", "pOrdered", "pInStore", "pSold", "manufPrice", "sellPrice");
System.out.printf("%-6s %-32s %-8d %-8d %-5d %-10.2f %-8.2f%n", "1111", "Dish Washer", 20, 20, 0, 250.50, 550.50);
Results in...
----------Joan's Hardware Store-----------
itemID itemName pOrdered pInStore pSold manufPrice sellPrice
1111 Dish Washer 20 20 0 250.50 550.50