-3

This is really odd, I wrote a class for this program and was about to test how it reads data in from the file but I'm getting a "Cannot find symbol" error that refers to the "new" in the first scanner declared. The same error for the "=" in the second Scanner variable, and a bunch of cannot find symbols for all the "Candidate_Info[i]" objects later on. I dunno where my error is. I'm using notepad++ by the way, compiling and running it using notepad++ too.

import java.util.Scanner; //I'm only gonna need scanner for this project I think
import java.io.*;

public class HuntowskiSamuel //This is what the file name should be as well as the class name
{
    public static void main (String [] args) throws IOException
    {
    File CFile = new File("cipcs115.txt"); //This will be the file and scanner variable used to pull the data for the candidates
    Scanner scan = new Scanner(Cfile);

    File CfileReadIn = new File("cipcs115.txt"); //While this file and scanner will be used to pull the number of candidates from the same file...hopefully
    Scanner scanReadIn = new Scanner(CFileReadIn);

    String StateName = "No name yet"; //This is where the state value will be held, that controls the input of the file
    int NumberOfCandidates = 0; // This will pull the number of candidates for the array size
    String garbage = "Empty"; //This is where the ReadIn scanner can dump excess stuff

    StateName = scanReadIn.next(); //The prime read for the while loop

    int NumberOfLettersEntered [] = new int [8]; //Since we only have the letters L, C, V, S, D, P, Q, and X (others/errors) that were entered, IN THAT ORDER. Its not graceful but it works

    while(StateName != "END_OF_FILE") //While we haven't reached the end of the file
    {
        for(int i = scanReadIn.nextInt(); i > 0; i--) //Read in the number of candidates, then run the loop that number of times
        {
            NumberOfCandidates++; //Every time this loop runs, it means there is one more candidate for the total amount
            garbage = scanReadIn.nextLine(); //This will take all the important info and dump it, seeing as we only need the number of candidates and the state name
        }
        StateName = scanReadIn.next(); //Pull the next state name
    }

    Candidate_Info Candidates [] = new Candidate_Info [NumberOfCandidates]; //This creates an array of the exact size of the number of candidates in the file

    for(int i = 0; i < NumberOfCandidates; i++) //Running the constructor for each and every candidate created
    {
        Candidate_Info [i] = Candidate_Info();
    }

    StateName = scan.next(); //Prime read for the data taking loop

    while(StateName != "END_OF_FILE") //The same as the other loop, only using the real file and scanner variables
    {
        int CandidateNumber = 0; //This will keep the array number straight from 0 to however many candidates - 1

        for(int i = 0; i < scan.nextInt(); i++) //This will loop for each of the candidates in ONE STATE, it pulls the number of candidates as an int
        {
            Candidate_Info[CandidateNumber].setState(StateName);
            Candidate_Info[CandidateNumber].setName(scan.next());
            Candidate_Info[CandidateNumber].setOffice(scan.next());
            Candidate_Info[CandidateNumber].setParty(scan.next().charAt(0)); //This might not work because it is just a single character versus the string that it would be passed
            Candidate_Info[CandidateNumber].setVotes(scan.nextInt());
            Candidate_Info[CandidateNumber].setSpent(scan.nextDouble());
            Candidate_Info[CandidateNumber].setMotto(scan.nextLine());
            CandidateNumber++;
        }
        StateName = scan.next();
    }
}
}

And here's the code for the Class I wrote.

//Samuel James Huntowski
// started: 11-18-2014
// last modified: 11-18-2014

