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

Thread: Cannot Find Variable/Symbol Errors... why?

  1. #1
    Member
    Join Date
    Jun 2011
    Posts
    182
    My Mood
    Where
    Thanks
    15
    Thanked 8 Times in 8 Posts

    Default Cannot Find Variable/Symbol Errors... why?

    I keep getting errors from the compiler saying it can't find the symbols a,b and c in this class. Unless I'm blind, I think I clearly defined them in the first part of the code.
    public class Algebra {
      public void linearAlg() {
        System.out.println("Enter variables a b and c in ax+b=c");
        try {
          CalcInput user = new CalcInput();
          String userInput = user.getUserInput(" ");
          double a = Double.parseDouble(userInput);
          String userInput2 = user.getUserInput(" ");
          double b = Double.parseDouble(userInput2);
          String userInput3 = user.getUserInput(" ");
          double c = Double.parseDouble(userInput3);
        } catch(NumberFormatException e) {
          System.out.println("Your input was invalid.  Only numeric real entries are accepted.");
          System.out.println("Do not use operators.  Enter the a,b and c variables in ax+b=c");
          System.out.println("");
        } catch(NullPointerException e) {
          System.out.println("There must be an input!");
          System.out.println("");
        }
        if(b < 0) {
          AlgebraHelper helper = new AlgebraHelper();
          helper.setVars(b,c);
          double addNum = helper.addThis();
          helper.setVars(addNum,c);
          double divNum = helper.divThis();
          System.out.println(divNum);
        } else if(b == 0) {
          AlgebraHelper helper = new AlgebraHelper();
          helper.setVars(a,c);
          double divNum = helper.divThis();
          System.out.println(divNum);
        } else {
          AlgebraHelper helper = new AlgebraHelper();
          helper.setVars(b,c);
          double subNum = helper.subThis();
          helper.setVars(subNum,c);
          double divNum = helper.divThis();
          System.out.println(divNum);
        }
      }
    }

    Is it because the a,b,c variables are being used inside of a loop?

    If that is the case, how do I get the previously defined symbols to be recognized in the loop?


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Cannot Find Variable/Symbol Errors... why?

    Read this:

    Help with program

  3. #3
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Cannot Find Variable/Symbol Errors... why?

    Hello bgroenks96.

    This is a simple error.

    You get this because you are attempting to call these variables which are defined within the try/catch. They are unreachable.

    To demonstrate this, you can see how the error stops when you remove the try/catch blocks.

    public class Algebra {
    	public void linearAlg() {
     
    		System.out.println("Enter variables a b and c in ax+b=c");
     
    			CalcInput user = new CalcInput();
    			String userInput = user.getUserInput(" ");
    			double a = Double.parseDouble(userInput);
    			String userInput2 = user.getUserInput(" ");
    			double b = Double.parseDouble(userInput2);
    			String userInput3 = user.getUserInput(" ");
    			double c = Double.parseDouble(userInput3);
     
    		if (b < 0) {
    			AlgebraHelper helper = new AlgebraHelper();
    			helper.setVars(b, c);
    			double addNum = helper.addThis();
    			helper.setVars(addNum, c);
    			double divNum = helper.divThis();
    			System.out.println(divNum);
    		} else if (b == 0) {
    			AlgebraHelper helper = new AlgebraHelper();
    			helper.setVars(a, c);
    			double divNum = helper.divThis();
    			System.out.println(divNum);
    		} else {
    			AlgebraHelper helper = new AlgebraHelper();
    			helper.setVars(b, c);
    			double subNum = helper.subThis();
    			helper.setVars(subNum, c);
    			double divNum = helper.divThis();
    			System.out.println(divNum);
    		}
    	}
    }

    To fix, just place the if statements within the try/catch like so:

    public class Algebra {
      public void linearAlg() {
        System.out.println("Enter variables a b and c in ax+b=c");
        try {
          CalcInput user = new CalcInput();
          String userInput = user.getUserInput(" ");
          double a = Double.parseDouble(userInput);
          String userInput2 = user.getUserInput(" ");
          double b = Double.parseDouble(userInput2);
          String userInput3 = user.getUserInput(" ");
          double c = Double.parseDouble(userInput3);
     
     
          if(b < 0) {
              AlgebraHelper helper = new AlgebraHelper();
              helper.setVars(b,c);
              double addNum = helper.addThis();
              helper.setVars(addNum,c);
              double divNum = helper.divThis();
              System.out.println(divNum);
            } else if(b == 0) {
              AlgebraHelper helper = new AlgebraHelper();
              helper.setVars(a,c);
              double divNum = helper.divThis();
              System.out.println(divNum);
            } else {
              AlgebraHelper helper = new AlgebraHelper();
              helper.setVars(b,c);
              double subNum = helper.subThis();
              helper.setVars(subNum,c);
              double divNum = helper.divThis();
              System.out.println(divNum);
            }
     
     
        } catch(NumberFormatException e) {
          System.out.println("Your input was invalid.  Only numeric real entries are accepted.");
          System.out.println("Do not use operators.  Enter the a,b and c variables in ax+b=c");
          System.out.println("");
        } catch(NullPointerException e) {
          System.out.println("There must be an input!");
          System.out.println("");
        }
     
      }
    }
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  4. The Following User Says Thank You to JavaPF For This Useful Post:

    bgroenks96 (June 8th, 2011)

  5. #4
    Member
    Join Date
    Jun 2011
    Posts
    182
    My Mood
    Where
    Thanks
    15
    Thanked 8 Times in 8 Posts

    Default Re: Cannot Find Variable/Symbol Errors... why?

    JavaPF,

    Thank you! Now I know... if variables are inside the Try/Catch they are unreachable. I will make the change.

    I designed it that way simply because I know it's generally better to have try and catch only in the part of the program that needs it. However, if it causes variables to become unreachable, it's probably better not to do that!

    Thank for your help. I was pretty confused on this one.

  6. #5
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Cannot Find Variable/Symbol Errors... why?

    All of which was explained in my explanation in the post I linked to. But I guess it was too hard for you to follow that link and read what I said. No thanks for me and no more help for you!

  7. #6
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Cannot Find Variable/Symbol Errors... why?

    Quote Originally Posted by Junky View Post
    All of which was explained in my explanation in the post I linked to. But I guess it was too hard for you to follow that link and read what I said. No thanks for me and no more help for you!
    Indeed all of this was in the link provided. I just felt like expanding on your answer.
    Your contributions are appreshiated as always Junky!
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

Similar Threads

  1. cannot find symbol variable lines 24, 25, 44, 45, 63, 64? Help!
    By ironman_777 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 26th, 2010, 02:48 PM
  2. cannot find symbol in reference variable
    By NPotter86 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 1st, 2010, 08:21 PM
  3. Help with Cannot Find Symbol Variable errors
    By skboone in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 20th, 2010, 10:52 AM
  4. cannot find symbol variable radius?
    By noobish in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 4th, 2009, 10:29 AM
  5. Replies: 5
    Last Post: September 19th, 2009, 06:48 AM

Tags for this Thread