我收到<identifier>预期错误

时间:2016-04-13 16:28:32

标签: java

在一堂课中,我有一个方法:

public void initalizeElements()
{
//does some stuff
}

然后在另一个课程中我尝试:

Molecules mol = new Molecules();

mol.initalizeElements();

我在第二行收到错误&#34;预期&#34;

我觉得这很简单,我只是简单地看了一眼,但我仍然无法弄明白。在有人说出来之前,是的,我看了几百个与我有同样问题的其他问题。但他们似乎都没有帮助我。

完整代码:

package mymolecules;
public class Molecules 
{
        public String[] elements = new String[35];
        private int lazyCounter = 0;

        public void initalizeElements()
        {
        elements[lazyCounter] = "H";
        lazyCounter += 1;
        elements[lazyCounter] = "He";
        lazyCounter += 1;
        elements[lazyCounter] = "Li";
        lazyCounter += 1;
        elements[lazyCounter] = "Be";
        lazyCounter += 1;
        elements[lazyCounter] = "B";
        lazyCounter += 1;
        elements[lazyCounter] = "C";
        lazyCounter += 1;
        elements[lazyCounter] = "N";
        lazyCounter += 1;
        elements[lazyCounter] = "O";
        lazyCounter += 1;
        elements[lazyCounter] = "F";
        lazyCounter += 1;
        elements[lazyCounter] = "Ne";
        lazyCounter += 1;
        elements[lazyCounter] = "Na";
        lazyCounter += 1;
        elements[lazyCounter] = "Mg";
        lazyCounter += 1;
        elements[lazyCounter] = "Al";
        lazyCounter += 1;
        elements[lazyCounter] = "Si";
        lazyCounter += 1;
        elements[lazyCounter] = "P";
        ....
        ....
        //on so on for the hole periodic table
        }
}

package mymolecules;
public class Parser 
{
    private String conString = "";
    private int conInt = 0;
    private int indexer = 0;

    private int lazyCounter = 0;

    private String sortList = "";
    private int sortCount = 0;

    Molecules mol = new Molecules();

    mol.initalizeElements();


    private String formula;
    public Parser(String Formula)
    {
        this.formula = Formula;
    }
    public void Sort()
    {
    while (lazyCounter > 34)
    {
        try {
        if (formula.contains(mol.elements[lazyCounter])) //Does the String contain the letter "H"
        {
            indexer = formula.indexOf(mol.elements[lazyCounter]); //indexer = where in the String "H" is located.
            if (formula.substring(indexer+1,indexer+2).matches("e")) //if "e" is located right after "H"
                    {
                        //System.out.println("Skip"); //Skipping because we'll deal with it later
                    }
            else
            {
               if (formula.substring(indexer+1,indexer+2).matches("[1-999].*")) // if any number is located after H
               {
                   conString = formula.substring(indexer+1,indexer+2); //Take the number after "H" and put it in conString
                   conInt = Integer.parseInt(conString); //conInt = ConString as an Integer
                   System.out.println("H"+conInt); //Print out "H" and the number
               }
               else
               {
                   System.out.println(mol.elements[lazyCounter]);
               }
            }   
        }
        }
        catch (java.lang.StringIndexOutOfBoundsException e) //Catches the error from earlier
        {
            System.out.println("Whew caught an error!"); //rest of this is self explanitory.
            System.out.println(mol.elements[lazyCounter]);
        }
        lazyCounter += 1;
    }
    }


}

1 个答案:

答案 0 :(得分:0)

您无法在用于定义变量的类的部分中开始调用方法。将它们移动到构造函数:

tidyc1[which(tidyc1[,5]==1),1:4]

仅在定义变量的部分中保留mol的声明,其中包含:

mol = new Molecules();
mol.initalizeElements();