Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Jul 2010
    Location
    California
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    "cannot find symbol" error in Java

    I know this is probably an easy question. I'm writing a program that solves a system of three equations, using Cramer's Rule. This is not an assignment for a class, I'm just doing it for fun.
    I wrote it initially on my TI-83+ graphing calculator, and I am now adapting it and writing it in Java. I think this should be right, but when I compile it I am getting 72 errors (!) that all say "cannot find symbol" - one for each time I'm using one of my variables in the formula that actually solves the system. (starting with the "double M = ").
    When I used the same variables earlier in the program (when it displays the equations so you can check if they were inputted correctly), it is fine. But in the formula, it isn't.
    Is it doing this because I declared the variables inside a "while" loop and am trying to use them outside of it?
    How can I fix this?

    Code:
    import java.util.Scanner;
    
    public class SolveASystemOf3 {
      public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        boolean inputIsCorrect = false;
    	while (inputIsCorrect == false) {
       	  System.out.println("Find X, Y, and Z. \nInput the first equation. (Ax + By + Cz = D)");
    	  System.out.print("A = ");
    	  double A = input.nextDouble();
    	  System.out.print("B = ");
    	  double B = input.nextDouble();
    	  System.out.print("C = ");
    	  double C = input.nextDouble();
    	  System.out.print("D = ");
    	  double D = input.nextDouble();
    	  System.out.println("     ");
    	  
    	  System.out.println("Input the second equation. (Ex + Fy + Gz = H)");
    	  System.out.print("E = ");
    	  double E = input.nextDouble();
    	  System.out.print("F = ");
    	  double F = input.nextDouble();
    	  System.out.print("G = ");
    	  double G = input.nextDouble();
    	  System.out.print("H = ");
    	  double H = input.nextDouble();
    	  System.out.println("     ");
    	  
    	  System.out.println("Input the third equation. (Ix + Jy + Kz = L)");
    	  System.out.print("I = ");
    	  double I = input.nextDouble();
    	  System.out.print("J = ");
    	  double J = input.nextDouble();
    	  System.out.print("K = ");
    	  double K = input.nextDouble();
    	  System.out.print("L = ");
    	  double L = input.nextDouble();
    	  System.out.println("     ");
    	  
    	  System.out.println("Check if the input is correct.");
              System.out.println(A + "x + " + B + "y + " + C + "z = " + D);
    	  System.out.println(E + "x + " + F + "y + " + G + "z = " + H);
    	  System.out.println(I + "x + " + J + "y + " + K + "z = " + L);
    	  
    
    	  boolean goodResponse = false;
    	  while (goodResponse == false) {
    	  	  System.out.println("Is this correct? Enter 0 for no and 1 for yes.");
    	      int inputAnswer = input.nextInt();
    	        if (inputAnswer == 0) {
    	          System.out.println("Re-enter your equations.");
    		      goodResponse = true;
    	        }
    	        else if (inputAnswer == 1) {
    	          inputIsCorrect = true;
    		      goodResponse = true;
    	  	      System.out.println("     ");
    		    }
    	        else {
    	        System.out.println("Invalid response.");
    	        }
    	    }
    	}
    
    //here is where it starts giving me trouble
        double M = ((A * F * K) + (B * G * I) + (C * E * J) - (I * F * C) - (J * G * A) - (K * E * B));
        if (M == 0) {
          System.out.println("There is no single solution for this system. \nIt may have no solutions or it may have infinite solutions.");	
    	}
    	else {
      	  double X = (((D * F * K) + (B * G * L) + (C * H * J) - (L * F * C) - (J * G * D) - (K * H * B)) / M);
    	  double Y = (((A * H * K) + (D * G * I) + (C * E * L) - (I * H * C) - (L * G * A) - (K * E * D)) / M);
          double Z = (((A * F * L) + (B * H * I) + (D * E * J) - (I * F * D) - (J * H * A) - (L * E * B)) / M);
    	  System.out.println("X = " + X);
    	  System.out.println("Y = " + Y);
    	  System.out.println("Z = " + Z);
    	}
      }  
    }
    Last edited by Fou-Lu; 08-03-2010 at 08:44 PM.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,978
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    The problem is the scope. Anytime you declare a variable within a loop it takes the scope of that loop. When the loop completes, the variable is gone.
    Declare and instantiate all of your variables at the top of your main method to prevent this from happening.

    Edit:
    Also, because of the large number of like variables, look at using an array to deal with this instead of manually providing all of the declarations.
    Last edited by Fou-Lu; 08-03-2010 at 08:52 PM.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #3
    New to the CF scene
    Join Date
    Jul 2010
    Location
    California
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thank you, I wasn't remembering whether the loop would limit the scope of my variables.

    So, I changed the program so that if the user inputs that the equations are not correct, the program exits.

    I would still like it to have some way that it automatically sends you back to inputting the variables if you did it wrong the first time. How can I do this without using the loop?

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,978
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    You can't, the code still processes from the first step to the last, and I wouldn't recommend spawning a new process just for that. The loop is a much easier solution.
    You can wrap it in a do/while to achieve this.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #5
    New to the CF scene
    Join Date
    Jul 2010
    Location
    California
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thank you!! the do-while loop was perfect! it's working great now.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •