0

I tried to place the 2nd if statement inside the loop statement and in the first if statement, and it keeps on saying "exists" cannot be a resolved to a variable. :(

import java.util.*;

public class MT_Assign2_Phonebook_LinkedList {

    public static void main(String[] args) {
        
        LinkedList <String> AddEntry_list = new LinkedList <String> (); //FirstName & Last Name
        LinkedList <String> Number_list = new LinkedList <String> (); //Telephone Number
        
        String firstname, lastname;
        String phonenum;
        
        Scanner input = new Scanner (System.in);
        
        System.out.println ("MAIN MENU");
        System.out.println ("1 = ADD Phonebook Entry");
        System.out.println ("2  = DELETE Phonebook Entry");
        System.out.println ("3 = VIEW All Entries ");
        System.out.println ("4 = SEARCH Entries ");
        System.out.println ("5 = Quit ");
        
        //ENTER USER'S CHOICE
        System.out.print ("\nPlease Select your choice: ");
            int choice = input.nextInt();
                
            if (choice == 1) {
                    System.out.println ();
                    System.out.println ("You chose to ADD PHONEBOOK ENTRY");
                    
                    //.get (record the input data ni user) with VALIDATION
                        for (int database_pb = 0; database_pb <AddEntry_list.size (); database_pb++) {
                            
                            if (AddEntry_list.get(database_pb).contains(firstname) && AddEntry_list.get(database_pb).contains(lastname))
                            {
                                System.out.println ("\n  ERROR OCCURED: PHONEBOOK ENTRY ALREADY EXIST!\n  END OF PROGRAM");
                                System.exit(0);
                                
                                boolean exists = true; //validate
                                
                            
                            }//END IF
                            
                            if (exists == false)    {
                                
                                System.out.print ("First Name: ");
                                System.out.print ("Last Name: ");
                                System.out.print ("Number (+63): ");
                                AddEntry_list.add (firstname + " " + lastname + " " + phonenum + " ");
                                Number_list.add (phonenum + " " + firstname + " " + lastname + " " );
                                    
                                System.out.println ("Successfully Added Entry! ");
                                
                            }  boolean exists =  false;
                        }//END OF FOR LOOP
                            
            }//CHOICE 1
            
            
            
                else 
                    System.out.println ("Invalid Input!");
        

    } //public static

}//public class end
Petr Janeček
  • 37,768
  • 12
  • 121
  • 145

1 Answers1

1

Scoping rules are the explanation.

Any given variable is declared someplace. Find the 'related' batch of braces ({ and }). You've decreed that this variable exists in that scope. And nowhere else.

The 'related' batch is always the nearest encompassing ones, except for parameters, where it's the method definition itself. Simple.

So, you have this block:

{
  System.out.println ("\n  ERROR OCCURED: PHONEBOOK ENTRY ALREADY EXIST!\n  END OF PROGRAM");
  System.exit(0);
  boolean exists = true; //validate
}                        

That's where your exists variable exists (heh), and nowhere else.

NB: That code block is nuts. You exit the program with an error message, and then say: 'exists = true' ??? that makes no sense.

rzwitserloot
  • 85,357
  • 5
  • 51
  • 72
  • I already removed the System.exit(0); and its still "exists" cannot be a resolved to a variable. – elsker deg Dec 06 '20 at 03:05
  • @elskerdeg This answer explains exactly why that is, and then as a sidenote, mentioned that in addition to the problem, your code is bizarre. You seem to have ignored the issue and focused on the sidenote. Please review the full answer. – rzwitserloot Dec 06 '20 at 19:39