Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Problem with Scanner

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Problem with Scanner

    Hi!

    I am new to this forum. I am a beginner in Java programming and have a problem with one of my assignments.

    I need the user to enter the size of a diamond (through the Scanner) to be printed out but the below code does not compile ("cannot find symbol: variable Size. I want the user enter the size only once at the beginning of the program and then I want to use it within different methods drawing different parts of the figure. Do I have to define a variable inside each of the methods? Any ideas how to fix this?
    Many thanks in advance!

    PHP Code:
     import java.util.*; 

    public class 
    Question2 

        public static 
    void main(String[] args)         { 
            
    Scanner keyboard = new Scanner(System.in); 
            
    System.out.print("Please enter the size of diamond you want to print out: "); 
            
    int Size keyboard.nextInt(); 
            
    line(); 
            
    drawtop(); 
            
    middleline(); 
            
    drawbtm(); 
            
    line(); 


        } 

        public static 
    void line() { 
              
    System.out.print("|"); 
            for (
    int i 1<= Size 2i++) { 
                
    System.out.print("-"); 
            } 
            
    System.out.print("*"); 
            for (
    int i 1<= Size 2i++) { 
                
    System.out.print("-"); 
            } 
            
    System.out.print("|"); 
            
    System.out.println(" "); 
        } 


  2. #2
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: Problem with Scanner

    Quote Originally Posted by Rafal75 View Post
    Hi!

    I am new to this forum. I am a beginner in Java programming and have a problem with one of my assignments.

    I need the user to enter the size of a diamond (through the Scanner) to be printed out but the below code does not compile ("cannot find symbol: variable Size. I want the user enter the size only once at the beginning of the program and then I want to use it within different methods drawing different parts of the figure. Do I have to define a variable inside each of the methods? Any ideas how to fix this?
    Many thanks in advance!

    PHP Code:
     import java.util.*; 

    public class 
    Question2 

        public static 
    void main(String[] args)         { 
            
    Scanner keyboard = new Scanner(System.in); 
            
    System.out.print("Please enter the size of diamond you want to print out: "); 
            
    int Size keyboard.nextInt(); 
            
    line(); 
            
    drawtop(); 
            
    middleline(); 
            
    drawbtm(); 
            
    line(); 


        } 

        public static 
    void line() { 
              
    System.out.print("|"); 
            for (
    int i 1<= Size 2i++) { 
                
    System.out.print("-"); 
            } 
            
    System.out.print("*"); 
            for (
    int i 1<= Size 2i++) { 
                
    System.out.print("-"); 
            } 
            
    System.out.print("|"); 
            
    System.out.println(" "); 
        } 
    Hello Rafal75!
    If you want to use Size in multiple methods you can declare it in the same level with the methods (in other words inside the class but outside the methods) - be careful with static fields and methods. And also you should better start your variable name with lower case by convention.

  3. #3
    Junior Member
    Join Date
    May 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with Scanner

    Quote Originally Posted by andreas90 View Post
    Hello Rafal75!
    If you want to use Size in multiple methods you can declare it in the same level with the methods (in other words inside the class but outside the methods) - be careful with static fields and methods. And also you should better start your variable name with lower case by convention.
    since it appears you are not modifing the variable inside your methods you could also simply pass it to each of your methods
    eg:
     import java.util.*;  
     
    public class Question2 {  
     
        public static void main(String[] args)         {  
            Scanner keyboard = new Scanner(System.in);  
            System.out.print("Please enter the size of diamond you want to print out: ");  
            int size = keyboard.nextInt();  
            line(size);  
            drawtop(size);  
            middleline(size);  
            drawbtm(size);  
            line(size);  
     
     
        }  
     
        public static void line(int size) {  
              System.out.print("|");  
            for (int i = 1; i <= size / 2; i++) {  
                System.out.print("-");  
            }  
            System.out.print("*");  
            for (int i = 1; i <= size / 2; i++) {  
                System.out.print("-");  
            }  
            System.out.print("|");  
            System.out.println(" ");  
        }

Similar Threads

  1. I am Facing problem in Scanner Class
    By snithishkumar in forum Java SE APIs
    Replies: 2
    Last Post: October 11th, 2011, 09:39 AM
  2. Scanner problem - need help
    By destructobob in forum What's Wrong With My Code?
    Replies: 5
    Last Post: February 23rd, 2011, 02:08 AM
  3. [SOLVED] Problem with Scanner ?
    By derekeverett in forum What's Wrong With My Code?
    Replies: 6
    Last Post: March 19th, 2010, 04:34 AM
  4. Simple scanner problem
    By Sinensis in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: September 20th, 2009, 09:01 PM
  5. Homework problem (scanner problems)
    By TommyFiz in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: September 20th, 2009, 06:10 PM

Tags for this Thread