Results 1 to 6 of 6
  1. #1
    Join Date
    Oct 2009
    Posts
    3
    Rep Power
    0

    Question Error "can not find symbol variable"

    Hello, i'm new here and I've only been programming for 6 weeks now so I think I've might have gotten over my head here. I keep getting the "can't find symbol variable error" and I've been trying to fix it for quite a while now. I'm trying to write a program that will calculate my GPA. I'm using JCreator btw.

    Here's the coding:

    Java Code:
    import java.util.Scanner;
    
    public class GpaChris
    {
    
    	public static void main (String[] args) 
    	{
    		Scanner scan = new Scanner(System.in);
    		
    		double count1 = 0;
    		double input2 = 0;
    		int count2 = 0;
    		
    		System.out.println("Is this an AP class? Yes or No ");
    		String type1 = scan.next();
    		
    		while(count2 < 75){
    			
    		count2++;
    	
    		if (type1 == "No"){
    		System.out.println("Please input your semester grade then press enter. Input 1234 to execute final calcalutions");
    		double input1 = scan.nextDouble();
    		if ((input1 <= 100) && (input1 >= 90))
    			input1 = 4;
    		else{
    		if ((input1 <= 89) && (input1 >= 80))
    			input1 = 3;
    		else{
    		if ((input1 <= 79) && (input1 >= 75))
    			input1 = 2;
    		else{
    		if ((input1 <=74) && (input1 >= 70))
    			input1 = 1;
    		else{
    		if (input1 < 70)
    			input1 = 0;
    		}}}}}	
    			
    		else {
    		System.out.println("Please input your semester grade then press enter. Input 1234 to execute final calcalutions");
    		double input1 = scan.nextDouble();
    		if ((input1 <= 100) && (input1 >= 90))
    			input1 = 5;
    		else{
    		if ((input1 <= 89) && (input1 >= 80))
    			input1 = 4;
    		else{
    		if ((input1 <= 79) && (input1 >= 75))
    			input1 = 3;
    		else{
    		if ((input1 <=74) && (input1 >= 70))
    			input1 = 2;
    		else{
    		if (input1 < 70)
    			input1 = 0;
    		
    		}}}}}		
    		if (input1 != 1234)
    		input2 += input1;
    		else
    		System.out.println(input2/count2);
    	
    		}	
     }
    }
    ==============================

    I'm having problems with Lines 59 and 60


    if (input1 != 1234)
    input2 += input1;
    Last edited by Fubarable; 10-04-2009 at 08:42 PM. Reason: Code tags added to improve readability

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I added code tags to your post above to help make your posted code more readable. One of your problems is that input1 and input2 are not visible in the lines that are having problems as they are "out of scope". This is because these variables are only visible from within the region of code where they were declared.
    You declare input1 here:
    Java Code:
          if (type1 == "No") {
            System.out.println("Please input your semester grade then press enter. Input 1234 to execute final calcalutions");
            double input1 = scan.nextDouble();
    and so it will only be visible in the region bounded by the { in the if statement above and its matching closing curly brace, }.

    Other problems:

    1) Sorry to be blunt, but your indentation is atrocious, and this is preventing you from seeing the scope of your variables properly.
    2) You shouldn't use == to compare Strings. Use the equals or equalsIgnoreCase methods instead.
    3) This:
    Java Code:
    }}}}}
    Should never be done.
    4) Your nested if/elses may be simplified with a switch/case statement.

  3. #3
    Join Date
    Oct 2009
    Posts
    3
    Rep Power
    0

    Default

    Sorry I didn't know about the code tags. I'll do that from now on.

    Sorry about the indention as well, I haven't written anything else this long yet and I'm not sure how to properly indent it, that and when I posted this it got rid of what indention I did have.

    I was doing "}}}}}" to make it easier to count to make sure I had the right number. What way should I do it?

    And what is the "equals or equalsIgnoreCase method" and the "Switch/case statement"?

    Also thank you for explaining to me the out of scape of thing, I understand what was wrong (and problems I've had before) a lot better now!

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Here is one way to indent, plus an example of using equalsIgnoreCase:
    Java Code:
          if (type1.equalsIgnoreCase("No")) {
            System.out.println("Please input your semester grade then press enter. Input 1234 to execute final calcalutions");
            double input1 = scan.nextDouble();
            if ((input1 <= 100) && (input1 >= 90))
              input1 = 4;
            else {
              if ((input1 <= 89) && (input1 >= 80))
                input1 = 3;
              else {
                if ((input1 <= 79) && (input1 >= 75))
                  input1 = 2;
                else {
                  if ((input1 <= 74) && (input1 >= 70))
                    input1 = 1;
                  else {
                    if (input1 < 70)
                      input1 = 0;
                  }
                }
              }
            }
          }
    
          else {
            //....

    For info on the Switch statement, please look here: Sun Switch Tutorial

  5. #5
    Join Date
    Oct 2009
    Posts
    3
    Rep Power
    0

    Default

    I really appreciate the help, thank you!

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. cannot find symbol for "list.addFirst"
    By alexbryan_08 in forum New To Java
    Replies: 10
    Last Post: 08-26-2009, 08:55 AM
  2. Replies: 1
    Last Post: 06-30-2009, 02:32 PM
  3. Reading strings with like "degree symbol" - HOW?
    By RR_QQ in forum New To Java
    Replies: 13
    Last Post: 02-18-2009, 02:16 AM
  4. Replies: 1
    Last Post: 10-20-2008, 07:35 AM
  5. "Cannont find symbol Constructor" error
    By Welsh in forum New To Java
    Replies: 7
    Last Post: 01-25-2008, 12:12 AM

Posting Permissions

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