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 11 of 11

Thread: Can't Enter Info from Keyboard in a Method

  1. #1
    Junior Member
    Join Date
    Jun 2014
    Posts
    5
    My Mood
    Stressed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Can't Enter Info from Keyboard in a Method

    I'm sure I am doing something stupid but I can't find it. I want to enter the triangle data in the method and every time I try to compile the program I get the error message--cannot find symbol. Please tell me what I have done wrong.

    import java.io.*;
    import java.util.*;

    public class trianglemethod {

    public static void main(String [] args)
    {
    Scanner kbreader = new Scanner (System.in);
    int choice = 0;
    String ans = "y";

    do
    {
    System.out.println("Are you entering");
    System.out.println("1. leg and leg");
    System.out.println("2. leg and hypotenuse");
    choice = kbreader.nextInt();
    kbreader.nextLine();
    if (choice == 1)
    legs();
    // else
    // hypo();
    kbreader.nextLine();
    System.out.println("Would you like to run this again? (Y/N)");
    ans = kbreader.nextLine();
    }
    while (ans.equalsIgnoreCase("Y"));
    }//main

    public static void legs()
    {
    double leg1=0;
    double leg2=0;
    double hypot=0;
    System.out.println("Enter the values for leg1 and leg2");
    leg1 = kbreader.nextDouble(); //the error shows up on this line
    leg2 = kbreader.nextDouble(); // and this line
    hypot = Math.sqrt(leg1*leg1 + leg2 * leg2);
    System.out.println("The sides of the triangle are: "+leg1+" "+leg2+" "+ hypot);
    }



    }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Can't Enter Info from Keyboard in a Method

    Welcome to the forum! Please read this topic to learn how to post code in code or highlight tags and other useful info for new members.

    Please post your code correctly per the above link.

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Can't Enter Info from Keyboard in a Method

    I get the error message--cannot find symbol.
    Please post the full text of the error message that shows what symbol is not being found and on what line it is coded.
    If you don't understand my answer, don't ignore it, ask a question.

  4. #4
    Junior Member
    Join Date
    Jun 2014
    Posts
    5
    My Mood
    Stressed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Can't Enter Info from Keyboard in a Method

    Quote Originally Posted by Norm View Post
    Please post the full text of the error message that shows what symbol is not being found and on what line it is coded.
    Norm,

    This is the error message I get.

    error: cannot find symbol line 44
    error: cannot find symbol line 45

    I "commented" exactly where the errors are in my original post. I'm extremely frustrated with this. I am working on lesson plans and can't figure this out.

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Can't Enter Info from Keyboard in a Method

    Sorry, I rarely try to read unformated code (ie not wrapped in code tags).
    What are the symbols that are not found?

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE GOES HERE
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Junior Member
    Join Date
    Jun 2014
    Posts
    5
    My Mood
    Stressed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Can't Enter Info from Keyboard in a Method

    [highlight = Java]
    import java.io.*;
    import java.util.*;
     
    public class trianglemethod {
     
        public static void main(String [] args) 
        {
        	Scanner kbreader = new Scanner (System.in);
        	int choice = 0;
        	String ans = "y";
     
        	do
        	{
        	System.out.println("Are you entering");
        	System.out.println("1.  leg and leg");
        	System.out.println("2.  leg and hypotenuse");
        	choice = kbreader.nextInt();
        	kbreader.nextLine();
        	if (choice == 1)
        		legs();
        //	else 
        //		hypo();
            kbreader.nextLine();
        	System.out.println("Would you like to run this again? (Y/N)");
        	ans = kbreader.nextLine();
        	}
        	while (ans.equalsIgnoreCase("Y"));
        }//main
     
        public static void legs()
        {
        	double leg1=0;
        	double leg2=0;
        	double hypot=0;
        	System.out.println("Enter the values for leg1 and leg2");
        	leg1 = kbreader.nextDouble();
        	leg2 = kbreader.nextDouble();
        	hypot = Math.sqrt(leg1*leg1 + leg2 * leg2);
        	System.out.println("The sides of the triangle are:  "+leg1+"  "+leg2+"  "+ hypot);
        }
     
     
     
    }
    [/highlight]

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Can't Enter Info from Keyboard in a Method

    Ok, that's easier to read.
    What are the symbols that are not found?
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Junior Member
    Join Date
    Jun 2014
    Posts
    5
    My Mood
    Stressed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Can't Enter Info from Keyboard in a Method

    I am using JCreator. The error message doesn't specify what the symbol is. I just get error: cannot find symbol for lines 44 and 45.

    I just removed the lines to enter leg1 and leg2 from the method. I moved them to the main method and sent them to the method and it works fine.

    Why can't I enter them in the method?
    Last edited by crazyteacher; June 21st, 2014 at 09:12 PM.

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Can't Enter Info from Keyboard in a Method

    . The error message doesn't specify what the symbol is.
    If your IDE doesn't tell what symbol is not found, that is going to make it hard to write programs. That is a common enough error that you need to figure out how to make your IDE tell you what symbol is not found.

    Is there some visual indication of what symbol is not found? A red line or a ^ under the line?

    I use the javac command to compile. It gives me this:
    The message shows the source with a ^ under the location of the error.
    TestSorts.java:138: cannot find symbol
    symbol  : variable var
    location: class TestSorts
             var = 2;
             ^
    If you don't understand my answer, don't ignore it, ask a question.

  10. #10
    Junior Member
    Join Date
    Jun 2014
    Posts
    5
    My Mood
    Stressed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Can't Enter Info from Keyboard in a Method

    Usually it tells me something like ; expected and the line number. I'm so stressed over this I keep messing with it and I have confused myself even more now. If I put the line Scanner kbreader = new Scanner (System.in); inside the method it works just fine. Any idea why?

  11. #11
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Can't Enter Info from Keyboard in a Method

    By convention in Java, class names begin with capital letters and method and variable names begin with lowercase letters. Please follow Java's naming conventions.
    Any idea why?
    Because kbreader in your original code is declared inside main() and is therefore 'local' to main(), and the 'symbol kbreader' is unknown (undeclared, undefined, etc.) inside the method legs(). Once you declared and initialized A SECOND variable named 'kbreader' local to the method legs(), the error went away. Even though the error went away and your program ran without error, the original condition remained.

Similar Threads

  1. keyboard scanner to take users input and enter students information method
    By foresboo in forum What's Wrong With My Code?
    Replies: 0
    Last Post: February 7th, 2013, 04:39 PM
  2. JButton with "Enter Key" keyboard action
    By chronoz13 in forum Java Swing Tutorials
    Replies: 2
    Last Post: March 14th, 2012, 06:49 AM
  3. How do i store input from the keyboard into a method?
    By javaStarter in forum Java Theory & Questions
    Replies: 2
    Last Post: December 23rd, 2011, 05:50 PM
  4. JButton with "Enter Key" keyboard action
    By chronoz13 in forum Java Code Snippets and Tutorials
    Replies: 0
    Last Post: January 26th, 2010, 10:53 AM