-2

For my program I am trying to have the loop run until the letter n is entered. But for some reason i keep receiving the error cannot find symbol in the condition for my loop. All help is greatly appreciated.

import java.io.*;
import java.util.*;
public class Prog213c
{
  public static void main(String[] args) {

        Scanner kbReader=new Scanner(System.in); 

        do{
            System.out.println( "Enter student number");
            int studentNumber = kbReader.nextInt();
            System.out.println(" Enter credits ");
            int credits = kbReader.nextInt();

            switch (credits) 
            {
            case 30: 
                System.out.println("Grade Level code = 2");
                break;
            case 29: 
                System.out.println("Grade Level code = 1");
                break;
            case 70:
                System.out.println("Grade Level code = 3");
                break;
            case 103:
                System.out.println("Grade Level code = 4");
                break;

            default: System.out.println("invalid number");
            }
            System.out.print("Do again(y/n)");
            String answer = kbReader.next();
        } while (answer == 'y'); // error received here
Juan Carlos Mendoza
  • 5,736
  • 7
  • 25
  • 50
lyah
  • 77
  • 6
  • 1
    The variable `answer` is declared inside the loop, meaning that it's not available in the condition (which is outside the loop). Move the declaration outside the loop. Probably this question is a duplicate. – davmac Nov 14 '17 at 17:23
  • Possible duplicate of [Scope of variable inside do-while loop](https://stackoverflow.com/questions/33324653/scope-of-variable-inside-do-while-loop) – Sneftel Nov 14 '17 at 17:24
  • Possible duplicate of [What does a "Cannot find symbol" compilation error mean?](https://stackoverflow.com/questions/25706216/what-does-a-cannot-find-symbol-compilation-error-mean) – davmac Nov 14 '17 at 17:25

4 Answers4

3

You have a few problems here:

String answer = kbReader.next();
} while (answer == 'y'); // error received here

Technically, answer is out of scope when you try to use - you can only use answer inside the loop itself. You should declare answer prior to starting your while loop so that it's in scope. Also, answer is a string and you're trying to "directly" compare it to a char.

This is also performing a case-sensitive comparison; while this isn't technically incorrect, it would be more user-friendly to accept accept either "Y" or "y".

Also, your switch statement won't work correctly. For example, case 30 will only be called if credits is exactly 30, which I assume isn't what you want.

You could do something like:

case 30:
case 31:
case 32: // ...

but that seems like a thoroughly painful way to do that. See also this question for more details.

This answer is particularly interesting and could be useful for your purposes. This is the code from that answer:

switch ((int) num/10) {
    case 1:
        System.out.println("10-19");
        break;
    case 2:
        System.out.println("20-29");
        break;
    case 3:
        System.out.println("30-39");
        break;
    case 4:
        System.out.println("40-49");
        break;
    default:
        break;
}

(Again, just to give credit where credit's due the above isn't my code, it was from the linked answer).

Also:

int studentNumber = kbReader.nextInt();

You never actually do anything with studentNumber, just prompt the user for it. Did you mean to do this?

  • well the switch statement is another problem in general. I was told to use a switch statement even though an if else statement is the only way it could work. In terms of my problem with the conditionals thank you for the answer. I have fixed the problem and my program will now compile – lyah Nov 14 '17 at 17:30
  • Glad it helped. Out of curiosity, do you know why the assignment required a `switch` statement? That seems like a very odd requirement to me. – EJoshuaS - Stand with Ukraine Nov 14 '17 at 17:33
  • Also yes I meant to do that. My assignment asks for the students to enter their student number. But the number isnt needed for the output at all – lyah Nov 14 '17 at 17:33
  • It was required because it was one of the requirements by my teacher and is the lesson we are on. Just by reading the assignment description however, a switch statement seems pretty impossible. – lyah Nov 14 '17 at 17:36
  • I added a few ideas about that. – EJoshuaS - Stand with Ukraine Nov 14 '17 at 17:46
2
  1. Single quotes are character literals in Java. So you can't compare a String with a char directly.
  2. Your answer variable has to be declared before the do-while loop.
  3. you have to use the equals method to compare strings. answer.equals("y")
Sneftel
  • 40,271
  • 12
  • 71
  • 104
Juan Carlos Mendoza
  • 5,736
  • 7
  • 25
  • 50
1

Problem 1: since answer is a string object, this is not working (answer == 'y');

you can do "y".equals(answer) is you get the nextLine from the scanner

or if you need to work with chars

char x = kbReader.next().charAt(0);
while (x == 'y');

Problem 2:

answer must be declared before the do-while loop...

your final code can look like

    Scanner kbReader = new Scanner(System.in);
    char answer = 'y';
    do {
        System.out.println("Enter student number");
        int studentNumber = kbReader.nextInt();
        System.out.println(" Enter credits ");
        int credits = kbReader.nextInt();

        switch (credits) {
        case 30:
            System.out.println("Grade Level code = 2");
            break;
        case 29:
            System.out.println("Grade Level code = 1");
            break;
        case 70:
            System.out.println("Grade Level code = 3");
            break;
        case 103:
            System.out.println("Grade Level code = 4");
            break;

        default:
            System.out.println("invalid number");
        }
        System.out.print("Do again(y/n)");
        answer = kbReader.next().charAt(0);
    } while (answer == 'y');  
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
0

Change your while condition to:

while ("y".equals(kbReader.next()));
Sunil Manheri
  • 2,313
  • 2
  • 18
  • 19