Jump to content

Recommended Posts

Posted (edited)

Eu fiz este programa para passar uma matriz m para um ficheiro HTML em NetBeans.

private static String escreverHTML(int x, int[][] m) throws FileNotFoundException {
	String format;
	format = "<table align='center'>";
	for (int i = 0; i < x; i++) {
		format += "<tr>";
		for (int j = 0; j < x; j++) {
			format += "<td>";
			if (j == 0) {
				format += "[";
			}
			format += m[i][j];
			if (j == x - 1) {
				format += "]";
			}
			format += "</td>";
		}
		format += "</tr>";
	}
	return format;
}

No entanto agora tenho que o descodificar e da-me erro:


public static int[][] lerHTML() throws FileNotFoundException {
	System.out.println("Digite o nome do ficheiro");
	String nomedoficheiro = in.nextLine();
	Scanner ficheiro = new Scanner(new File(nomedoficheiro + ".html"));
	String ficheirotxt = "";
	while (ficheiro.hasNext()) {
		ficheirotxt += ficheiro.nextLine();
	}
	ficheiro.close();
	String[] table = ficheirotxt.split("<table align='center'>");
	String[] tr = table[1].split("<tr>");
	String[] barra = table[1].split("[");
	int[][] matriz = new int[table[1].split("<tr>").length - 1][tr[1].split("<td>").length - 1];
	for (int i = 1; i < table[1].split("<tr>").length; i++) {
		String[] td = tr[i].split("<td>");
		for (int j = 1; j < tr[1].split("<td>").length;j++) {
			matriz[i - 1][j - 1] = cortar(td[j]);
		}
	}
	return matriz;
}

private static int cortar(String line) {
	int devolve = 0;
	String[] splitted = line.split("</td>");
	String troca = splitted[0].replace(",", ".");
	devolve = Integer.parseInt(troca);
	return devolve;
}
Edited by apocsantos
geshi

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.