public class Candidate_Info
{
private String State; //All the variables that were given to me in the specification
private String Name_of_Candidate; 
private String Election_Office;
private char Party;
private int Number_of_Votes;
private double Dollars_Spent;
private String Motto;

private final double DOLLARS_SPENT_MIN = 0.0; //Mutator method for Dollars_Spent must check to see if greater then this value

private final int NUMBER_OF_ATTRIBUTES = 7; //for use in the equals method

public Candidate_Info()
{
    State = "No state assigned"; //Giving empty values to all of the variables
    Name_of_Candidate = "No name yet"; 
    Election_Office = "No office assigned";
    Party = 'X';
    Number_of_Votes = 0;
    Dollars_Spent = 0.0;
    Motto = "No motto yet";
}

//These are all of the Accessor Methods 

public String getState()
{
    return State;
}

public String getName()
{
    return Name_of_Candidate;
}

public String getOffice()
{
    return Election_Office;
}

public char getParty()
{
    return Party;
}

public int getVotes()
{
    return Number_of_Votes;
}

public double getSpent()
{
    return Dollars_Spent;
}

public String getMotto()
{
    return Motto;
}

//Mutator methods will go here

public void setState(String newState)
{
    State = newState;
    System.out.println("The candidate's state is now set to " + newState + ".");
}

public void setName(String newName)
{
    Name_of_Candidate = newName;
    System.out.println("The candidate's name is now set to " + newName + ".");
}

public void setOffice(String newOffice)
{
    Election_Office = newOffice;
    System.out.println("The candidate's office is now set to " + newOffice + ".");
}

public void setParty(char newParty)
{
    if(!((newParty == 'd') || (newParty == 'r') || (newParty == 'i') || (newParty == 'o'))) //If the value of newParty DOES NOT EQUAL 'o', 'd', 'r', or 'i' then do the next set of code
    {
        System.out.println("Invalid party input. Candidate's party remains unchanged. Please try again.");
    }
    else
    {
        Party = newParty;
        System.out.println("The candidate's party is now set to " + newParty + ".");
    }
}

public void setVotes(int newNumberOfVotes)
{
    Number_of_Votes = newNumberOfVotes;
    System.out.println("The candidate's number of votes is now set to " + newNumberOfVotes + ".");
}

public void setSpent(double newDollarsSpent)
{
    if(newDollarsSpent < DOLLARS_SPENT_MIN) //If the amount of money spent is less then zero (Which just wouldn't make sense, so that's why I set the variable to zero)
    {
        System.out.println("New amount of dollars spent is invalid. Candidate's dollars spent remains unchanged. Please try again.");
    }
    else
    {
        Dollars_Spent = newDollarsSpent;
        System.out.println("The candidate's dollars spent is now set to " + newDollarsSpent + ".");
    }
}

public  void setMotto(String newMotto)
{
    Motto = newMotto;
    System.out.println("The candidate's motto is now set to \"" + newMotto + "\"");
}

public void displayAll()
{
    System.out.println(State + "\t" + Name_of_Candidate + "\t" 
    + Election_Office + "\t" + 
    Party + "\t" + Number_of_Votes + 
    "\t" + Dollars_Spent + "\t" + Motto); //Display all info separated by tabs
}

public String toString()
{
    String ReturnThis = (State + "\t" + Name_of_Candidate + "\t" + 
    Election_Office + "\t" + Party + 
    "\t" + Number_of_Votes + "\t" + 
    Dollars_Spent + "\t" + Motto); //same as displayAll() just in one string

    return ReturnThis;
}

public boolean equals(Candidate_Info PassedCandidate)
{
    boolean TF [] = new boolean [NUMBER_OF_ATTRIBUTES]; //An array of booleans that match the number of attributes above

    boolean finalResult; //This will hold the final boolean result of all the below calculations

    if(State.equals(PassedCandidate.getState())) TF[0] = true; //This isn't the most graceful method of doing this, but it works
    else TF[0] = false;

    if(Name_of_Candidate.equals(PassedCandidate.getName())) TF[1] = true;
    else TF[1] = false;

    if(Election_Office.equals(PassedCandidate.getOffice())) TF[2] = true;
    else TF[2] = false;

    if(Party == PassedCandidate.getParty()) TF[3] = true;
    else TF[3] = false;

    if(Number_of_Votes == PassedCandidate.getVotes()) TF[4] = true;
    else TF[4] = false;

    if(Dollars_Spent == PassedCandidate.getSpent()) TF[5] = true;
    else TF[5] = false;

    if(Motto.equals(PassedCandidate.getMotto())) TF[6] = true;
    else TF[6] = false;

    if(TF[0] && TF[1] && TF[2] && TF[3] && TF[4] && TF[5] && TF[6]) finalResult = true; //If ALL OF THE ATTRIBUTES equal the attributes of the passed candidate, therefore making all the TF variables true, then they are equal
    else finalResult = false;

    return finalResult;
}
}
  • Check your capitalization of variables, particularly CFile and CFileReadIn - I see differences in the first few lines of your class. – J. Titus Nov 29 '14 at 06:09
  • 1
    Took a quick look. That "Cannot find symbol" is because you named it "CFile" but there you're using "Cfile". The case needs to match. It's easier to see errors in an java IDE such as eclipse/intellij idea. – manyways Nov 29 '14 at 06:10

