【发布时间】:2019-11-23 12:45:03
【问题描述】:
在编写将数组列表和整数作为输入的方法的标头时出现多个错误。
我尝试了几种不同的方法来编写该方法的标头。正文很好,给了我想要的东西,但我无法获取标题/调用名称(我不知道您调用方法的第一行)以不引发错误
/**
* Creates Arraylist "list" using prompt user for the input and output file path and sets the file name for the output file to
* p01-runs.txt
*
*/
Scanner scan = new Scanner(System.in);
System.out.println("Please enter the path to your source file: ");
String inPath = scan.nextLine(); // sets inPath to user supplied path
System.out.println("Please enter the path for your source file: ");
String outPath = scan.nextLine() + "p01-runs.txt"; // sets outPath to user supplied input path
ArrayList<Integer> listRunCount = new ArrayList<Integer>();
ArrayList<Integer> list = new ArrayList<Integer>();
/**
* Reads data from input file and populates array with integers.
*/
FileReader fileReader = new FileReader(inPath);
BufferedReader bufferedReader = new BufferedReader(fileReader);
// file writing buffer
PrintWriter printWriter = new PrintWriter(outPath);
System.out.println("Reading file...");
/**
* Reads lines from the file, removes spaces in the line converts the string to
* an integer and adds the integer to the array
*/
File file = new File(inPath);
Scanner in = new Scanner(file);
String temp=null;
while (in.hasNextLine()) {
temp = in.nextLine();
temp = temp.replaceAll("\\s","");
int num = Integer.parseInt(temp);
list.add(num);
}
listRunCount.findRuns(list, RUN_UP);
//********************************************************************************************************
public ArrayList<Integer> findRuns(ArrayList<Integer> list, int RUN_UP){
returns listRunCount;
}
错误信息
Multiple markers at this line
- Syntax error on token "int", delete this token
- Syntax error, insert ";" to complete LocalVariableDeclarationStatement
- Integer cannot be resolved to a variable
- ArrayList cannot be resolved to a variable
- Syntax error, insert ";" to complete LocalVariableDeclarationStatement
- Illegal modifier for parameter findRuns; only final is permitted
- Syntax error, insert ") Expression" to complete CastExpression
- Syntax error on token "findRuns", = expected after this token
- Syntax error, insert "VariableDeclarators" to complete
LocalVariableDeclaration
- Syntax error, insert ";" to complete Statement
【问题讨论】:
-
您的方法似乎是在另一个方法的中间声明的。那不是有效的Java。方法必须在类中声明,而不是在另一个方法中。
-
你能把整个班级结构贴出来吗?正如@JB Nizet 所说,看起来您已经创建了一个方法而没有在 findRuns 之前关闭该方法。
-
好的,我明白了!所以老师希望我们以这种方式开始我们的所有作业......
-
// Main.java public class Main { public static void main(String[] pArgs) { Main mainObject = new Main(); // 或者你可以直接写: new Main().run(); mainObject.run() // 代替这两行。 } private void run() { // 您将在此处开始编写代码以实现软件要求。 } }
-
我现在看到 fileRuns 在 main 方法中。我可以解决这个问题。