0

I tried compiling my personal contacts database program, but I have nothing but "cannot find symbol" errors Should I be instantiating an object more often than once? If not, what is wrong with this code?

I moved writetoFile and readFile initializations to outside try block, and declared them inside, and it helped! Now the only compiler error messages are the ones shown at the very bottom.


import java.util.Scanner;
import java.io.PrintWriter;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.lang.Double;
import java.util.Arrays;

public class Contacts   
{

 String name;
 String phone;
 String addy;
 String[] namesDB = new String[100];
 int index=0;


 public Contacts (String fullName, String phoneNo, String address)
 {
  name=fullName;
  phone=phoneNo;
  addy=address;
 }

 public String printName(String fullName)
 {
  for (index=0;index<100;index++)
  {
   if (fullName==namesDB[index])
    System.out.println(namesDB[index]);
    else
     index++;
  }
  return namesDB[index];
 }
 public String deleteName(String fullName)
 {
  for (index=0;index<100;index++)
  {
   if (fullName==namesDB[index])
    {
     namesDB[index]= "0";
     System.out.println("Record Deleted.");
    }
    else
        index++;
  }
  return namesDB[index];
 }
 static String stripQuo(String str)
  {
      if (str.startsWith("\""))
      {
          str = str.substring(1, str.length());
      }
      if (str.endsWith("\""))
      {
          str = str.substring(0, str.length() - 1);
      }
      return str;
  }

    public static void main(String[] args)
    {
        String EMPTY = "Empty";
        int index=0;
       String[] namesDB = new String[100];

         String fullName, phoneNo, address;

         for (int i=0; i<100; i++)
            {
                Contacts w = new Contacts("-1", "-1", "-1");
                w.namesDB[i]=EMPTY;
                i++;

            }
          PrintWriter writetoFile=null;
      BufferedReader readFile=null;

    //Creating Writable File, PCD=Personal Contacts Database
        try
        {
         writetoFile=new PrintWriter(new FileOutputStream("PCDFile.txt"));
         readFile = new BufferedReader(new FileReader("PCDFile.txt"));

        }
        catch(FileNotFoundException ex)
        {
        System.out.println("Error opening PCDFile.txt");
        System.exit(0);
        }

       catch(IOException gh)
       {
         System.out.println("Error reading from PCDFile.txt.");
            System.exit(0);
       }
                //ADDING
         if (args.length == 1) 
          {
           String option=args[0];
                if (option.equals("-a"))
                {
                 for (int i=0;i<100; i++)
                 {
                    if(namesDB[i]==EMPTY)
                    {

                     System.out.println("Full Name: ");
                     Scanner keyboard = new Scanner(System.in);
                     fullName=keyboard.nextLine();
                     System.out.println("Phone: ");
                     phoneNo=keyboard.nextLine();
                     System.out.println("Address: ");
                     address=keyboard.nextLine();
                     Contacts w = new Contacts(fullName, phoneNo, address);
                     namesDB[i]="fullName/n" + "phoneNo/n" + "address/n";

                     writetoFile.println(namesDB[i]);
                     System.out.println(namesDB[i]);

                }
                    else
                        index++;
                 }               
                }
                //SEARCHING
                else 
                {
                 option=stripQuo(option);
                 option=fullName;
                 String line= readFile.readLine();
                 for (index=0;index<100;index++)
                 {
                  if (line!=null)
                  {
                   if(line.equals(fullName))
                   {
                       Contacts w = new Contacts(fullName, phoneNo, address);
                        w.printName(fullName);
                    }
                  }
                  else
                    index++;
                  if (index==namesDB.length-1)
                  {
                   System.out.println("No record found for " + fullName + ".");
                  }
                 }
              }
        if (args.length == 2) 
          {
         option=args[0];
         fullName=args[1];
                if (option.equals("-s"))
                {
                 Contacts w = new Contacts(fullName, phoneNo, address);
                 fullName=w.stripQuo(fullName);

                 String line=readFile.readline();
                 for (index=0;index<100; index++)
                 {
                  if (line!=null)
                  {

                   if(line.equals(w.Contacts(fullName, phoneNo, address)))
                   {
                     w.printName(fullName);
                    }
                  }
                  else
                    index++;
                  if (index==namesDB.length-1) 
                  {
                   System.out.println("No record found for " + fullName + ".");
                  }
                 }
                }
                else if (option.equals("-d"))
                {
                 fullName=w.stripQuo(fullName);
                 String line=readFile.readline();
                 for (index=0;index<100; index++)
                 {
                  if (line!=null)
                  {
                   if(line.equals(w.Contacts(fullName, phoneNo, address)))
                   {
                    w.deleteName(fullName);
                    }
                  }
                  else if (index==namesDB.length-1)
                    System.out.println("No record found for " + fullName + ".");
                  else
                    index++;
}}}     


writetoFile.close();
readFile.close();
}}}