1 Answers1

1

Samuel, try and use the "camelCase" naming convention where the first letter of a variable name is lowercase, not uppercase. Only classes should get uppercase first letters. That lets you easily identify whether something is a class or a variable.

Not doing this has already resulted in a small mistake in the beginning of your code, where you accidentally refer to the CFile variable as Cfile, which Java will interpret as two different things. This is why you were getting the error about not being able to find the symbol, because Java didn't know what Cfile was, only CFile.

I also took a look lower in your code. You create a candidates variable, but then accidentally keep referring to it by its class Candidate_Info in the for and while loops.

To construct a new object out of a class, you must put the new keyword before it. You cannot just directly reference the constructor method as you did in the for loop.

Here's a version that may better show what I mean:

import java.util.Scanner; //I'm only gonna need scanner for this project I think
import java.io.*;

public class HuntowskiSamuel //This is what the file name should be as well as the class name
{
    public static void main (String [] args) throws IOException
    {
    File cFile = new File("cipcs115.txt"); //This will be the file and scanner variable used to pull the data for the candidates
    Scanner scan = new Scanner(cFile);

    File cFileReadIn = new File("cipcs115.txt"); //While this file and scanner will be used to pull the number of candidates from the same file...hopefully
    Scanner scanReadIn = new Scanner(cFileReadIn);

    String stateName = "No name yet"; //This is where the state value will be held, that controls the input of the file
    int numberOfCandidates = 0; // This will pull the number of candidates for the array size
    String garbage = "Empty"; //This is where the ReadIn scanner can dump excess stuff

    stateName = scanReadIn.next(); //The prime read for the while loop

    int numberOfLettersEntered [] = new int [8]; //Since we only have the letters L, C, V, S, D, P, Q, and X (others/errors) that were entered, IN THAT ORDER. Its not graceful but it works

    while(stateName != "END_OF_FILE") //While we haven't reached the end of the file
    {
        for(int i = scanReadIn.nextInt(); i > 0; i--) //Read in the number of candidates, then run the loop that number of times
        {
            numberOfCandidates++; //Every time this loop runs, it means there is one more candidate for the total amount
            garbage = scanReadIn.nextLine(); //This will take all the important info and dump it, seeing as we only need the number of candidates and the state name
        }
        stateName = scanReadIn.next(); //Pull the next state name
    }

    Candidate_Info candidates [] = new Candidate_Info [numberOfCandidates]; //This creates an array of the exact size of the number of candidates in the file

    for(int i = 0; i < numberOfCandidates; i++) //Running the constructor for each and every candidate created
    {
        candidates[i] = new Candidate_Info();
    }

    stateName = scan.next(); //Prime read for the data taking loop

    while(stateName != "END_OF_FILE") //The same as the other loop, only using the real file and scanner variables
    {
        int candidateNumber = 0; //This will keep the array number straight from 0 to however many candidates - 1

        for(int i = 0; i < scan.nextInt(); i++) //This will loop for each of the candidates in ONE STATE, it pulls the number of candidates as an int
        {
            candidates[candidateNumber].setState(stateName);
            candidates[candidateNumber].setName(scan.next());
            candidates[candidateNumber].setOffice(scan.next());
            candidates[candidateNumber].setParty(scan.next().charAt(0)); //This might not work because it is just a single character versus the string that it would be passed
            candidates[candidateNumber].setVotes(scan.nextInt());
            candidates[candidateNumber].setSpent(scan.nextDouble());
            candidates[candidateNumber].setMotto(scan.nextLine());
            candidateNumber++;
        }
        stateName = scan.next();
    }
}
}

Note that without your text files, it's going to be hard to determine how your code will actually work, but I just wanted to warn you about a common problem with Scanner when you mix nextInt with nextLine. See this.

Community
  • 1
  • 1
Rob Wise
  • 4,560
  • 3
  • 21
  • 30