Convert .txt file into .cvs file using java? - java

I have a text file.
Date Order ID SKU Transaction type Payment Type Payment Detail Amount Quantity Product Title
11-May-15 171-4579244-1779543 NT52-178 Refund Amazon fees Commission Rs. 49.32 Masha Women's Cotton Nighty NT52-178
11-May-15 171-4579244-1779543 NT52-178 Refund Amazon fees Fixed closing fee Rs. 11.24 Masha Women's Cotton Nighty NT52-178
11-May-15 171-4579244-1779543 NT52-178 Refund Amazon fees Shipping holdback Rs. 3.71 Masha Women's Cotton Nighty NT52-178
11-May-15 171-4579244-1779543 NT52-178 Refund Product charges Rs.
-399.00 1 Masha Women's Cotton Nighty NT52-178
I want to convert it into a CSV file.
I am using following code
File file = new File("/Users/manish/Documents/New folder/report.txt");
StringBuffer str = new StringBuffer();
BufferedReader br = null;
try
{
br = new BufferedReader(new FileReader(file));
String text = null;
// repeat until all lines is read
while ((text = br.readLine()) != null)
{
String splitarray[] = text.split(" ");
String Date = splitarray[0];
String Order_ID = splitarray[1]; // line 42
String sku = splitarray[2];
String Transaction_type = splitarray[3];
String Payment_type = splitarray[4];
String Payment_detail = splitarray[5];
String amount = splitarray[6];
String Quantity = splitarray[7];
String Product_title = splitarray[8];
System.out.println(Date+ " " + Order_ID);
}
} catch (FileNotFoundException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
} finally
{
try
{
if (br != null)
{
br.close();
}
} catch (IOException e)
{
e.printStackTrace();
}
}
// show file contents here
System.out.println(str.toString());
but it gives error
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
at test_pract.TEST_pract.main(TEST_pract.java:42)
Java Result: 1

The Problem are you empy lines,
then your splitarray[] is empty and the use of splitarray[1] is not possible.
You can use an if statement to sort this out:
if(!text.equals("")) {
splitarray[] ....
}
And you have another Problem: You split with 2 Whitespaces,
if you have 4 Whitespaces in your Text (and you have in your example) then you get an empty String in your Splitarray, because it is splitting between those 4 whitespaces. then you get different arrays per line.
you should eliminate these or use a better split argument like:
text.replace(" ", " ")
or
text.split(" | ")
the last one will try to split at 4 whitespaces or of not found at 2 whitespaces.

Related

csvReader is skipping the first alphabet of every row of fist column while reading from a csv file

I wrote a csvReader method in Java.
Everything seems to work fine, but it is just skipping the very first alphabet of every row in first column while printing the values. Here is my code =>
public static void csvReader(String fileName) {
try {
BufferedReader fileReader = new BufferedReader(new FileReader(fileName));
List<Student> students = new ArrayList<>();
fileReader.readLine();
while (fileReader.read() > 0) {
String line = fileReader.readLine();
String[] tokens = line.split(COMMA_DELIMETER);
if (tokens.length > 0) {
Student student = new Student(tokens[0], tokens[1], Integer.valueOf(tokens[2]),
Integer.valueOf(tokens[3]));
students.add(student);
}
}
students.forEach(System.out::println);
} catch (Exception ex) {
ex.printStackTrace();
}
}
Here "Student" is just a POJO class having firstName, lastName, id and age fields. In am getting the output as =>
Student details : ID = 101 first name = ikhil Last name = Chaurasia age = 28
Student details : ID = 102 first name = adhna Last name = Chaurasia age = 28
Where the result should be like =>
Student details : ID = 101 first name = Nikhil Last name = Chaurasia age = 28
Student details : ID = 102 first name = Sadhna Last name = Chaurasia age = 28
The csv file content are shown below :
The toString method is implemented as shown below :
public String toString() {
return "Student details : ID = "+id+ " first name = "+firstName+ " Last name = "+lastName+ " age = "+age;
}
Any help would be highly appreciated.
Thanks
The issue is in your while loop condition: fileReader.read() > 0.
This reads a single character every time you call it (such as 'N', 'S'), which causes the subsequent call to readLine to skip the first character.
Instead, just check the condition using readLine:
String line;
while ((line = fileReader.readLine()) != null) {
String[] tokens = line.split(COMMA_DELIMETER);
// do the rest of the stuff
}

How to access each element after a split

I am trying to read from a text file and split it into three separate categories. ID, address, and weight. However, whenever I try to access the address and weight I have an error. Does anyone see the problem?
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.*;
class Project1
{
public static void main(String[] args)throws Exception
{
List<String> list = new ArrayList<String>();
List<String> packages = new ArrayList<String>();
List<String> addresses = new ArrayList<String>();
List<String> weights = new ArrayList<String>();
//Provide the file path
File file = new File(args[0]);
//Reads the file
BufferedReader br = new BufferedReader(new FileReader(file));
String str;
while((str = br.readLine()) != null)
{
if(str.trim().length() > 0)
{
//System.out.println(str);
//Splits the string by commas and trims whitespace
String[] result = str.trim().split("\\s*,\\s*", 3);
packages.add(result[0]);
//ERROR: Doesn't know what result[1] or result[2] is.
//addresses.add(result[1]);
//weights.add(result[2]);
System.out.println(result[0]);
//System.out.println(result[1]);
//System.out.println(result[2]);
}
}
for(int i = 0; i < packages.size(); i++)
{
System.out.println(packages.get(i));
}
}
}
Here is the text file (The format is intentional):
,123-ABC-4567, 15 W. 15th St., 50.1
456-BgT-79876, 22 Broadway, 24
QAZ-456-QWER, 100 East 20th Street, 50
Q2Z-457-QWER, 200 East 20th Street, 49
678-FGH-9845 ,, 45 5th Ave,, 12.2,
678-FGH-9846,45 5th Ave,12.2
123-A BC-9999, 46 Foo Bar, 220.0
347-poy-3465, 101 B'way,24
,123-FBC-4567, 15 West 15th St., 50.1
678-FGH-8465 45 5th Ave 12.2
Seeing the pattern in your data, where some lines start with an unneeded comma, and some lines having multiple commas as delimiter and one line not even having any comma delimiter and instead space as delimiter, you will have to use a regex that handles all these behaviors. You can use this regex which does it all for your data and captures appropriately.
([\w- ]+?)[ ,]+([\w .']+)[ ,]+([\d.]+)
Here is the explanation for above regex,
([\w- ]+?) - Captures ID data which consists of word characters hyphen and space and places it in group1
[ ,]+ - This acts as a delimiter where it can be one or more space or comma
([\w .']+) - This captures address data which consists of word characters, space and . and places it in group2
[ ,]+ - Again the delimiter as described above
([\d.]+) - This captures the weight data which consists of numbers and . and places it in group3
Demo
Here is the modified Java code you can use. I've removed some of your variable declarations which you can have them back as needed. This code prints all the information after capturing the way you wanted using Matcher object.
Pattern p = Pattern.compile("([\\w- ]+?)[ ,]+([\\w .']+)[ ,]+([\\d.]+)");
// Reads the file
try (BufferedReader br = new BufferedReader(new FileReader("data1.txt"))) {
String str;
while ((str = br.readLine()) != null) {
Matcher m = p.matcher(str);
if (m.matches()) {
System.out.println(String.format("Id: %s, Address: %s, Weight: %s",
new Object[] { m.group(1), m.group(2), m.group(3) }));
}
}
}
Prints,
Id: 456-BgT-79876, Address: 22 Broadway, Weight: 24
Id: QAZ-456-QWER, Address: 100 East 20th Street, Weight: 50
Id: Q2Z-457-QWER, Address: 200 East 20th Street, Weight: 49
Id: 678-FGH-9845, Address: 45 5th Ave, Weight: 12.2
Id: 678-FGH-9846, Address: 45 5th Ave, Weight: 12.2
Id: 123-A BC-9999, Address: 46 Foo Bar, Weight: 220.0
Id: 347-poy-3465, Address: 101 B'way, Weight: 24
Id: 678-FGH-8465, Address: 45 5th Ave, Weight: 12.2
Let me know if this works for you and if you have any query further.
The last line only contains one token. So split will only return an array with one element.
A minimal reproducing example:
import java.io.*;
class Project1 {
public static void main(String[] args) throws Exception {
//Provide the file path
File file = new File(args[0]);
//Reads the file
BufferedReader br = new BufferedReader(new FileReader(file));
String str;
while ((str = br.readLine()) != null) {
if (str.trim().length() > 0) {
String[] result = str.trim().split("\\s*,\\s*", 3);
System.out.println(result[1]);
}
}
}
}
With this input file:
678-FGH-8465 45 5th Ave 12.2
The output looks like this:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
at Project1.main(a.java:22)
Process finished with exit code 1
So you will have to decide, what your program should do in such cases. You might ignore those lines, print an error, or only add the first token in one of your lists.
you can add following code in your code
if (result.length > 0) {
packages.add(result[0]);
}
if (result.length > 1) {
addresses.add(result[1]);
}
if (result.length > 2) {
weights.add(result[2]);
}

What is the simplest way to write the output of a Java document to a .rtf file?

I am wondering what the easiest way to write the output (last line of non-commented code below) to a .rtf file so that I can format some aspects with italics as well as keep a continuous, copy and paste-able, list of all my citations. Is there a way to do what I want that is simple? I am a beginner at Java and don't want anything too complicated to deal with.
/* (c) Tyler Holley January, 2018
* CitationGenerator Version 0.2
*
* User inputs academic source information and gets a proper citation back ready for copy and pasting into a Works Cited page.
*/
import java.util.Scanner;
class CitationGenerator {
public static String bookFormat(String author, String title, String publisher, int pubDate) {
//
String bFormat = author + ". " + title + ", " + publisher + ", " + pubDate + ".";
return bFormat;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String error1 = "ERROR: INVALID INPUT."; // Catchall error
String errorN = "ERROR: No other formats are currently supported in this version."; // Filler error while WIP
System.out.println("Welcome to CitationGenerator v0.1!");
System.out.print("What format is your citation in? MLA/APA/Chicago: "); // Add APA/Chicago support
String format = in.next();
if (format.equalsIgnoreCase("MLA")) { // equalsIgnoreCase ignores case inputted so MLA, mLa, mla, etc. are all valid
System.out.print("\nIs your source a book, website, or other? ");
String sourceType = in.next();
// Try and figure out how to clear the console after the user inputs sourceType
if (sourceType.equalsIgnoreCase("book")) {
System.out.print("\nAuthor's First Name: ");
String fName = in.next();
System.out.print("Author's Last Name: ");
String lName = in.next();
in.nextLine();
System.out.print("\nBook Title: ");
String title = in.nextLine();
System.out.print("\nPublisher Name: ");
String publisher = in.nextLine();
System.out.print("\nPublication Date: ");
int pubDate = in.nextInt();
String author = lName + ", " + fName; // Converts fName and lName to one concatonated String
System.out.println("\n\nHere is your citation! Don't forget to italicize the title!\n");
// Try to automatically italicize text when converting program to a .rtf
System.out.println(bookFormat(author, title, publisher, pubDate));
// GOAL: Alternate to the println below :
//System.out.println("\n\nYour citation is ready, would you like to save it to a/the .rtf document? y/n");
// This would branch into an if/else statement to print either to a document or continue to terminal output.
}
}
}
Pretty similar to writing to any file:
DataOutputStream dos;
File file = new File("\\your\\file\\path\\file.rtf");
try {
dos = new DataOutputStream(new FileOutputStream(file));
dos.writeBytes(bookFormat(author, title, publisher, pubDate));
dos.close();
catch( //IOexception or similar etc
Output:
surname, firstname. booktitle, bookpublisher, 1234.

extract data column-wise from text file using Java

I'm working under Java and want to extract data according to column from a text file.
"myfile.txt" contents:
ID SALARY RANK
065 12000 1
023 15000 2
035 25000 3
076 40000 4
I want to extract the data individually according to any Column i.e ID, SALARY, RANK etc
Basically I want to perform operations on individual data according to columns.
I've listed the data from "myfile.txt" by using while loop and reading line-by-line:
while((line = b.readLine()) != null) {
stringBuff.append(line + "\n");
}
link: Reading selective column data from a text file into a list in Java
Under bove link it is written to use the following:
String[] columns = line.split(" ");
But how to use it correctly, please any hint or help?
You can use a regex to detect longer spaces, example:
String text = "ID SALARY RANK\n" +
"065 12000 1\n" +
"023 15000 2\n" +
"035 25000 3\n" +
"076 40000 4\n";
Scanner scanner = new Scanner(text);
//reading the first line, always have header
//I suppose
String nextLine = scanner.nextLine();
//regex to break on any ammount of spaces
String regex = "(\\s)+";
String[] header = nextLine.split(regex);
//this is printing all columns, you can
//access each column from row using the array
//indexes, example header[0], header[1], header[2]...
System.out.println(Arrays.toString(header));
//reading the rows
while (scanner.hasNext()) {
String[] row = scanner.nextLine().split(regex);
//this is printing all columns, you can
//access each column from row using the array
//indexes, example row[0], row[1], row[2]...
System.out.println(Arrays.toString(row));
System.out.println(row[0]);//first column (ID)
}
while((line = b.readLine()) != null) {
String[] columns = line.split(" ");
System.out.println("my first column : "+ columns[0] );
System.out.println("my second column : "+ columns[1] );
System.out.println("my third column : "+ columns[2] );
}
Now instead of System.out.println, do whatever you want with your columns.
But I think your columns are separated by tabs so you might want to use split("\t") instead.

InputMismatchException Error in Java

public static void readStaffsFromFile() {
String inFileName = "startup.txt";
int numStaff, staffID;
String name, address;
Staff newStaff;
boolean fileExists;
Scanner inFile = null;
File databaseFile = new File(inFileName);
fileExists = databaseFile.exists();
if (fileExists) {
try {
inFile = new Scanner(databaseFile);
} catch (FileNotFoundException fnfe) {
JOptionPane.showMessageDialog(null, "The file startup.txt has just now been deleted.");
return; // cannot do anything more.
}
numStaff = inFile.nextInt();
inFile.nextLine();
for (int i = 0; i < numStaff; i++) {
staffID = inFile.nextInt();
name = inFile.nextLine();
address = inFile.nextLine();
// try{
newStaff = new Staff(staffID, name, address);
addStaff(newStaff);
// } catch (StaffException se)
// {
// System.out.println("Unable to add staff: " + name +
// " to the system.");
// }
}
}
JOptionPane.showMessageDialog(null, "System has been set up with default data from startup.txt.");
}
I have this method and when I try to call this method from main, it gives me this error.
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:909)
at java.util.Scanner.next(Scanner.java:1530)
at java.util.Scanner.nextInt(Scanner.java:2160)
at java.util.Scanner.nextInt(Scanner.java:2119)
at SystemStartUp.readStaffsFromFile(SystemStartUp.java:195)
at SystemStartUp.loadFromFile(SystemStartUp.java:160)
at StartUp.main(StartUp.java:9)
The error line of error states that my error starts from the line of "staffID = inFile.nextInt();"
The input file looks like this.
13
11111111
Chris Ling
999 Dandenong Road
22222222
Des Casey
100 Silly Drive
33333333
Maria Indrawan
90 Programming Road
44444444
Campbell Wilson
2/5 Database Street
55555555
Janet Fraser
21 Web Drive
66666666
Judy Sheard
34 Hos Road
77777777
Ngoc Minh
24 Message Street
88888888
Martin Atchinson
45 Martine Street
99999999
Aleisha Matthews
1/6 Admin Road
10101010
Denyse Cove
100 Reception Street
12121212
Cornelia Liou
232 Reception Road
23232323
Trudi Robinson
111 Manager Street
34343434
Henry Linger
2/4 HDR Street
Probably the staffID doesn't always contains numbers. Please check the input file.
After staffID, you have to add inFile.nextLine(); to consume the new line character after of the line with number. Otherwise, you will get the error on the second loop.

Categories

Resources