Compiler Messages:

/Contacts.java:170: cannot find symbol
symbol  : method readline()
location: class java.io.BufferedReader
                 String line=readFile.readline();
                                                     ^
/Contacts.java:176: cannot find symbol
symbol  : method Contacts(java.lang.String,java.lang.String,java.lang.String)
location: class Contacts
                   if(line.equals(w.Contacts(fullName, phoneNo, address)))
                                                   ^
/Contacts.java:191: cannot find symbol
symbol  : variable w
location: class Contacts
                 fullName=w.stripQuo(fullName);
                                          ^
/Contacts.java:192: cannot find symbol
symbol  : method readline()
location: class java.io.BufferedReader
                 String line=readFile.readline();
                                                     ^
/Contacts.java:197: cannot find symbol
symbol  : variable w
location: class Contacts
                   if(line.equals(w.Contacts(fullName, phoneNo, address)))
                                                  ^
/Contacts.java:199: cannot find symbol
symbol  : variable w
location: class Contacts
                    w.deleteName(fullName);
                                        ^
Michael Brewer-Davis
  • 14,018
  • 5
  • 37
  • 49
helpMe123
  • 1
  • 1
  • 1
  • 2

4 Answers4

2

Your writeToFile object only exists within the scope of the try statement in which it was created. Declare it outside of the try block, but initialize it inside.

The same goes for your readFile object. And the w object, only it's in an if block. You should have a look at variable scoping: Language/VariableScope.htm">http://www.java2s.com/Tutorial/Java/0020_Language/VariableScope.htm

ajwood
  • 18,227
  • 15
  • 61
  • 104
  • Thanks, I changed my writetoFile and readFile, and it helped. I'll try the w object – helpMe123 Nov 21 '10 at 00:51
  • I declare Contacts w=null outside of an if block, but initialize it inside and it doesn't work. Still getting cannot find symbol. Am I initializing and declaring correctly? – helpMe123 Nov 21 '10 at 01:04
2

A few style tips:

In general, avoid using if/else/loops without brackets, even if they only contain a single line of code. Especially you have if's with brackets and their corresponding else's without. This just makes it confusing for me to read.

A database (DB) is like a kind of data structure. Naming an array variable with DB on the end is like having a variable "Set namesArrayList". It just says the variable refers to something which it doesn't. An array is not a database. Now potentially you may want to change the data structure you're using so you may just want to call it, 'names' to indicate it's a collection of things without specifying data structure, etc.

Try not to write to much continuous unfactored code. Code that goes.

if (args.length == 1) {  
      .. some huge slab here 
} else if (args.length == 2) {
    .. another huge slab here
}

if hard to read/keep track of.

It's probably better to take those huge slabs of text and put them into functions

if (args.length == 1) {  
      whatWeDoFor1Arg(..params here..);
} else if (args.length == 2) {
      whatWeDoFor2Args(..params here..);
}

Or even:

if (args.length == 1) {  
      option=args[0];

      if (option .equals(...) ) {
            add(...) // or whatever
      } else if (option.equals(...) ) {
            search(..) // or whatever
      }


} else if (args.length == 2) {
     ... etc ...
}

