javafor-loopif-statementoutputprims-algorithm

Wrong count i++ in for loop in Java code - Prim's algorithm


I have a big problem. I am a beginner in Java. Please help me, thanks. This part of code is a Prim's Algorithm.

The code is:

for (int i = 1; i < graph.getNumOfNodes(); i++) {
    double distance = ((int)graph.getEdges(i) - 8);

    if (graph.getEdges(i) <= 8){
        System.out.println("Day " + i + ". " + "Number of hours worked " + (int)graph.getEdges(i) + "/8 per day. Finished kilometers: " + (int)graph.getEdges(i) );

    } else if (graph.getEdges(i) > 8) {
        System.out.println("Day " + i + ". " + "Number of hours worked 8/8 per day. Finished kilometers: 8. " + "Missing to complete: " + (int)distance);
        System.out.println("Day " + i + ". " + "Number of hours worked " + (int)distance + "/8 per day. Finished kilometers: " + (int)distance);
    }
}

Now output: the Day 3 is twice but I need the second day 3 to be day four and the rest will be one larger.

Day 1. Number of hours worked 2/8 per day. Finished kilometers: 2
Day 2. Number of hours worked 7/8 per day. Finished kilometers: 7
Day 3. Number of hours worked 8/8 per day. Finished kilometers: 8. Missing to complete: 2
Day 3. Number of hours worked 2/8 per day. Finished kilometers: 2
Day 4. Number of hours worked 5/8 per day. Finished kilometers: 5
Day 5. Number of hours worked 4/8 per day. Finished kilometers: 4
Day 6. Number of hours worked 3/8 per day. Finished kilometers: 3
Day 7. Number of hours worked 1/8 per day. Finished kilometers: 1
Day 8. Number of hours worked 8/8 per day. Finished kilometers: 8
Day 9. Number of hours worked 5/8 per day. Finished kilometers: 5
Day 10. Number of hours worked 4/8 per day. Finished kilometers: 4
Day 11. Number of hours worked 3/8 per day. Finished kilometers: 3
Day 12. Number of hours worked 8/8 per day. Finished kilometers: 8

I would like the output like this:

Day 1. Number of hours worked 2/8 per day. Finished kilometers: 2
Day 2. Number of hours worked 7/8 per day. Finished kilometers: 7
Day 3. Number of hours worked 8/8 per day. Finished kilometers: 8. Missing to complete: 2
Day 4. Number of hours worked 2/8 per day. Finished kilometers: 2
Day 5. Number of hours worked 5/8 per day. Finished kilometers: 5
Day 6. Number of hours worked 4/8 per day. Finished kilometers: 4
Day 7. Number of hours worked 3/8 per day. Finished kilometers: 3
Day 8. Number of hours worked 1/8 per day. Finished kilometers: 1
Day 9. Number of hours worked 8/8 per day. Finished kilometers: 8
Day 10. Number of hours worked 5/8 per day. Finished kilometers: 5
Day 11. Number of hours worked 4/8 per day. Finished kilometers: 4
Day 12. Number of hours worked 3/8 per day. Finished kilometers: 3
Day 13. Number of hours worked 8/8 per day. Finished kilometers: 8

Solution

  • I would just add a counter to count the extra days as follows:

    extraDays = 0
    for (int i = 1; i < graph.getNumOfNodes(); i++) {
        double distance = ((int)graph.getEdges(i) - 8);
    
        if (graph.getEdges(i) <= 8) {
            System.out.println("Day " + (i + extraDays) + ". " + "Number of hours worked " +                 (int)graph.getEdges(i) + "/8 per day. Finished kilometers: " + (int)graph.getEdges(i) );
        } else if (graph.getEdges(i) > 8) {
            System.out.println("Day " + (i + extraDays) + ". " + "Number of hours worked 8/8 per day. Finished kilometers: 8. " + "Missing to complete: " + (int)distance);
            extraDays += 1
            System.out.println("Day " + (i + extraDays) + ". " + "Number of hours worked " + (int)distance + "/8 per day. Finished kilometers: " + (int)distance);
        }
    }