This site requires JavaScript, please enable it in your browser!
Greenfoot back
erdelf
erdelf wrote ...

2012/11/30

wrong values

erdelf erdelf

2012/11/30

#
I have a problem with the following code. The values a, b, c and d are pretty much lower then I expected. str, has the value: "<rect x="0" y="0" width="180" height="180" fill="white" />"
					switch (i) 
					{
					
					// Wenn i = 0 ist...
					case 0:

						// Alles zwischen y=" und dem naehsten " wird in read_y
						// geschrieben
						read_y = str.substring(end - beg + 11, beg + 4);
						length = read_y.length();

						// read_y wird "Ordnungsgemaess verschlossen"
						read_y = tangible.StringFunctions.changeCharacter(
								read_y, length, '\0');

						// switch-Abfrage wird beendet
						break;

					// Wenn i = 1 ist...
					case 1:

						// Alles zwischen x=" und dem naehsten " wird in read_x
						// geschrieben
						read_x = str.substring(end - beg + 5, beg + 4);
						length = read_x.length();
						// read_x wird "Ordnungsgemaess verschlossen"
						read_x = tangible.StringFunctions.changeCharacter(
								read_x, length, '\0');

						// switch-Abfrage wird beendet
						break;

					// Wenn i = 2 ist...
					case 2:

						// Alles zwischen h=" und dem naehsten " wird in read_h
						// geschrieben
						read_h = str.substring(end - beg + 19, beg + 4);
						length = read_h.length();

						// read_h wird "Ordnungsgemaess verschlossen"
						read_h = tangible.StringFunctions.changeCharacter(
								read_h, length, '\0');

						// switch-Abfrage wird beendet
						break;

					// Wenn i = 3 ist...
					case 3:

						// Alles zwischen t=" und dem naehsten " wird in read_t
						// geschrieben
						read_t = str.substring(end - 3, beg + 4);
						length = read_t.length();
						// read_t wird "Ordnungsgemaess verschlossen"
						read_t = tangible.StringFunctions.changeCharacter(
								read_t, length, '\0');

						// switch-Abfrage wird beendet
						break;
					}

					// Zaehler Variable i wird um 1 erhoeht/inkrementiert
					i++;
				}

				// 4 Integer werden erstellt
				int a;
				int b;
				int c;
				int d;

				// read_x (char) wird zu einem Integer umgewandelt und in a
				// gespeichert (a wird zum Wert x eines Rechtecks aus der Datei
				// fraktal.svg)
				read_x = read_x.trim();
				a = Integer.parseInt(read_x);

				// read_y (char) wird zu einem Integer umgewandelt und in b
				// gespeichert (b wird zum Wert y eines Rechtecks aus der Datei
				// fraktal.svg)
				read_y = read_y.trim();
				b = Integer.parseInt(read_y);

				// read_h (char) wird zu einem Integer umgewandelt und in c
				// gespeichert (c wird zum Wert width eines Rechtecks aus der
				// Datei fraktal.svg)
				read_h = read_h.trim();
				c = Integer.parseInt(read_h);

				// read_t (char) wird zu einem Integer umgewandelt und in d
				// gespeichert (d wird zum Wert height eines Rechtecks aus der
				// Datei fraktal.svg)
				read_t = read_t.trim();
				d = Integer.parseInt(read_t);
				
				System.out.println(a + " " + b + " " + c + " " + d);
erdelf erdelf

2012/12/1

#
could someone help, I dont get, what is wrong.
danpost danpost

2012/12/1

#
It is quite hard to figure out what is wrong when you have hanging variables 'i', 'beg', and 'end'. What values do 'beg' and 'end' contain (or what values can they contain) and what is controlling the value of 'i'?
erdelf erdelf

2012/12/1

