Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7845197
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T17:09:01+00:00 2026-06-02T17:09:01+00:00

I’m attempting to calculate the critical path, using the critical path method. I have

  • 0

I’m attempting to calculate the critical path, using the critical path method. I have several test cases, and the output should be printed to a file called “tarea2.out”.

The problem is that when I print the file, it seems I overwrite the output for each case and finally output only shows me the last of them. I know it’s silly, but I’m new to Java and I can not make the output is correct

Here’s my code:

package tarea;

import java.io.*;
import java.util.Arrays;
import java.util.StringTokenizer;

/**
 *
 * @author Francisco
 */
public class Main {
    public static int c;

    public static void recorrido(int[][] adj) throws IOException{
        int n=adj.length;
        int casitas[][] = new int[n][2];
        int mejorCamino[] = new int [n];

        int temp;

        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++){
                temp = adj[i][j];
                if(temp > -1){
                    if(temp + casitas[i][0]>casitas[j][0]){
                        casitas[j][0] = temp + casitas[i][0];
                        mejorCamino[j] = i;
                    }
                }
            }
        }
        //Hacemos el paso hacia atrás.
        for(int y=0;y<n;y++)
        casitas[y][1] = casitas[n-1][0];

        for (int j=n-1;j>=0;j--){
            for(int i=0;i<n;i++){
                temp = adj[i][j];
                 if(temp > -1){
                    casitas[i][1]= Math.min(casitas[j][1] - temp , casitas[i][1]);
                }
            }
        }

        int x=n-1;
        String cam = "";
        while(x>0){
            if(x==n-1)
                cam= mejorCamino[x] + " " + x;
            else
                cam= mejorCamino[x] + " " + x + "\n" + cam;
            adj[mejorCamino[x]][x] = -1;
            x = mejorCamino[x];

        }

        //Calculamos las holguras con nuestra nueva matriz
        String mac="";
        int HT , HL;
        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++){
                temp = adj[i][j];
                if (temp > -1){
                    HT = casitas[j][1] - temp - casitas[i][0];
                    HL = casitas[j][0] - temp - casitas[i][0];
                    mac += "\n" + i + " " + j + " " + HT + " " + HL;
                    if (HT>HL)
                        mac += " R";
                }
            }
        }

        String sFichero = "tarea2.out";
        File fichero = new File(sFichero);

        BufferedWriter bw = new BufferedWriter(new FileWriter(sFichero));

        bw.write("Case " + c + ": total duration " + casitas[n-1][0] );
                 bw.write("\n");
                 bw.write(cam);
                 bw.write(mac);
                 bw.write("\n");

                 // Hay que cerrar el fichero
        bw.close(); 
    }


}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T17:09:03+00:00Added an answer on June 2, 2026 at 5:09 pm

    You’re creating the BufferedWriter every time you call recorrido method.

    BufferedWriter bw = new BufferedWriter(new FileWriter(sFichero));
    

    The file will be rewritten every time. Add the true parameter to the constructor to tell that it will open the file in append mode

    BufferedWriter bw = new BufferedWriter(new FileWriter(sFichero, true));
    

    Also, it would be better if you open the file once in all the application and in recorrido just write the content instead of open/close the file many times.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I have thousands of HTML files to process using Groovy/Java and I need to
I have a reasonable size flat file database of text documents mostly saved in
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I am reading a book about Javascript and jQuery and using one of the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.