-3

这真的很奇怪,我为这个程序编写了一个类,并且正要测试它如何从文件中读取数据,但是我得到一个“找不到符号”错误,它指的是第一个扫描仪中声明的“新”。第二个 Scanner 变量中的“=”出现相同的错误,并且稍后会出现一堆找不到所有“Candidate_Info[i]”对象的符号。我不知道我的错误在哪里。顺便说一句,我正在使用记事本++,也使用记事本++编译和运行它。

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

1 回答 1

1

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

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

我还看了一下你的代码。您创建了一个变量,但随后意外地在and循环中candidates通过其类继续引用它。Candidate_Infoforwhile

要从一个类中构造一个新对象,你必须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();
    }
}
}

请注意,如果没有您的文本文件,将很难确定您的代码将如何实际工作,但我只是想警告您,Scanner当您nextIntnextLine. 看到这个

于 2014-11-29T06:31:45.687 回答