# Q: Draw square by any symbol.

Page 1 of 1

## 12 Replies - 9859 Views - Last Post: 10 May 2011 - 11:19 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=231118&amp;s=02497499e232fed21695b4ac4f5d096b&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 PureLily

• New D.I.C Head

Reputation: 0
• Posts: 10
• Joined: 25-March 11

# Q: Draw square by any symbol.

Posted 08 May 2011 - 03:44 AM

Hello everyone...
yesterday I had task to draw a square by (*)
this is the way I done it.

import java.util.Scanner;
public class drawS{
public static void main(String[]args){
Scanner ask = new Scanner(System.in);
int length = ask.nextInt();
int i;
drowLine(length);
System.out.println();
for (i=0;i<length-2;i++){
drowEmptyLine(length);
System.out.println();
}
drowLine(length);
}

public static void drowLine(int n){
for (int i=0;i<n;i++){
System.out.print('*');
}
}

public static void drowEmptyLine(int n){
System.out.print('*');
for(int i=0; i< n-2; i++){
System.out.print(" ");
}
System.out.print('*');
}
}

The the teacher asked us to make the user choose the symbol to draw the square..
Here is my try but I faced some problems

import java.util.Scanner;
public class anySymbol{
public static void main(String[]args){
Scanner ask = new Scanner(System.in);

System.out.println("Enter Symbol");
String Letter= in.nextLine();

System.out.println("Enter Length");
int length = ask.nextInt();
int i;
drowLine(length);
System.out.println();
for (i=0; i<length-2 ;i++){
drowEmptyLine(length);
System.out.println();
}
drowLine(length);
}

public static void drowLine(int n){
String Symbol = in.nextLine();
for (int i=0;i<n;i++){
System.out.print(Symbol);
}
}

public static void drowEmptyLine(int n){
String Symbol = in.nextLine();
System.out.print(Symbol);
for(int i=0; i< n-2; i++){
System.out.print(" ");
}
System.out.print(Symbol);
}
}

this is the error :
3 errors found:
[line: 8]
cannot find symbol
symbol : variable in

[line: 25]
cannot find symbol
symbol : variable in
location: class anySymbol

[line: 33]
cannot find symbol
symbol : variable in
location: class anySymbol

This post has been edited by PureLily: 08 May 2011 - 03:46 AM

Is This A Good Question/Topic? 0

## Replies To: Q: Draw square by any symbol.

### #2 Davide Vitelaru

• New D.I.C Head

Reputation: 0
• Posts: 6
• Joined: 06-May 11

## Re: Q: Draw square by any symbol.

Posted 08 May 2011 - 04:02 AM

You are printing Symbol instead of Letter. I assume Letter is the user's symbol of choice, right?

### #3 PureLily

• New D.I.C Head

Reputation: 0
• Posts: 10
• Joined: 25-March 11

## Re: Q: Draw square by any symbol.

Posted 08 May 2011 - 04:13 AM

Davide Vitelaru, on 08 May 2011 - 04:02 AM, said:

You are printing Symbol instead of Letter. I assume Letter is the user's symbol of choice, right?

Sorry I don't really get your question
But I give the user the ability to choose a character to draw a square
try the first program to get my idea

### #4 CasiOo

• D.I.C Lover

Reputation: 1514
• Posts: 3,375
• Joined: 05-April 11

## Re: Q: Draw square by any symbol.

Posted 08 May 2011 - 04:33 AM

Scanner ask = new Scanner(System.in);
String Letter= in.nextLine();
String Symbol = in.nextLine();

### #5 PureLily

• New D.I.C Head

Reputation: 0
• Posts: 10
• Joined: 25-March 11

## Re: Q: Draw square by any symbol.

Posted 08 May 2011 - 11:40 PM

CasiOo, on 08 May 2011 - 04:33 AM, said:

Scanner ask = new Scanner(System.in);
String Letter= in.nextLine();
String Symbol = in.nextLine();

This is the the same to what I did!!

Will somebody HELP ME here!!

This post has been edited by PureLily: 08 May 2011 - 11:39 PM

### #6 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 11447
• Posts: 43,144
• Joined: 27-December 08

