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

Thread: do while problem / symbol not recognized

  1. #1
    Junior Member
    Join Date
    Mar 2014
    Posts
    7
    My Mood
    Busy
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default do while problem / symbol not recognized

    Hi

    I'm a newbie to programming and I'm encountering issues with the following program.
    The compiler tells me that the variable after the while is not recognized.

    What I especially don't understand is that the variable is defined and accepted in the character input and the swicth but later on is not recognized. I'm sure of this because I wrote the entire program, and then compiled it afterwards. I was therefore able to see the compiler advancing as the code was corrected

    The error message is "cannot find symbol - variable choix"

    Sorry for the french, I'm in a french programming class


        do {
            System.out.println ("1); Acheter Patons ");
            System.out.println (" 2); Fabriquer baguettes ");
            System.out.println (" 3); Vendre baguette ");
            System.out.println (" q ou Q); Quitter ");
            char choix = Clavier.lireChar();
     
            switch (choix) {
                //demande le nombre de patons a achete
                case 1: 
                    System.out.println (" Nombre de patons :");
                    nombreDePatons = Clavier.lireDouble();
                    while (nombreDePatons <= 0 ){
                        System.out.println (" Erreur : choix non valide! ");  
                    }
                    patons = patons + nombreDePatons;
                    accumulateurPatons = accumulateurPatons + nombreDePatons;
                    break;
                    // demande le nombre de baguette  produire
                case 2:
                    System.out.println (" Nombre de baguette : ");
                    nombreDeBaguettes = Clavier.lireDouble();
                    while (nombreDeBaguettes <= 0 ){
                        System.out.println (" Erreur : choix non valide! ");  
                    }
                    if (nombreDeBaguettes > nombreDePatons) {
                        System.out.println ("Pas de patons, pas de baguette! ");
                    } else if (nombreDeBaguettes <= nombreDePatons) {
                        baguette = baguette + nombreDeBaguettes;
                        patons = patons - nombreDeBaguettes;
                    }
                    break; 
                // demande le nombre de baguette  vendre
                case 3:
                    System.out.println (" Nombre de baguette : ");
                    baguetteAVendre = Clavier.lireDouble ();
                    while (baguetteAVendre <= 0 ){
                        System.out.println (" Erreur : choix non valide! ");  
                    }
                    if (baguetteAVendre > nombreDeBaguettes) { 
                        System.out.print ("Pas de baguette, pas de ventes! ");
                    } else if (baguetteAVendre <= nombreDeBaguettes) {           
                    baguette = baguette - baguetteAVendre;
                        accumulateurBaguette = accumulateurBaguette + baguetteAVendre;
                    }     
                default:
                    System.out.println (" Erreur : choix non valide! ");  
            }
        } while (choix != 'q' && choix != 'Q') ;

    Thanks in advance

    Christian
    Last edited by Christian_Doucet; March 27th, 2014 at 06:47 AM.


  2. #2
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    284
    My Mood
    Amused
    Thanks
    6
    Thanked 63 Times in 61 Posts

    Default Re: do while problem / symbol not recognized

    Hi! Welcome to the forum! Please read Announcements - What's Wrong With My Code?

    The error message is "cannot find symbol - variable choix"
    it is because you do the declaration of variable inside your do loop statement, and that is the reason why it is not recognized.

    those variables that declares inside a block statement are block variables and is not recognizable outside the block.
    for example:
    for(int i = 0; i < 2; i++) {
        int b = i; // declaration of b
        // and this variable b is recognizable only inside this loop
    }
    System.out.println(b); // will cause a compilation error because b cannot be found
    the above code will make a compilation error because the print statement is trying to access a variable that is declared inside a block statement, and because it is declared inside the block statement, it is only recognized inside the block statement.

    in your case, you declared your variable inside a block statement (a do while loop), therefore, when you try to access/read it outside that block (in while statement) it causes a compilation error symbol not found.
    and if you ask why since that while statement is incorporate with do while loops so it should be recognized.
    the answer is, look at the brackets in do statements, the variable is accessible only within that brackets, while statement of do while loop lies outside that brackets
    Last edited by dicdic; March 26th, 2014 at 09:54 PM.

  3. The Following User Says Thank You to dicdic For This Useful Post:

    Christian_Doucet (March 27th, 2014)

  4. #3
    Junior Member
    Join Date
    Mar 2014
    Posts
    7
    My Mood
    Busy
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: do while problem / symbol not recognized

    Thanks for the help!

Similar Threads

  1. problem finding symbol
    By ghostheadx in forum What's Wrong With My Code?
    Replies: 4
    Last Post: September 20th, 2013, 11:42 AM
  2. Replies: 1
    Last Post: June 23rd, 2013, 02:37 AM
  3. [SOLVED] Javac Not recognized?
    By Future_Programmer in forum Java Theory & Questions
    Replies: 4
    Last Post: June 12th, 2013, 06:04 AM
  4. Problem: cannot find symbol
    By ganti91 in forum What's Wrong With My Code?
    Replies: 20
    Last Post: May 28th, 2012, 11:13 AM
  5. IP Can't Recognized
    By Voipswitch1 in forum Java Networking
    Replies: 1
    Last Post: February 8th, 2012, 05:03 PM