Datentypen extrem viele Fehler nur irgendwie seh ich den Grund ned...

pisco

Bekanntes Mitglied
Hey Leute,

hab gerade ein Übungsbeispiel aus einem Coderbuch gecodet:

Java:
import java.lang.*;

public AsciiImage {
	
	char[][] image;
	
	AsciiImage(int width, int height){
		
		this.image = new char[width][height];
		clear();
	}
	
	
	public void clear(){
		for(int h=0; h<getHeight(); h++){
			for(int w=0; w<getWidth(); w++{
					image[h][w]= ".";//is eigentlich egal ob [h][w] oder [w][h]	
			}	
		}
	}
	
	public int getHeight(){
		return image.length;
	}	
	
	public int getWidth(){
		return image[0].length;
	}
	
	public char getPixel(int x, int y){
		return image[x-1][y-1];//x-1 weil das Array bei 0 anfängt
	}
	
	public void setPixel(int x, int y, char c) {
		image[x][y] = c;//setzt die Pixel für den char c Bsp.: c = H 
	}
	
	public void replace(char oldChar, char newChar){//geht das Array durch und ersetz das alte durch das neue Zeichen
		for(int h=0; h<getHeight();h++){
			for(int w=0; w<getWidth();w++){
				
				if(oldChar == image [w][h]){
					image [w][h] = newChar;
				}
			}
		}
	}
	
	public String toString(){//gibt eine Lesbaredarstellung aus
		String output = "";
		
		for(int h=0; h<getHeight();h++){
			for(int w=0; w<getWidth();w++){
				output+= String.valueOf(image [w][h]);
			}
			output = '\n';
		}
		output = '\n';
		
		return output;
	}
	
	public void transpose(){//Zeilen und Spalten vertauschen
		char [][] c_image = new char [][];
		
		for(int h=0; h<getHeight(); h++){
			for(int w=0; w<getWidth(); w++){
				c_image [w][h] += image[h][w]
			}
		}
		image = c_image;
		
		return image;
	}
	
	public void drawLine(int x0, int y0, int x1, int y1, char c) {//zeichnet eine Linie zwischen den Koordinaten (x0,y0)  und (x1,y1). Anfangs- und Endpunkt sind dabei inkludiert. c spezifiziert das zu verwendende Zeichen. Sie können davon ausgehen, dass nur gültige Koordinaten übergeben werden.
		int dx = x1 - x0;//Differenz von X damit man ungefähr weis wo man sich im Bild befindet
		int dy = y1 - y0;//Differenz von Y
		float k;//Steigung
		
		if ((Math.abs(dy) <= Math.abs(dx)) && (dx >= 0)) {
			k = (float)dy/(float)dx;
			float y = y0;
			
			for (int x = x0; x <= x1; x++) {
				//System.out.println("y="+y + " x:"+x + "  k:" + k);
				setPixel(x, Math.round(y), c);
				y +=k;
			}
		} else if ((Math.abs(dy) <= Math.abs(dx)) && (dx < 0)) {
			k = (float)-dy /(float) dx;
			float y = y0;
			
			for (int x = x0; x >= x1; x--) {
				setPixel(x, Math.round(y), c);
				y+=k;
			}
		} else if ((Math.abs(dy) > Math.abs(dx)) && (dy >= 0)) {
			k = (float)dx /(float) dy;
			float x = x0;
			
			for (int y = y0; y <= y1; y++) {
				setPixel(Math.round(x), y, c);
				x += k;
			}
		} else if ((Math.abs(dy) > Math.abs(dx)) && (dy < 0)) {
			k = (float)-dx / (float)dy;
			float x = x0;
			
			for (int y = y0; y >= y1; y--) {
				setPixel(Math.round(x), y, c);
				x+=k;
			}
		}
	}
	
	//END
}

Exceptions:

oben gehts noch weiter wird aber leider nicht mehr angezeigt:
...
for(int w=0; w<getWidth();w++){
^
AsciiImage.java:59: class, interface, or enum expected
}
^
AsciiImage.java:61: class, interface, or enum expected
}
^
AsciiImage.java:64: class, interface, or enum expected
return output;
^
AsciiImage.java:65: class, interface, or enum expected
}
^
AsciiImage.java:67: class, interface, or enum expected
public void transpose(){//Zeilen und Spalten vertauschen
^
AsciiImage.java:70: class, interface, or enum expected
for(int h=0; h<getHeight(); h++){
^
AsciiImage.java:70: class, interface, or enum expected
for(int h=0; h<getHeight(); h++){
^
AsciiImage.java:70: class, interface, or enum expected
for(int h=0; h<getHeight(); h++){
^
AsciiImage.java:71: class, interface, or enum expected
for(int w=0; w<getWidth(); w++){
^
AsciiImage.java:71: class, interface, or enum expected
for(int w=0; w<getWidth(); w++){
^
AsciiImage.java:77: class, interface, or enum expected
return image;
^
AsciiImage.java:78: class, interface, or enum expected
}
^
AsciiImage.java:80: class, interface, or enum expected
public void drawLine(int x0, int y0, int x1, int y1, char c) {//zeichnet
eine Linie zwischen den Koordinaten (x0,y0) und (x1,y1). Anfangs- und Endpunkt
sind dabei inkludiert. c spezifiziert das zu verwendende Zeichen. Sie k├Ânnen d
avon ausgehen, dass nur g├╝ltige Koordinaten ├╝bergeben werden.
^
AsciiImage.java:82: class, interface, or enum expected
int dy = y1 - y0;//Differenz von Y
^
AsciiImage.java:83: class, interface, or enum expected
float k;//Steigung
^
AsciiImage.java:85: class, interface, or enum expected
if ((Math.abs(dy) <= Math.abs(dx)) && (dx >= 0)) {
^
AsciiImage.java:87: class, interface, or enum expected
float y = y0;
^
AsciiImage.java:89: class, interface, or enum expected
for (int x = x0; x <= x1; x++) {
^
AsciiImage.java:89: class, interface, or enum expected
for (int x = x0; x <= x1; x++) {
^
AsciiImage.java:89: class, interface, or enum expected
for (int x = x0; x <= x1; x++) {
^
AsciiImage.java:92: class, interface, or enum expected
y +=k;
^
AsciiImage.java:93: class, interface, or enum expected
}
^
AsciiImage.java:96: class, interface, or enum expected
float y = y0;
^
AsciiImage.java:98: class, interface, or enum expected
for (int x = x0; x >= x1; x--) {
^
AsciiImage.java:98: class, interface, or enum expected
for (int x = x0; x >= x1; x--) {
^
AsciiImage.java:98: class, interface, or enum expected
for (int x = x0; x >= x1; x--) {
^
AsciiImage.java:100: class, interface, or enum expected
y+=k;
^
AsciiImage.java:101: class, interface, or enum expected
}
^
AsciiImage.java:104: class, interface, or enum expected
float x = x0;
^
AsciiImage.java:106: class, interface, or enum expected
for (int y = y0; y <= y1; y++) {
^
AsciiImage.java:106: class, interface, or enum expected
for (int y = y0; y <= y1; y++) {
^
AsciiImage.java:106: class, interface, or enum expected
for (int y = y0; y <= y1; y++) {
^
AsciiImage.java:108: class, interface, or enum expected
x += k;
^
AsciiImage.java:109: class, interface, or enum expected
}
^
AsciiImage.java:112: class, interface, or enum expected
float x = x0;
^
AsciiImage.java:114: class, interface, or enum expected
for (int y = y0; y >= y1; y--) {
^
AsciiImage.java:114: class, interface, or enum expected
for (int y = y0; y >= y1; y--) {
^
AsciiImage.java:114: class, interface, or enum expected
for (int y = y0; y >= y1; y--) {
^
AsciiImage.java:116: class, interface, or enum expected
x+=k;
^
AsciiImage.java:117: class, interface, or enum expected
}
^
70 errors


Woran liegts bei meinem code?

schließlich sind die angezeigten Fehler nicht vorhanden...

gruss und dank euch schon mal für eure Antwort
 
[JAVA=3]public AsciiImage {[/code]

Merkst du, was fehlen könnte?


EDIT:
Wenn du den obigen Fehler korrigiert hast, bleiben noch ein paar:

[JAVA=16]for(int w=0; w<getWidth(); w++{
[/code]
Schließende runde Klammer fehlt.

[JAVA=64]char [][] c_image = new char [][];[/code]
Größenangabe des Arrays fehlt.

[JAVA=68]c_image [w][h] += image[h][w][/code]
Strichpunkt fehlt.
 
Zuletzt bearbeitet:
Als erstes heißt es public class AsciiImage und nicht nur public

dann in der Methode clear()
die for schleife muss mit einer Klammer geschlossen werden und

image[h][w]= ".";
ersetzen durch image[h][w]= '.';


in toString

output = '\n'; ersetzen durch output += "\n";

wobei du hier eher die Klasse StringBuilder ansehen solltest.


public void transpose(){//Zeilen und Spalten vertauschen
char [][] c_image = new char [][];
ersetuen durch
char [][] c_image = new char [getWidth()][getHeight()];
dann musst du dier hier auch noch die return anweisung neu anschauen, da du ja eigentlich

als return type void hast



so das wars mal für den Anfang
 
böse ist auch
for(int w=0; w<getWidth(); w++{

->
for(int w=0; w<getWidth(); w++) {


die vielen Fehler deuten immer auf schlechte Klammerung hin, der Code wird nicht als das interpretiert was es ist sondern steht an falscher Stelle,
fast jede normale Codezeile außerhalb von Methoden oder Konstruktoren ist grundsätzlich ein Fehler
 

Zurück
Oben