## Re: Q: Draw square by any symbol.

Posted 09 May 2011 - 05:09 PM

If you want to use your Scanner in multiple methods, you either have to pass it as a parameter (and modify the method headers), or declare it in your class as a static variable. The latter option will require you to declare it in the class, but not inside any method.

### #7 jon.kiparsky

• Pancakes!

Reputation: 8930
• Posts: 15,435
• Joined: 19-March 11

## Re: Q: Draw square by any symbol.

Posted 09 May 2011 - 07:10 PM

PureLily, on 09 May 2011 - 01:40 AM, said:

CasiOo, on 08 May 2011 - 04:33 AM, said:

Scanner ask = new Scanner(System.in);
String Letter= in.nextLine();
String Symbol = in.nextLine();

This is the the same to what I did!!

Will somebody HELP ME here!!

He is helping you. That is what you did. Do you see why it doesn't work? What is "in"?You have a scanner called "ask", but what is "in"?

And that's what the compiler is asking you:

This:
cannot find symbol
symbol : variable in

is the compiler's way of saying "Help! What is 'in'?"

I thnk you have the logic mostly right, but you're missing a few key things.
I see that you have two methods for drawing lines, and they both ask the user for input. Why is that?

### #8 PureLily

• New D.I.C Head

Reputation: 0
• Posts: 10
• Joined: 25-March 11

## Re: Q: Draw square by any symbol.

Posted 10 May 2011 - 09:33 AM

Thanks for helping me
both of your replies are great because u r explaining to me the causes of the error (I guess.. )
but my problem is that English is not my first language so I don't really get what u r saying
I`ll be selfish and ask u to guide me what to do exactly..
I have removed both of ask in the button of my work..now I got 1 error
import java.util.Scanner;
public class anySymbol{
public static void main(String[]args){
Scanner ask = new Scanner(System.in);

System.out.println("Enter Symbol");
String Letter= in.nextLine();

System.out.println("Enter Length");
int length = ask.nextInt();
int i;
drawLine(length);
System.out.println();
for (i=0; i<length-2 ;i++){
drawEmptyLine(length);
System.out.println();
}
drawLine(length);
}

public static void drawLine(int n){
String Symbol;
for (int i=0;i<n;i++){
System.out.print(Symbol);
}
}

public static void drawEmptyLine(int n){
String Symbol ;
System.out.print(Symbol);
for(int i=0; i< n-2; i++){
System.out.print(" ");
}
System.out.print(Symbol);
}
}

I get :
1 error found:
[line: 8]
cannot find symbol
symbol : variable in
location: class anySymbol

### #9 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 11447
• Posts: 43,144
• Joined: 27-December 08

## Re: Q: Draw square by any symbol.

Posted 10 May 2011 - 09:35 AM

Your Scanner is named ask. You are reading in.nextLine(). The variable in doesn't exist. You want to read ask.nextLine().

### #10 PureLily

• New D.I.C Head

Reputation: 0
• Posts: 10
• Joined: 25-March 11

## Re: Q: Draw square by any symbol.

Posted 10 May 2011 - 10:29 AM

yes I have tried to do that but then 2 errors appeared!!

2 errors found:
[line: 28]
variable Symbol might not have been initialized
[line: 35]
variable Symbol might not have been initialized

### #11 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 11447
• Posts: 43,144
• Joined: 27-December 08

## Re: Q: Draw square by any symbol.

Posted 10 May 2011 - 10:39 AM

You have declared Symbol in your methods, but you haven't initialized it by assigning it a value. That is why you are getting that error. See my sample below, with the comments:
String x; //declaring a variable
x = "this is a value"; //initializing a variable

//declare and initialize a variable in one line
String y = "this is a test";

### #12 PureLily

• New D.I.C Head

Reputation: 0
• Posts: 10
• Joined: 25-March 11

## Re: Q: Draw square by any symbol.

Posted 10 May 2011 - 10:45 AM

thank u a lot

### #13 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 11447
• Posts: 43,144
• Joined: 27-December 08

## Re: Q: Draw square by any symbol.

Posted 10 May 2011 - 11:19 AM

Glad I could help!