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);
^