#
@danpost, thx for looking over it. The variable i is 0 at the beginning, but this part will be executed four times, so every case is executed. Here is the full code: The main class
package carpet;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class SierpinskiCarpet {
	FileReader f;
	FileWriter fnew;
	BufferedReader fReader;
	BufferedWriter fnewWriter;

	public SierpinskiCarpet() {
		drawCarpet();
	}

	public static void main(String[] args) {
		new SierpinskiCarpet();
	}

	public void drawCarpet() {
		try {
			f = new FileReader("Fraktal.svg");
			fnew = new FileWriter("NewFraktal.svg");
			fReader = new BufferedReader(f);
			fnewWriter = new BufferedWriter(fnew);
		} catch (IOException e) {
			e.printStackTrace();
		}
		try {
			String ls;
			int j = 0;
			while ((ls = fReader.readLine()) != null) { // anfangstag
				fnewWriter.write(ls);
				fnewWriter.newLine();
				if (j == 1)
					break;
				j++;
			}
			// hauptteil
			// zaehlt die neu erstellten Objekte
			int obj = 0;
			@SuppressWarnings("unused")
			String line = new String(new char[256]);
			// line wird Inhalt des Strings str um Operationen wie .find
			// ausfuehren zu koennen
			String str = new String(fReader.readLine());
			System.out.println(str);

			// Sofern im String str die Woerter rect und white gefunden werden
			// soll folgendes passieren
			System.out.println(str.indexOf("rect"));
			System.out.println(str.indexOf("white") != -1);
			if (str.indexOf("rect") != -1 && str.indexOf("white") != -1) {
				// beg, end und length werden als size_t definiert
				int beg;
				int end;
				int length;

				// Die Char Tabelle findstr wird erstellt und gefuellt
				// h=" und t=" sind die Endungen von width und height
				String[] findstr = { "y=\"", "x=\"", "h=\"", "t=\"" };

				// 4 Chararrays werden definiert zum speichern der Werte y, x,
				// width und height aus str
				// Eine Tabelle waere hier schoener und strukturierter, ist aber
				// leider nicht moeglich
				String read_y = new String(new char[20]);
				String read_x = new String(new char[20]);
				String read_h = new String(new char[20]);
				String read_t = new String(new char[20]);

				// Zaehler Variable
				int i = 0;

				// Sollange i nicht 4 ist...
				while (i != 4) {
					// beg wird zum Fundort des ersten Elements aus findstr (im
					// zweiten Durchlauf zum zweiten Element usw.)
					beg = str.indexOf(findstr[i]);

					// end wird zum Fundort des naehsten , auf finstr folgenden,
					// " (Gaensefuesschen)
					end = str.indexOf("\"", beg + 3);
					System.out.println(i);
					switch (i) {

					// Wenn i = 0 ist...
					case 0:

						// Alles zwischen y=" und dem naehsten " wird in read_y
						// geschrieben
						read_y = str.substring(end - beg + 11, beg + 4);
						length = read_y.length();

						// read_y wird "Ordnungsgemaess verschlossen"
						read_y = tangible.StringFunctions.changeCharacter(
								read_y, length, '\0');

						// switch-Abfrage wird beendet
						break;

					// Wenn i = 1 ist...
					case 1:

						// Alles zwischen x=" und dem naehsten " wird in read_x
						// geschrieben
						read_x = str.substring(end - beg + 5, beg + 4);
						length = read_x.length();
						// read_x wird "Ordnungsgemaess verschlossen"
						read_x = tangible.StringFunctions.changeCharacter(
								read_x, length, '\0');

						// switch-Abfrage wird beendet
						break;

					// Wenn i = 2 ist...
					case 2:

						// Alles zwischen h=" und dem naehsten " wird in read_h
						// geschrieben
						read_h = str.substring(end - beg + 19, beg + 4);
						length = read_h.length();

						// read_h wird "Ordnungsgemaess verschlossen"
						read_h = tangible.StringFunctions.changeCharacter(
								read_h, length, '\0');

						// switch-Abfrage wird beendet
						break;

					// Wenn i = 3 ist...
					case 3:

						// Alles zwischen t=" und dem naehsten " wird in read_t
						// geschrieben
						read_t = str.substring(end - 3, beg + 4);
						length = read_t.length();
						// read_t wird "Ordnungsgemaess verschlossen"
						read_t = tangible.StringFunctions.changeCharacter(
								read_t, length, '\0');

						// switch-Abfrage wird beendet
						break;
					}

					// Zaehler Variable i wird um 1 erhoeht/inkrementiert
					i++;
				}

				// 4 Integer werden erstellt
				int a;
				int b;
				int c;
				int d;

				// read_x (char) wird zu einem Integer umgewandelt und in a
				// gespeichert (a wird zum Wert x eines Rechtecks aus der Datei
				// fraktal.svg)
				read_x = read_x.trim();
				a = Integer.parseInt(read_x);

				// read_y (char) wird zu einem Integer umgewandelt und in b
				// gespeichert (b wird zum Wert y eines Rechtecks aus der Datei
				// fraktal.svg)
				read_y = read_y.trim();
				b = Integer.parseInt(read_y);

				// read_h (char) wird zu einem Integer umgewandelt und in c
				// gespeichert (c wird zum Wert width eines Rechtecks aus der
				// Datei fraktal.svg)
				read_h = read_h.trim();
				c = Integer.parseInt(read_h);

				// read_t (char) wird zu einem Integer umgewandelt und in d
				// gespeichert (d wird zum Wert height eines Rechtecks aus der
				// Datei fraktal.svg)
				read_t = read_t.trim();
				d = Integer.parseInt(read_t);

				System.out.println(a + " " + b + " " + c + " " + d);

				fnewWriter.write("<rect x=\"" + a + "\" y=\"" + b
						+ "\" width=\"" + c * 1 / 3 + "\" height=\"" + d * 1
						/ 3 + "\" fill=\"white\"/>" + "\n");
				fnewWriter.write("<rect x=\"" + (c * 1 / 3) + a + "\" y=\"" + b
						+ "\" width=\"" + c * 1 / 3 + "\" height=\"" + d * 1
						/ 3 + "\" fill=\"white\"/>" + "\n");
				fnewWriter.write("<rect x=\"" + (c * 2 / 3) + a + "\" y=\"" + b
						+ "\" width=\"" + c * 1 / 3 + "\" height=\"" + d * 1
						/ 3 + "\" fill=\"white\"/>" + "\n");
				fnewWriter.write("<rect x=\"" + a + "\" y=\"" + (d * 1 / 3) + b
						+ "\" width=\"" + c * 1 / 3 + "\" height=\"" + d * 1
						/ 3 + "\" fill=\"white\"/>" + "\n");

				// Das Rechteck in der Mitte wird schwarz gefuellt
				fnewWriter.write("<rect x=\"" + (c * 1 / 3) + a + "\" y=\""
						+ (d * 1 / 3) + b + "\" width=\"" + c * 1 / 3
						+ "\" height=\"" + d * 1 / 3 + "\" fill=\"black\"/>"
						+ "\n");
				fnewWriter.write("<rect x=\"" + (c * 2 / 3) + a + "\" y=\""
						+ (d * 1 / 3) + b + "\" width=\"" + c * 1 / 3
						+ "\" height=\"" + d * 1 / 3 + "\" fill=\"white\"/>"
						+ "\n");
				fnewWriter.write("<rect x=\"" + a + "\" y=\"" + (d * 2 / 3) + b
						+ "\" width=\"" + c * 1 / 3 + "\" height=\"" + d * 1
						/ 3 + "\" fill=\"white\"/>" + "\n");
				fnewWriter.write("<rect x=\"" + (c * 1 / 3) + a + "\" y=\""
						+ (d * 2 / 3) + b + "\" width=\"" + c * 1 / 3
						+ "\" height=\"" + d * 1 / 3 + "\" fill=\"white\"/>"
						+ "\n");
				fnewWriter.write("<rect x=\"" + (c * 2 / 3) + a + "\" y=\""
						+ (d * 2 / 3) + b + "\" width=\"" + c * 1 / 3
						+ "\" height=\"" + d * 1 / 3 + "\" fill=\"white\"/>"
						+ "\n");

				// obj wird um 9 erhoeht da 9 neue Rechtecke berrechnet wurden
				obj = obj + 9;
			}
			// Sollte die ausgelesene Zeile aus fraktal.svg die Woerter rect und
			// black enthalten...
			else if (str.indexOf("rect") != -1 && str.indexOf("black") != -1) {

				// ...wird die Zeile 1:1 in NewFraktal.svg uebernommen
				fnewWriter.write("n");

				// obj wird um 1 erhoeht/inkrementiert da 1 neues Rechteck zu
				// NewFraktal.svg hinzugefuegt wurde
				obj++;
			}
			fnewWriter.write("</svg>"); // end tag
			fnewWriter.close();
			f.close();
			fReader.close();
			fnew.close();
			BufferedReader fnewReader = new BufferedReader(new FileReader(
					"NewFraktal.svg"));
			String lines;
			lines = fnewReader.readLine();
			do {
				System.out.println(lines);
			} while ((lines = fnewReader.readLine()) != null);
			fnewReader.close();
		} catch (IOException e1) {
			e1.printStackTrace();
		}
	}
}
class for string functions not available in java, but in c++.
package tangible;

