added a counter for max users made so that there are no empty rows. e.g if 3 names but 2 titles, 3rd title uses placeholder label.

This commit is contained in:
bobbert committed 2025-04-02 15:12:28 +01:00
1 parent faeab294c0
commit 39c2969092
1 file changed
+39 -11
+39 -11
View File
@@ -10,6 +10,7 @@ public class Main {
public static void main(String[] args) {
String file = "data/cv_repo_0.csv";
UserGroup userGroup = UserGroup.getInstance();
int maxUserCount = 0;
try (BufferedReader r = new BufferedReader(new FileReader(file))) {
// Skip the first line (Section,Sub-Section,Variants)
@@ -18,30 +19,57 @@ public class Main {
// Read all lines into memory so we can process by column
String[] lines = r.lines().toArray(String[]::new);
// Parse each line
for (int lineIndex = 0; lineIndex < lines.length; lineIndex++) {
String[] parts = lines[lineIndex].split(",");
// First pass: determine maximum number of users
for (String line : lines) {
String[] parts = line.split(",");
int userCount = parts.length - 2; // Subtract first two columns
maxUserCount = Math.max(maxUserCount, userCount);
}
// Create all users with default "Unknown" values
for (int i = 0; i < maxUserCount; i++) {
User user = userGroup.getUser(i);
user.setName("Unknown");
user.setTitle("Unknown");
user.setEmail("Unknown");
user.setPhone("Unknown");
user.setAddress("Unknown");
}
// Second pass: fill in actual values where available
for (String line : lines) {
String[] parts = line.split(",");
if (parts.length < 3) continue;
String sectionType = parts[0] + "," + parts[1];
// Process each user column (starting from column 2, index 2)
for (int colIndex = 2; colIndex < parts.length; colIndex++) {
int userIndex = colIndex - 2; // User index starts from 0
User user = userGroup.getUser(userIndex);
String value = parts[colIndex].trim();
// Assign attribute based on line content
String sectionType = parts[0] + "," + parts[1];
switch (sectionType) {
case "User,Name" -> user.setName(parts[colIndex]);
case "User,Title" -> user.setTitle(parts[colIndex]);
case "User,Email" -> user.setEmail(parts[colIndex]);
case "Contact Information,Phone" -> user.setPhone(parts[colIndex]);
case "Contact Information,Address" -> user.setAddress(parts[colIndex]);
// Only update if the value is not empty
if (!value.isEmpty()) {
switch (sectionType) {
case "User,Name" -> user.setName(value);
case "User,Title" -> user.setTitle(value);
case "User,Email" -> user.setEmail(value);
case "Contact Information,Phone" -> user.setPhone(value);
case "Contact Information,Address" -> user.setAddress(value);
}
}
}
}
System.out.println("Total users created: " + maxUserCount); // console debug
} catch (Exception e) {
System.out.print(e);
}
//outside reader.
new MainViewer();
}
}