Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-03-2010, 08:30 PM   PM User | #1
DoryFish
New to the CF scene

 
Join Date: Jul 2010
Location: California
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
DoryFish is an unknown quantity at this point
"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..
DoryFish is offline   Reply With Quote
Old 08-03-2010, 08:49 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,653
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php

Last edited by Fou-Lu; 08-03-2010 at 08:52 PM..
Fou-Lu is offline   Reply With Quote
Old 08-03-2010, 10:36 PM   PM User | #3
DoryFish
New to the CF scene

 
Join Date: Jul 2010
Location: California
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
DoryFish is an unknown quantity at this point
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?
DoryFish is offline   Reply With Quote
Old 08-03-2010, 11:02 PM   PM User | #4
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,653
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Old 08-04-2010, 12:17 AM   PM User | #5
DoryFish
New to the CF scene

 
Join Date: Jul 2010
Location: California
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
DoryFish is an unknown quantity at this point
thank you!! the do-while loop was perfect! it's working great now.
DoryFish is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:48 AM.


Advertisement
Log in to turn off these ads.