This makes your code more readable. Also the function names act like automatic comments for your subsections (ie. what's in the functions). Also since you redo alot of the same code, you may be able to just do this in a function, then call the function multiple times (with different parameters), ie. reuse it.

Also, what are you actually trying to do with the Contacts class. Why not have a class to deal with individual contacts, eg: a Contact class, with say, name, address, phoneNum, fields. And a class to deal with a collection of contacts, eg: AddressBook class. Which contains a field which is something along the lines of: ArrayList<Contact> contacts , ie. an collection of contacts, and some methods to operate of this, eg: add, search, delete etc. You don't have to store a string representation of each object in your collection, you can store the actual objects themselves.

Doing this would make the code to run the command line very simple.

Now regarding the errors:

First error:

Contacts.java:170: cannot find symbol symbol : method readline() 
location: class java.io.BufferedReader String line=readFile.readline();

You've mistyped the name of the function. It should be readLine() with a capital L.

Second error:

^ /Contacts.java:176: cannot find symbol symbol : method  
                    Contacts(java.lang.String,java.lang.String,java.lang.String) 
location: class Contacts if(line.equals(w.Contacts(fullName, phoneNo, address))) 

What are you actually trying to do here. There's no Contacts method on your contacts class. Seems pretty self-explanatory. Going w.Contacts() where w is a Contacts object, tries to invoke a method called Contacts of the object (which obviously doesn't exist). Now if you define a toString() method for Contacts which returns a String of the same form as what's in your list of names (ie. namesDB). Then you can go: if ( line.equals(w.toString() ) ...

Third error:

^ /Contacts.java:191: cannot find symbol symbol : variable w 
location: class Contacts fullName=w.stripQuo(fullName);

You haven't actually declared a Contacts variable called 'w' either in this else statement or anywhere "above" this else statement. ie. you haven't declared a 'w' variable within the "scope" of this line.

So you could go: Contacts w = new Contacts(fullName, phoneNo, address); at the top of the else statement or in the if statement outside it.

BUT, this is dodgy because you're making a Contacts object with a fullName you've got from a commandline arg (this part is fine) and some uninitialised variables (address/phoneNo) decalred at the top of your main function!!!

Actually, since this is a static method, you should really be going: Contacts.stripQuo()

You should call static methods using the name of the class, not the name of an object of that class (even though that might work). (see above for how).

Fourth error:

same as first error

Fifth error:

Same thing as both the second error and the third error.

Sixth error:

