Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Task Two New Line error #10

Open
WA64 opened this issue Dec 10, 2024 · 5 comments
Open

Task Two New Line error #10

WA64 opened this issue Dec 10, 2024 · 5 comments

Comments

@WA64
Copy link

WA64 commented Dec 10, 2024

I am receiving the following error in the TaskTwoTests file even before I try implementing a listener specifically with "kafkaProducer.send(transactionLine);":

"java.lang.NumberFormatException: For input string: "122.86
5"
at java.base/jdk.internal.math.FloatingDecimal.readJavaFormatString([FloatingDecimal.java:2054](..."

The data file this is using has 122.86 and 5 separated by a new line character. It had no issues processing the numbers beforehand separated by a comma. The yaml file was actually blank when I downloaded the zip so a used the solution provided in the other post (Thank you for that). Am I meant to fix this somehow in my listener class, or could there be an error in my configuration files? I am running VS Code on Windows 11.

@WA64
Copy link
Author

WA64 commented Dec 10, 2024

It looks like transactionline from this loop in the test:

for (String transactionLine : transactionLines){

is printing out the whole data file as a string, rather than separating each line

@puchakayala-jeevan
Copy link

Make sure that you're properly handling newlines and commas and that the data is split correctly for processing.

@puchakayala-jeevan
Copy link

for (String transactionLine : transactionLines) {
// Trim any unwanted spaces or newline characters and split the line by commas
transactionLine = transactionLine.trim();
String[] parts = transactionLine.split("\n");

// Check if there are exactly two parts
if (parts.length == 2) {
    try {
        double value1 = Double.parseDouble(parts[0].trim());
        double value2 = Double.parseDouble(parts[1].trim());
        // Process the values (e.g., sending them to the producer)
        kafkaProducer.send(transactionLine);
    } catch (NumberFormatException e) {
        System.out.println("Invalid number format: " + transactionLine);
    }
} else {
    System.out.println("Unexpected data format: " + transactionLine);
}

}

@Sibany007

This comment has been minimized.

@Sibany007
Copy link

The provided code snippet seems to be part of a Java program that processes a string split into parts. However, there are a couple of issues that need to be addressed. Here is a revised version of the code:

// Check if there are exactly two parts
if (parts.length == 2) {
    try {
        double value1 = Double.parseDouble(parts[0].trim());
        double value2 = Double.parseDouble(parts[1].trim());
        // Create the transaction line (assuming you want to format it somehow)
        String transactionLine = value1 + "," + value2; // Adjust according to your needs
        // Process the values (e.g., sending them to the producer)
        kafkaProducer.send(transactionLine);
    } catch (NumberFormatException e) {
        System.out.println("Invalid number format: " + transactionLine);
    }
} else {
    System.out.println("Unexpected data format: " + transactionLine);
}

Key Changes:

  1. Transaction Line Creation: Make sure the transactionLine is correctly defined before it is sent to the kafkaProducer. In the above code, it's created by concatenating value1 and value2. Adjust this as necessary to match the expected format for your Kafka producer.

  2. Handling transactionLine: The initial reference to transactionLine in the catch block might cause an issue if it's not defined in the scope of the try block. Ensure that transactionLine is constructed before it’s used, as shown.

  3. Consider Null or Empty Parts: If there's a chance that parts could be null or empty, you might want to add additional checks to handle those cases gracefully.

This should resolve any issues with the code snippet provided.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants