-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextToCSVConverter.java
More file actions
197 lines (162 loc) · 7.67 KB
/
TextToCSVConverter.java
File metadata and controls
197 lines (162 loc) · 7.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.util.*;
public class TextToCsvConverter extends JFrame {
private JTextArea textArea;
private JButton convertButton;
private JButton downloadButton;
private JLabel statusLabel;
private String csvContent;
public TextToCsvConverter() {
setTitle("Text to CSV Converter");
setSize(600, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setLayout(new BorderLayout(10, 10));
// Top Panel with Instructions
JPanel topPanel = new JPanel();
JLabel instructionLabel = new JLabel("Paste your text below (every 4 lines = 1 row):");
topPanel.add(instructionLabel);
add(topPanel, BorderLayout.NORTH);
// Center Panel with Text Area
textArea = new JTextArea();
textArea.setFont(new Font("Monospaced", Font.PLAIN, 12));
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
JScrollPane scrollPane = new JScrollPane(textArea);
scrollPane.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
add(scrollPane, BorderLayout.CENTER);
// Bottom Panel with Buttons
JPanel bottomPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 10, 10));
convertButton = new JButton("Convert to CSV");
downloadButton = new JButton("Download CSV");
downloadButton.setEnabled(false);
statusLabel = new JLabel("Paste text and click Convert");
statusLabel.setFont(new Font("Arial", Font.PLAIN, 12));
bottomPanel.add(convertButton);
bottomPanel.add(downloadButton);
bottomPanel.add(statusLabel);
add(bottomPanel, BorderLayout.SOUTH);
// Convert Button Action
convertButton.addActionListener(e -> convertTextToCsv());
// Download Button Action
downloadButton.addActionListener(e -> downloadFile());
}
private void convertTextToCsv() {
String text = textArea.getText().trim();
if (text.isEmpty()) {
JOptionPane.showMessageDialog(this, "Please paste some text first!",
"No Text", JOptionPane.WARNING_MESSAGE);
return;
}
try {
String[] lines = text.split("\\r?\\n");
StringBuilder csv = new StringBuilder();
int rowCount = 0;
// Process every 4 lines as one row
for (int i = 0; i < lines.length; i += 4) {
if (i + 3 < lines.length) {
// Skip first line (line 0 of the group)
// Add second line (index i+1)
String line2 = lines[i + 1].trim();
csv.append("\"").append(line2.replace("\"", "\"\"")).append("\"").append(",");
// Add third line (index i+2)
String line3 = lines[i + 2].trim();
csv.append("\"").append(line3.replace("\"", "\"\"")).append("\"").append(",");
// Process fourth line (index i+3) - split date/time and amount
String line4 = lines[i + 3].trim();
String[] parts = splitDateAndAmount(line4);
csv.append("\"").append(parts[0].replace("\"", "\"\"")).append("\"").append(",");
csv.append("\"").append(parts[1]).append("\"");
csv.append("\n");
rowCount++;
} else if (i + 2 < lines.length) {
// Handle case where we have only 3 lines left
String line2 = lines[i + 1].trim();
csv.append("\"").append(line2.replace("\"", "\"\"")).append("\"").append(",");
String line3 = lines[i + 2].trim();
csv.append("\"").append(line3.replace("\"", "\"\"")).append("\"").append(",");
csv.append("\"\",\"\"");
csv.append("\n");
rowCount++;
}
}
csvContent = csv.toString();
statusLabel.setText("Converted! " + rowCount + " rows created");
downloadButton.setEnabled(true);
JOptionPane.showMessageDialog(this, "Text converted successfully!\n" +
rowCount + " rows with 4 columns each.\nClick Download to save.",
"Success", JOptionPane.INFORMATION_MESSAGE);
} catch (Exception ex) {
JOptionPane.showMessageDialog(this, "Error converting text: " + ex.getMessage(),
"Error", JOptionPane.ERROR_MESSAGE);
ex.printStackTrace();
}
}
private String[] splitDateAndAmount(String line) {
// Expected format: "20 Oct 2025, 03:00 PM- ₹250.00" or "20 Oct 2025, 03:00 PM + ₹250.00"
String[] result = new String[2];
// Find the position after AM or PM
int amIndex = line.indexOf("AM");
int pmIndex = line.indexOf("PM");
int splitIndex = -1;
if (amIndex != -1) {
splitIndex = amIndex + 2;
} else if (pmIndex != -1) {
splitIndex = pmIndex + 2;
}
if (splitIndex != -1 && splitIndex < line.length()) {
// Extract date and time part
result[0] = line.substring(0, splitIndex).trim();
// Extract amount part
String amountPart = line.substring(splitIndex).trim();
// Find the sign (- or +)
String sign = "";
if (amountPart.startsWith("-")) {
sign = "-";
amountPart = amountPart.substring(1).trim();
} else if (amountPart.startsWith("+")) {
sign = "+";
amountPart = amountPart.substring(1).trim();
}
// Remove currency symbol (₹ or any other non-numeric except decimal point)
amountPart = amountPart.replaceAll("[^0-9.]", "");
// Combine sign with amount
result[1] = sign + amountPart;
} else {
// Fallback if format doesn't match
result[0] = line;
result[1] = "";
}
return result;
}
private void downloadFile() {
JFileChooser fileChooser = new JFileChooser();
fileChooser.setDialogTitle("Save CSV File");
fileChooser.setSelectedFile(new File("output.csv"));
int result = fileChooser.showSaveDialog(this);
if (result == JFileChooser.APPROVE_OPTION) {
File outputFile = fileChooser.getSelectedFile();
// Add .csv extension if not present
if (!outputFile.getName().toLowerCase().endsWith(".csv")) {
outputFile = new File(outputFile.getAbsolutePath() + ".csv");
}
try (PrintWriter pw = new PrintWriter(new FileWriter(outputFile))) {
pw.print(csvContent);
JOptionPane.showMessageDialog(this, "CSV file saved successfully!",
"Success", JOptionPane.INFORMATION_MESSAGE);
statusLabel.setText("File saved: " + outputFile.getName());
} catch (IOException ex) {
JOptionPane.showMessageDialog(this, "Error saving file: " + ex.getMessage(),
"Error", JOptionPane.ERROR_MESSAGE);
}
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
TextToCsvConverter converter = new TextToCsvConverter();
converter.setVisible(true);
});
}
}