What's wrong with this code?

What's wrong with this code?
[code]
import java.io.*;
public class Diamond
{
static char character;
static int width;
static int current;
static int space;
static String line;

public static void main(String[] args) throws IOException
{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(new FileWriter("diamond.dat"));
System.out.print("Character please: ");
character = in.readLine();
System.out.print("Width of diamond please: ");
width = Integer.parseInt(in.readLine());
if ((width / 2) == 0)
{
width++;
}
else
{
current = 1;
while (width >= current) // top half of diamond
{
space = ((width - current) - 1) / 2;
out.println(current * character);
current = current + 2;
}
current = width;
while (current >= 1) // bottom half of diamond
{
space = ((width - current) - 1) / 2;
out.println(current * character);
current = current - 2;
}
}
}
}
[/code]
Here's the error:
[code]
Diamond.java:15: incompatible types
found : java.lang.String
required: char
character = in.readLine();
^
1 error
[/code]
How do I fix the error, and center the lines, so that it looks like a diamond?

TIA,
Nathan Pinno
Swat spam before it fills your inbox!

Comments

  • : What's wrong with this code?
    : [code]
    : import java.io.*;
    : public class Diamond
    : {
    : static char character;
    : static int width;
    : static int current;
    : static int space;
    : static String line;
    :
    : public static void main(String[] args) throws IOException
    : {
    : BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    : PrintWriter out = new PrintWriter(new FileWriter("diamond.dat"));
    : System.out.print("Character please: ");
    : character = in.readLine();
    : System.out.print("Width of diamond please: ");
    : width = Integer.parseInt(in.readLine());
    : if ((width / 2) == 0)
    : {
    : width++;
    : }
    : else
    : {
    : current = 1;
    : while (width >= current) // top half of diamond
    : {
    : space = ((width - current) - 1) / 2;
    : out.println(current * character);
    : current = current + 2;
    : }
    : current = width;
    : while (current >= 1) // bottom half of diamond
    : {
    : space = ((width - current) - 1) / 2;
    : out.println(current * character);
    : current = current - 2;
    : }
    : }
    : }
    : }
    : [/code]
    : Here's the error:
    : [code]
    : Diamond.java:15: incompatible types
    : found : java.lang.String
    : required: char
    : character = in.readLine();
    : ^
    : 1 error
    : [/code]
    : How do I fix the error, and center the lines, so that it looks like a diamond?
    :
    : TIA,
    : Nathan Pinno
    : Swat spam before it fills your inbox!
    :
    Change character into a String type or only take the 0th character from the readLine(). First print a number of spaces to "fill up" the line before drawing the character.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion