Jump to content
SuissoS

Descodificar matriz de um ficheiro HTML

Recommended Posts

SuissoS

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

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • 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.