面对基本问题的Java新手

时间:2013-10-01 21:36:46

标签: java

在编程方面我是个新手,我第一次使用Java编写。这是在大学里的任务。我正在使用NetBeans。

你看我完全迷失了,不知道我能做些什么来解决我的问题。这是我面临的问题:

1:      for (String edge : edges.elementSet()) - 它说找不到符号'elementSet'。我该怎么做才能解决这个问题?

2:      myConnections.addAll(y.getListOfLinks(tagsOfTheCurrentPictureAsAnArray)); - 在这里,它说找不到符号'tagsOfTheCurrentPictureAsAnArray',但我之前提到过:

String[] tagsOfTheCurrentPictureAsAnArray = tagsOfTheCurrentPicture.split(",");
for (String x : tagsOfTheCurrentPictureAsAnArray) {

我在这里做错了什么? 完整的脚本:

public static void main(String[] args) throws FileNotFoundException, IOException {
    String myFile = "C:\\Users\\Nils\\Desktop\\Uni\\Let's go Digital\\Digital data in practise\\Lecture4\\Datasets\\WWII.csv";
    FileWriter myFileWriter = new FileWriter("C:\\nodes.txt");
    BufferedWriter myBufferedNodesWriter = new BufferedWriter(myFileWriter);
    FileWriter myEdgesWriter = new FileWriter("C:\\edges.txt");
    BufferedWriter myBufferedEdgesWriter = new BufferedWriter(myFileWriter);
    FileReader myFileReader = new FileReader(myFile);
    BufferedReader myBufferedReader = new BufferedReader(myFileReader);
    String Comma = ",";
    char Commaderps = Comma.charAt(0);
    CsvReader excellentCsvReader;
    excellentCsvReader = new CsvReader(myBufferedReader, Commaderps);
    String textDelimiter = "\"";
    char Backslash = textDelimiter.charAt(0);
    excellentCsvReader.setTextQualifier(Backslash);
    excellentCsvReader.setUseTextQualifier(true);
    excellentCsvReader.readRecord();
    HashMultiset<String> myTags = HashMultiset.create();
    HashMultiset<String> myConnections = HashMultiset.create();
    while (excellentCsvReader.readRecord()) {
        String[] values = excellentCsvReader.getValues();
        String tagsOfTheCurrentPicture = values[2];
        String[] tagsOfTheCurrentPictureAsAnArray = tagsOfTheCurrentPicture.split(",");
        for (String x : tagsOfTheCurrentPictureAsAnArray) {
            myTags.add(x);
        }
        myBufferedNodesWriter.write("Id,Label,Number");
        myBufferedNodesWriter.newLine();
        for (String tag : myTags) {
            myBufferedNodesWriter.write(tag);
            myBufferedNodesWriter.write(",");
            myBufferedNodesWriter.write(tag);
            myBufferedNodesWriter.write(",");
            myBufferedNodesWriter.write(myTags.count(tag));
            myBufferedNodesWriter.newLine();
        }
        myBufferedNodesWriter.close();
        myBufferedEdgesWriter.write("Id,Source,Target,Type,Weight");
        myBufferedEdgesWriter.newLine();
        Integer idEdge = 0;
        for (String edge : edges.elementSet()) {
            idEdge = idEdge + 1;
            myBufferedEdgesWriter.write(idEdge);
            myBufferedEdgesWriter.write(",");
            myBufferedEdgesWriter.write(edge);
            myBufferedEdgesWriter.write(",");
            myBufferedEdgesWriter.write("Undirected");
            myBufferedEdgesWriter.write(",");
            myBufferedEdgesWriter.write(String.valueOf(edges.count(edge)));
            myBufferedEdgesWriter.newLine();
        }
        myBufferedEdgesWriter.close();
    }
    Utils y = new Utils();
    myConnections.addAll(y.getListOfLinks(tagsOfTheCurrentPictureAsAnArray));
    System.out.println("Tag:" + tagsOfTheCurrentPictureAsAnArray[3]);

}

}

2 个答案:

答案 0 :(得分:1)

我想我知道tagsOfTheCurrentPictureAsAnArray问题在哪里

while (excellentCsvReader.readRecord()) {
    ...
    String[] tagsOfTheCurrentPictureAsAnArray = tagsOfTheCurrentPicture.split(",");
    for (String x : tagsOfTheCurrentPictureAsAnArray) {
        myTags.add(x);
    }
    ...
}
Utils y = new Utils();
myConnections.addAll(y.getListOfLinks(tagsOfTheCurrentPictureAsAnArray));

上次使用它时,它将超出范围,因为它超出了while循环。如果你想在while循环之后保持它,你必须在while循环之外声明它。

所以你想要

String[] tagsOfTheCurrentPictureAsAnArray = tagsOfTheCurrentPicture.split(",");
while (excellentCsvReader.readRecord()) {
    ...

如果你想为每次迭代使用相同的数组,或者

String[] tagsOfTheCurrentPictureAsAnArray;
while (excellentCsvReader.readRecord()) {
    ...
    tagsOfTheCurrentPictureAsAnArray = tagsOfTheCurrentPicture.split(",");

如果你想在每次迭代时重新初始化它

答案 1 :(得分:0)

请发布整个代码。一些猜测:

1)edges的类没有名为elementSet的方法。方法名称是否拼写正确? edges的类型是否正确?

2)是否宣布了tagsOfTheCurrentPictureAsAnArray?如果是,是否在使用范围内声明?