Same as third error. Except you actually do need to call this on a object (ie. because it's not a static method)

user307666
  • 59
  • 3
1

Fixes


Well, first of all, Contacts w = new Contacts(fullName, phoneNo, address); must be a couple lines up, so it is outside the if braces, making it visible in the following else if statement.

Next, the occurences of .readline() should be .readLine() (capital "L").

Third, the part that says if (line.equals(w.Contacts(fullName, phoneNo, address))) cannot be done. You can not call the constructor of a class via the object. I think what you want is if (line.equals(new Contacts(fullName, phoneNo, address))), but this is still improper. You are trying to compare a String (line) object and a Contacts object. I don't know what you are trying to accomplish, but you should compare two Strings or two Contactss, here. Perhaps try line.equals(w.name) if that is what you are trying to do.

Fourth, whenever you get an error from readFile.readLine();, it is because of an uncaught IOException. I would fix this with a try-catch block, but a throws modifier at the top of the method will work, too.

Fifth, where you have String fullName, phoneNo, address;, you should have, instead, String fullName = "", phoneNo = "", address = "";. In this way, you will not rin into any compiler errors about not having initialized them.

Sixth, on one line in your printName, deleteName, and main method, you say fullName == namesDB[index], which will give you runtime errors, as you are comparing two non-primitive objects with ==. Replace this with fullName.equals(namesDB[index])

Also, you imported java.lang.Double. Why? First off, all java.lang files and packages are automatically imported. Second, you shouldn't use Doubles, but instead doubles for math. Also, you imported java.util.Arrays, but never use it.

My Solution


Below is my solution to your problems. Since I don't know what you are trying to accomplish, you may have to tweak it a bit.

import java.util.Scanner;
import java.io.PrintWriter;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Contacts
{
  String name;
  String phone;
  String addy;
  String[] namesDB = new String[100];
  int index = 0;

  public Contacts(String fullName, String phoneNo, String address)
  {
    name = fullName;
    phone = phoneNo;
    addy = address;
  }

  public String printName(String fullName)
  {
    for (index = 0; index < 100; index++)
    {
      if (fullName.equals(namesDB[index]))
      {
        System.out.println(namesDB[index]);
      }
      else
      {
        index++;
      }
    }
    return namesDB[index];
  }

  public String deleteName(String fullName)
  {
    for (index = 0; index < 100; index++)
    {
      if (fullName.equals(namesDB[index]))
      {
        namesDB[index] = "0";
        System.out.println("Record Deleted.");
      }
      else
      {
        index++;
      }
    }
    return namesDB[index];
  }

  static String stripQuo(String str)
  {
    if (str.startsWith("\""))
    {
      str = str.substring(1, str.length());
    }
    if (str.endsWith("\""))
    {
      str = str.substring(0, str.length() - 1);
    }
    return str;
  }

  public static void main(String[] args)
  {
    String EMPTY = "Empty";
    int index = 0;
    String[] namesDB = new String[100];

    String fullName = "", phoneNo = "", address = "";

    for (int i = 0; i < 100; i++)
    {
      Contacts w = new Contacts("-1", "-1", "-1");
      w.namesDB[i] = EMPTY;
      i++;

    }
    PrintWriter writetoFile = null;
    BufferedReader readFile = null;

    //Creating Writable File, PCD=Personal Contacts Database
    try
    {
      writetoFile = new PrintWriter(new FileOutputStream("PCDFile.txt"));
      readFile = new BufferedReader(new FileReader("PCDFile.txt"));

    }
    catch (FileNotFoundException ex)
    {
      System.out.println("Error opening PCDFile.txt");
      System.exit(0);
    }
    catch (IOException gh)
    {
      System.out.println("Error reading from PCDFile.txt.");
      System.exit(0);
    }
    //ADDING
    if (args.length == 1)
    {
      String option = args[0];
      if (option.equals("-a"))
      {
        for (int i = 0; i < 100; i++)
        {
          if (namesDB[i].equals(EMPTY))
          {

            System.out.println("Full Name: ");
            Scanner keyboard = new Scanner(System.in);
            fullName = keyboard.nextLine();
            System.out.println("Phone: ");
            phoneNo = keyboard.nextLine();
            System.out.println("Address: ");
            address = keyboard.nextLine();
            Contacts w = new Contacts(fullName, phoneNo, address);
            namesDB[i] = "fullName/n" + "phoneNo/n" + "address/n";

            writetoFile.println(namesDB[i]);
            System.out.println(namesDB[i]);

          }
          else
          {
            index++;
          }
        }
      }
      //SEARCHING
      else
      {
        option = stripQuo(option);
        option = fullName;
        String line = "";
        try
        {
          line = readFile.readLine();
        }
        catch (IOException ex)
        {
          System.err.println("Could not read line: " + ex);
        }
        for (index = 0; index < 100; index++)
        {
          if (line != null)
          {
            if (line.equals(fullName))
            {
              Contacts w = new Contacts(fullName, phoneNo, address);
              w.printName(fullName);
            }
          }
          else
          {
            index++;
          }
          if (index == namesDB.length - 1)
          {
            System.out.println("No record found for " + fullName + ".");
          }
        }
      }
      if (args.length == 2)
      {
        option = args[0];
        fullName = args[1];
        Contacts w = new Contacts(fullName, phoneNo, address);
        if (option.equals("-s"))
        {
          fullName = w.stripQuo(fullName);

          String line = "";
          try
          {
            line = readFile.readLine();
          }
          catch (IOException ex)
          {
            Logger.getLogger(Contacts.class.getName()).log(Level.SEVERE, null, ex);
          }
          for (index = 0; index < 100; index++)
          {
            if (line != null)
            {

              if (line.equals(w.name))
              {
                w.printName(fullName);
              }
            }
            else
            {
              index++;
            }
            if (index == namesDB.length - 1)
            {
              System.out.println("No record found for " + fullName + ".");
            }
          }
        }
        else if (option.equals("-d"))
        {
          fullName = w.stripQuo(fullName);
          String line = "";
          try
          {
            line = readFile.readLine();
          }
          catch (IOException ex)
          {
            Logger.getLogger(Contacts.class.getName()).log(Level.SEVERE, null, ex);
          }
          for (index = 0; index < 100; index++)
          {
            if (line != null)
            {
              if (line.equals(w.name))
              {
                w.deleteName(fullName);
              }
            }
            else if (index == namesDB.length - 1)
            {
              System.out.println("No record found for " + fullName + ".");
            }
            else
            {
              index++;
            }
          }
        }
      }


      writetoFile.close();
      try
      {
        readFile.close();
      }
      catch (IOException ex)
      {
        System.err.println("Could not close file: " + ex);
      }
    }
  }
}

A Question to You


As a note, most of this could have been avoided if you were using NetBeans. What IDE do you use?

Ky -
  • 30,724
  • 51
  • 192
  • 308
0

Straighten your scoping like user512652 said, and then compare Strings with .equals() instead of ==

Costis Aivalis
  • 13,680
  • 3
  • 46
  • 47