//----------------------------------------------------------------------------------------
//	This class provides the ability to simulate various classic C string functions
//	which don't have exact equivalents in the Java framework.
//----------------------------------------------------------------------------------------
public final class StringFunctions
{
	//------------------------------------------------------------------------------------
	//	This method allows replacing a single character in a string, to help convert
	//	C++ code where a single character in a character array is replaced.
	//------------------------------------------------------------------------------------
	public static String changeCharacter(String sourcestring, int charindex, char changechar)
	{
		return (charindex > 0 ? sourcestring.substring(0, charindex) : "")
			+ Character.toString(changechar) + (charindex < sourcestring.length() - 1 ? sourcestring.substring(charindex + 1) : "");
	}

	//------------------------------------------------------------------------------------
	//	This method simulates the classic C string function 'isxdigit' (and 'iswxdigit').
	//------------------------------------------------------------------------------------
	public static boolean isXDigit(char character)
	{
		if (Character.isDigit(character))
			return true;
		else if ("ABCDEFabcdef".indexOf(character) > -1)
			return true;
		else
			return false;
	}

	//------------------------------------------------------------------------------------
	//	This method simulates the classic C string function 'strchr' (and 'wcschr').
	//------------------------------------------------------------------------------------
	public static String strChr(String stringtosearch, char chartofind)
	{
		int index = stringtosearch.indexOf(chartofind);
		if (index > -1)
			return stringtosearch.substring(index);
		else
			return null;
	}

