javacompiler-errors

an issue concerning program compiling


the bellow code is a part of my program that is suppose to give the user the option to continue by pressing p and exiting by pressing e. anyways I keep getting this single error and I don't know what to do to fix it here is the error (I tried moving the line arround but it had no effect and if I remove it the program compiles but then it goes to an endless loop) :

C:\Users\Asus\Desktop\javaproject\products.java:112: error: cannot find symbol
           user_selection  =  keyboard.nextLine() ;
                              ^
  symbol:   variable keyboard
  location: class productsDataFinder
1 error

Process completed.

and here is the code:

String  user_selection  =  "????"  ;

      System.out.print("\n This program prints inventory. Please, select from"
                    +  "\n the following menu by typing in a letter. ") ;

      while ( user_selection.charAt( 0 )  !=  'e' )
      {
         System.out.print("\n\n   p   Print inventory."
                         +  "\n   e   Exit the program.\n\n   " ) ;

        user_selection  =  keyboard.nextLine() ;   //error concerning this line

     if ( user_selection.charAt( 0 )  ==  'p' )
     { 

       System.out.print("\n Please insert your serial number:  ");              

        Scanner  keyboard  =  new Scanner( System.in ) ;
        int  given_id  =  keyboard.nextInt() ;
        int  products_index  =  0 ;

            boolean table_search_ready  =  false ;

         while ( table_search_ready  ==  false )
         {

         if ( products_index >= products_table.length )
         {

            table_search_ready  =  true ;

                      System.out.print( "\n    Sorry, no such product id "
                           +  given_id  + ".\n" ) ;
         }

         else if ( products_table[ products_index ].get_id()  ==  given_id )
         {

            products_table[ products_index ].print_products_data() ;

            table_search_ready  =  true ;
         }



         else
         {
            products_index  ++  ;
          }

Solution

  • The variable keyboard hasn't been defined.

    That is to say, you are using it...

    user_selection  =  keyboard.nextLine() ;
    

    before it is defined

    Scanner  keyboard  =  new Scanner( System.in ) ;