Results 1 to 3 of 3
  1. #1
    helpme123 is offline Member
    Join Date
    Nov 2010
    Posts
    1
    Rep Power
    0

    Default Urgent: Cannot Find Symbol!

    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?

    Please reply asap!

    -----------

    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;

    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++;

    }

    //Creating Writable File, PCD=Personal Contacts Database
    try
    {
    PrintWriter writetoFile=new PrintWriter(new FileOutputStream("PCDFile.txt")
    );
    BufferedReader readFile=null;
    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:128: cannot find symbol
    symbol : variable writetoFile
    location: class Contacts
    writetoFile.println(namesDB[i]);
    ^
    /Contacts.java:141: cannot find symbol
    symbol : variable readFile
    location: class Contacts
    String line= readFile.readLine();
    ^
    /Contacts.java:175: cannot find symbol
    symbol : method Contacts(java.lang.String,java.lang.String,java.la ng.String)
    location: class Contacts
    if(line.equals(w.Contacts(fullName, phoneNo, address)))
    ^
    and etc..

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Hello and welcome to the forum

    Amongst other issues you have scope problems in your code. You must remember that variables declared inside of a block such as a try/catch block, are visible only inside that block. If you need to use the variable outside of the block, then it needs to be declared outside of the block. For e.g.,

    Rather than
    Java Code:
          try {
             // writetoFile is only visible inside of this block of code
             PrintWriter writetoFile = new PrintWriter(new FileOutputStream("PCDFile.txt"));

    Instead do:
    Java Code:
          PrintWriter writetoFile = null; declared outside of block and visible outside of block
          try {
             //PrintWriter writetoFile = new PrintWriter(new FileOutputStream("PCDFile.txt"));
             writetoFile = new PrintWriter(new FileOutputStream("PCDFile.txt"));

    A suggestion: Rather than type in a whole bunch of code and then finding a mess of compiler errors, you're much better off compiling early and frequently, and not adding any new code til old compiler errors are all corrected.

    Another suggestion: and while I know you're in a hurry, you're better off keeping your urgency to yourself as it can irritate some of the volunteers here as no one likes to feel pressured. So stating urgency can have the opposite effect intended.

    Much luck with your project.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    A couple of things: could you please use the code tags. Basically you put [CODE] at the start of your code and [/CODE] at the end. That way the code is readable to others. You can edit your post and do this now, although you may have to check that the indentation is correct.

    Secondly, what the "Urgent" "asap!" business all about? Be aware that all any one here has to go on are the words you post and, all too often, our words come across with meanings we possibly never intended. Many here will read such expressions, think "you're not the boss of me!", and move on without offering the help you want.

    ----------------------------

    Java Code:
    Contacts.java:128: cannot find symbol
    symbol : variable writetoFile
    location: class Contacts
    writetoFile.println(namesDB[i]);

    "cannot find symbol" is the compiler's way of saying you have used a variable that hasn't - as far as it's concerned - been defined. Common reasons include typos, trying to call methods with the wrong argument types, and problems with scope. This one is scope. (probably - you don't actually say which line 128 is)

    Java Code:
    try {
        PrintWriter writetoFile = new PrintWriter(new FileOutputStream("PCDFile.txt"));
        BufferedReader readFile = null;
        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]); /* <-- line 128  */

    In Java a variable gets declared (you say what type it is) and is said to be in scope up to the matching } of the block where it was declared. The variable can only be used when it is in scope.

    Look at

    Java Code:
    PrintWriter writetoFile = new PrintWriter(new FileOutputStream("PCDFile.txt"));

    This declares writetoFile to be of type PrintWriter. It does so at the start of the try block in main(). The scope of this variable will be until the end of that try block - ie just several lines later. Beyond that point you can't use writetoFile and, if you try, you will get a "cannot find symbol" message.

    Try declaring the variable in a larger (enclosing) scope. For example outside of and before the try block.

    ----------------------------

    Avoid situations where you have lots of compiler messages. You can do this by compiling often - every statement or block of code if need be - and responding to the compiler messages one at a time as they occur.

    If you can't understand a message post it here - but remember to indicate whch line of your code it is referring to.

    And remember, again, that we can't do a blind thing about the urgency with which this problem imposes itself on you. And we won't be responding as-soon-as-possible: we'll respond if and when we feel like it.

Similar Threads

  1. Cannot find symbol
    By Johanis in forum New To Java
    Replies: 18
    Last Post: 11-09-2010, 08:34 PM
  2. Still cannot find symbol!
    By Johanis in forum New To Java
    Replies: 1
    Last Post: 11-04-2010, 04:32 PM
  3. Can not find symbol ???
    By AliceNewbie in forum New To Java
    Replies: 1
    Last Post: 02-17-2010, 01:44 AM
  4. Replies: 9
    Last Post: 10-18-2008, 08:26 PM
  5. Replies: 3
    Last Post: 05-01-2008, 08:30 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •