12 Replies - 6193 Views - Last Post: 10 May 2011 - 11:19 AM Rate Topic: -----

#1 PureLily  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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?
Was This Post Helpful? 0
  • +
  • -

#3 PureLily  Icon User is offline

  • New D.I.C Head

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

Re: Q: Draw square by any symbol.

Posted 08 May 2011 - 04:13 AM

View PostDavide 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
Was This Post Helpful? 0
  • +
  • -

#4 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1275
  • View blog
  • Posts: 2,840
  • 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();
Was This Post Helpful? 1
  • +
  • -

#5 PureLily  Icon User is offline

  • New D.I.C Head

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

Re: Q: Draw square by any symbol.

Posted 08 May 2011 - 11:40 PM

View PostCasiOo, 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

Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10186
  • View blog
  • Posts: 37,613
  • 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.

Also, you name your Scanner ask, but read in in.nextLine(). Don't you mean to read ask.nextLine() instead?
Was This Post Helpful? 1
  • +
  • -

#7 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7294
  • View blog
  • Posts: 12,138
  • Joined: 19-March 11

Re: Q: Draw square by any symbol.

Posted 09 May 2011 - 07:10 PM

View PostPureLily, on 09 May 2011 - 01:40 AM, said:

View PostCasiOo, 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?
Was This Post Helpful? 1
  • +
  • -

#8 PureLily  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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.. :hehe: )
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
Was This Post Helpful? 0
  • +
  • -

#9 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10186
  • View blog
  • Posts: 37,613
  • 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().
Was This Post Helpful? 0
  • +
  • -

#10 PureLily  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#11 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10186
  • View blog
  • Posts: 37,613
  • 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"; 


Was This Post Helpful? 1
  • +
  • -

#12 PureLily  Icon User is offline

  • New D.I.C Head

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

Re: Q: Draw square by any symbol.

Posted 10 May 2011 - 10:45 AM

thank u a lot :flowers:
Was This Post Helpful? 0
  • +
  • -

#13 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10186
  • View blog
  • Posts: 37,613
  • Joined: 27-December 08

Re: Q: Draw square by any symbol.

Posted 10 May 2011 - 11:19 AM

Glad I could help! :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1