plotString

Fabian04088

Aktives Mitglied
* Writes a string to the plottingArea.
* The first character of the string is put to the coordinates (x, y), the rest of the string is written horizontally starting from there and going left to right - as long as it fits into the canvas.
*
* @param x x coordinate of the start of the string
* @param y y coordinate of the start of the string
* @param s the string to write
*/
public static void plotString(int x, int y, String s) {



Bei der Plot String muss ich jedes Zeichen des Strings einzelne setzten und dafür würde ich mir eine for Schleife vorstellen mit string.length() aber ich weiss nicht ganz genau wie ich das schreiben kann
 
Zuletzt bearbeitet von einem Moderator:

mihe7

Top Contributor
Ja, bis auf die Tatsache, dass length kein Attribut sondern eine Methode der Klasse String ist und daher mit Klammern aufgerufen werden muss, stimmt das schon mal.

So, als nächstes: die Klasse String hat eine Methode charAt(ix), die das Zeichen liefert, das sich an dem gegebenen Index (ix) befindet. Wie würde die for-Schleife aussehen, wenn Du jedes Zeichen des Strings abrufst?
 

mihe7

Top Contributor
Nein, schreib mal eine komplette for-Schleife, inkl aller Variablen, die Du brauchst. Ich frage mich z.B. gerade, wo Dein ix herkommt.
 

mihe7

Top Contributor
Du hast die for-Schleife doch oben schon (fast richtig) geschrieben. Drei Tipps:
1. Du musst die Korrektur aus #4 berücksichtigen
2. In der Schleife brauchst Du nur jedes Zeichen des Strings abzurufen (Methode charAt richtig verwenden)
3. Innerhalb der Schleife steht Dir das aktuelle i zur Verfügung
 

mihe7

Top Contributor
Nein, eine for-Schleife besteht aus ein paar Teilen:
Java:
for (initialisierung; bedingung; inkrement) {
     anweisungen;
}
Zuerst wird die Initialisierung ausgeführt, anschließend wird der Rumpf so lange wiederholt, bis die bedingung nicht mehr gilt. Nach jeder Wiederholung wird inkrement ausgeführt.

Mit einer while-Schleife sähe das so aus:
Java:
initialisierung;
while (bedingung) {
    anweisungen;
    inkrement;
}

Es wird immer ein Schritt nach dem anderen ausgeführt.

Oben hattest Du doch schon
Java:
for( int i = 0; i < s.length; i++)
da fehlen einfach nur die Klammern nach length, d. h. die leere Schleife sähe so aus:
Java:
for( int i = 0; i < s.length(); i++) {

}
Im Rumpf der Schleife sollst Du jetzt einfach mit der Methode charAt das aktuelle Zeichen abrufen.
 

Wurstkopp

Bekanntes Mitglied
Ok. Ich frage, da dein Code so mehrere Fehler beinhaltet, auf die dich die IDE bereits hinweisen müsste.
1. Das mit der while Schleife war nur ein aufgezeigte Alternative. Beides hier zu verschachteln macht hier wenig Sinn. Entscheide dich für eine Variante (Die For Schleife sieht doch schon ganz gut aus).
2. Google nach "java string charat". Oder allgemein, wie ruft man eine Methode eines Strings auf?
 

Fabian04088

Aktives Mitglied
for( int i = 0; i < s.length(); i++)
System.out.println(s.charAt(i));

okay ich hab jetzt nachgeschaut und das kling am sinnvollsten für mich ich hoffe das ist richtig
 
K

kneitzel

Gast
Also Du bist jetzt hier mit mehreren Themen durchs Forum durch. Da sollte doch langsam ein gewisses Verständnis existieren bezüglich:
- Variablen
- Aufrufen von Methoden
- Kontrollstrukturen

for( int i = 0; i < s.length(); i++)
System.out.println(s.charAt(i));

okay ich hab jetzt nachgeschaut und das kling am sinnvollsten für mich ich hoffe das ist richtig
Was heißt "nachgeschaut"? Den Code auf Google gefunden und keine Ahnung, was er macht?

Oder hast Du den Code bei Dir einmal ausprobiert? Also ein kleines Programm geschrieben und geschaut, was da passiert?
Denn genau da wirst Du nicht drum herum kommen: Du musst Dinge ausprobieren und damit "spielen", wenn Du es wirklich verstehen willst.
 

insanezulu

Mitglied

Fabian04088

Aktives Mitglied
public class PlottingArea {

private int width, height;

private char[][] canvas = null;

private final char EMPTY_CHAR = '#';

public PlottingArea(int width, int height) {
this.width = width;
this.height = height;

canvas = new char[height][width];
for (int y = 0; y < height; y++)
for (int x = 0; x < width; x++)
setChar(x, y, EMPTY_CHAR);
}

public int getWidth() {
return width;
}

public int getHeight() {
return height;
}

public char[][] plotString(int xStart, int yStart, String s) {
// First line
int firstLineLength = width - xStart; // get empty space in first line
String firstLine = firstLineLength < s.length() ? s.substring(0, firstLineLength) : s;
plotHorizontalLine(xStart, yStart, firstLine); // plot it

int lineStartIndex = firstLineLength; // get the new start index

// Other lines
for (int i = yStart + 1; i < height; i++) { // height +1
if (lineStartIndex >= s.length())
return canvas; // String has already fully been plotted
int lineEndIndex = s.length() - lineStartIndex < width ? lineStartIndex + (s.length() - lineStartIndex) : lineStartIndex + width;
String line = s.substring(lineStartIndex, lineEndIndex); // get the new line
plotHorizontalLine(0, i, line); // plot it
lineStartIndex += width; // set the new start index
}

// Plotting finished
return canvas;
}

private void plotHorizontalLine(int x, int y, String s) {
int currentChar = 0;
for (int i = x; i < width; i++) {
if (currentChar < s.length())
setChar(i, y, s.charAt(currentChar++));
}
}

/**
* Inserts the character at his appropriate coordinates
*
* @param x x coordinate of the character
* @param y y coordinate of the character
* @param c the character to be inserted
*/
private void setChar(int x, int y, char c) {
canvas[y][x] = c;
}

}


private int width, height;

private char[][] canvas = null;

private final char EMPTY_CHAR = '#';

public PlottingArea(int width, int height) {
this.width = width;
this.height = height;

canvas = new char[height][width];
for (int y = 0; y < height; y++)
for (int x = 0; x < width; x++)
setChar(x, y, EMPTY_CHAR);
}

public int getWidth() {
return width;
}

public int getHeight() {
return height;
}

public char[][] plotString(int xStart, int yStart, String s) {
// First line
int firstLineLength = width - xStart; // get empty space in first line
String firstLine = firstLineLength < s.length() ? s.substring(0, firstLineLength) : s;
plotHorizontalLine(xStart, yStart, firstLine); // plot it

int lineStartIndex = firstLineLength; // get the new start index

// Other lines
for (int i = yStart + 1; i < height; i++) { // height +1
if (lineStartIndex >= s.length())
return canvas; // String has already fully been plotted
int lineEndIndex = s.length() - lineStartIndex < width ? lineStartIndex + (s.length() - lineStartIndex) : lineStartIndex + width;
String line = s.substring(lineStartIndex, lineEndIndex); // get the new line
plotHorizontalLine(0, i, line); // plot it
lineStartIndex += width; // set the new start index
}

// Plotting finished
return canvas;
}

private void plotHorizontalLine(int x, int y, String s) {
int currentChar = 0;
for (int i = x; i < width; i++) {
if (currentChar < s.length())
setChar(i, y, s.charAt(currentChar++));
}
}

/**
* Inserts the character at his appropriate coordinates
*
* @param x x coordinate of the character
* @param y y coordinate of the character
* @param c the character to be inserted
*/
private void setChar(int x, int y, char c) {
canvas[y][x] = c;
}

}


das wollte ich schreiben bis ich gemerkt hat des mein System es nicht Akzeptiert :D
 
Ähnliche Java Themen
  Titel Forum Antworten Datum
F plotString Java Basics - Anfänger-Themen 1

Ähnliche Java Themen

Neue Themen


Oben