	//------------------------------------------------------------------------------------
	//	This method simulates the classic C string function 'strrchr' (and 'wcsrchr').
	//------------------------------------------------------------------------------------
	public static String strRChr(String stringtosearch, char chartofind)
	{
		int index = stringtosearch.lastIndexOf(chartofind);
		if (index > -1)
			return stringtosearch.substring(index);
		else
			return null;
	}

	//------------------------------------------------------------------------------------
	//	This method simulates the classic C string function 'strstr' (and 'wcsstr').
	//------------------------------------------------------------------------------------
	public static String strStr(String stringtosearch, String stringtofind)
	{
		int index = stringtosearch.indexOf(stringtofind);
		if (index > -1)
			return stringtosearch.substring(index);
		else
			return null;
	}

	//------------------------------------------------------------------------------------
	//	This method simulates the classic C string function 'strtok' (and 'wcstok').
	//------------------------------------------------------------------------------------
	private static String activestring;
	private static int activeposition;
	public static String strTok(String stringtotokenize, String delimiters)
	{
		if (stringtotokenize != null)
		{
			activestring = stringtotokenize;
			activeposition = -1;
		}

		//the stringtotokenize was never set:
		if (activestring == null)
			return null;

		//all tokens have already been extracted:
		if (activeposition == activestring.length())
			return null;

		//bypass delimiters:
		activeposition++;
		while (activeposition < activestring.length() && delimiters.indexOf(activestring.charAt(activeposition)) > -1)
		{
			activeposition++;
		}

		//only delimiters were left, so return null:
		if (activeposition == activestring.length())
			return null;

		//get starting position of string to return:
		int startingposition = activeposition;

		//read until next delimiter:
		do
		{
			activeposition++;
		} while (activeposition < activestring.length() && delimiters.indexOf(activestring.charAt(activeposition)) == -1);

		return activestring.substring(startingposition, activeposition);
	}
}
danpost danpost

2012/12/1

#
I am not sure, but i think your substring logic is off within the cases. For example, in case 0: do you not want str.substring(beg+3, end)? Even if that is not correct, I think you should review the logic of what you have there.
erdelf erdelf

2012/12/1

#
thx danpost. it works without problems now
You need to login to post a reply.