我有一些错误 - Java程序

时间:2014-11-29 05:57:03

标签: java class compiler-errors cannot-find-symbol

这真的很奇怪,我为这个程序编写了一个类,并且即将测试它是如何从文件中读取数据但是我得到了一个"找不到符号"引用" new"的错误在第一个扫描仪声明。 " ="的错误相同在第二个Scanner变量中,一堆无法找到所有" Candidate_Info [i]"的符号。以后的对象。我不知道我的错误在哪里。我顺便使用notepad ++,使用notepad ++编译和运行它。

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();
    }
}
}

这是我写的课程的代码。

//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;
}
}

1 个答案:

答案 0 :(得分:1)

Samuel,尝试使用“camelCase”命名约定,其中变量名的第一个字母是小写,而不是大写。只有类才能获得大写的第一个字母。这可以让您轻松识别某个东西是类还是变量。

不这样做已经导致代码开头出现一个小错误,你不小心将CFile变量称为Cfile,Java会将其解释为两个不同的东西。这就是为什么你得到关于无法找到符号的错误,因为Java不知道Cfile是什么,只有CFile

我的代码也低了一些。您创建了candidates变量,但随后在Candidate_Infofor循环中不小心继续引用它while

要从类中构造新对象,必须在其前面加上new关键字。您不能像在for循环中那样直接引用构造函数方法。

这是一个可以更好地展示我的意思的版本:

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();
    }
}
}

请注意,如果没有您的文本文件,很难确定代码的实际工作方式,但我只想在ScannernextInt混合时警告您nextLine的常见问题{{1}}。 See this