目前在编译文件时出现错误问题
我一直在检查我的 while 循环和其他东西,看看问题出在哪里,但我找不到
这就像现在显示的唯一错误(我希望)
希望有人可以帮助解决我错过了哪个支架或它是什么类型的支架,因为我很困惑;_;
import java.util.Scanner;
import java.io.File;
import java.util.StringTokenizer;
import java.util.InputMismatchException;
import java.io.FileNotFoundException;
import java.util.NoSuchElementException;
public class InputFiler {
public static void main(String [ ] args)throws InputMismatchException
{
//error checking for commandline input
//to make sure the user entered at least one comandline argument
if(args.length == 0)
{
System.out.println("Please enter the file name " +
"as the 1st commandline argument.");
}
else
{
Integer[ ] array = InputFiler.readFileReturnIntegers(args[0]);
InputFiler.printArrayAndIntegerCount(array, args[0]);
}
}//end of main
if(filename.length() == 0){
System.out.println("Please enter the file name as the 1st commandline argument.");
}
else { //attempt connect and read file
File file = new File(filename);
Scanner inputFromFile = null;
}
try {
inputFromFile = new Scanner(file);
}
catch (FileNotFoundException fnfe) {
System.out.print("ERROR: File not found for \"");
System.out.println(filename+"\"");
}
//if made connection to file, read file
if(inputFromFile != null){
System.out.print("Reading from file \"" + inputFile + "\":\n");
//loop and print to check if file connected
//read next integer and store into array
while (inputFromFile.hasNextLine()) {
try {
x = inputFromFile.nextInt();
array[i] = x;
i++;
System.out.println(x);
}
catch (InputMismatchException ime) {
inputFromFile.next();
}
catch (NoSuchElementException nsee) {
}
}
}
//...
return array;
}//end of readFileReturnIntegers
System.out.println("Number of integers in file \"" + inputFile + "\" = " + array.length);
//print array index and elements
for(int i=0;i<array.length;i++) {
if(array[i] != null){
System.out.print("\nindex = " + i + ", ");
System.out.print("element = " + array[i]);
}
}
}
//...
}//end of printArrayAndIntegerCount
}//end of class
它显示的错误是这样的:
}//类结束^ 1错误
我当然错过了一些东西,但我不知道是哪一个
这些是我需要为我的输出显示的输入 txt 文件
它需要匹配顶部索引的长度(这就是我使用array.length的原因)
电力.txt
number of integers in file "electricity.txt" = 4
index = 0, element = 1877
index = 1, element = 1923
index = 2, element = 1879
index = 3, element = 2000
1000.txt
number of integers in file "1000.txt" = 1001
index = 0, element = 1000
index = 1, element = 2
index = 2, element = 3
index = 3, element = 5
index = 4, element = 7
index = 5, element = 11
index = 6, element = 13
index = 7, element = 17
index = 8, element = 19
till index 1000 and element 7919
这是可以正常工作的代码,但在每个 txt 文件达到一定数量后会显示空值
import java.util.Scanner;
import java.io.File;
import java.util.StringTokenizer;
import java.util.InputMismatchException;
import java.io.FileNotFoundException;
import java.util.NoSuchElementException;
public class Testing
public static void main(String[] commandlineArguments)throws InputMismatchException
{
if(commandlineArguments.length == 0)
{
System.out.println("Please enter the file name " +
"as the 1st commandline argument.");
}
else
{
Integer[] array = Testing.readFileReturnIntegers(commandlineArguments[0]);
Testing.printArrayAndIntegerCount(array, commandlineArguments[0]);
}
}
public static Integer []readFileReturnIntegers(String inputFile)
{
Scanner scanFile = null;
try {
scanFile = new Scanner(file);
}
catch (FileNotFoundException exception) {
System.out.print("ERROR: File not found for \"");
System.out.println(inputFile +"\"");
}
if(scanFile != null)
{
while (scanFile.hasNextLine())
{
try
{
int element = scanFile.nextInt();
array[size] = element;
size++;
}
catch (InputMismatchException exception)
{
scanFile.next();
}
catch (NoSuchElementException exception)
{
scanFile.next();
}
}
}
return array;
}
public static void printArrayAndIntegerCount(Integer [] array, String inputFile)
{
int num = 0;
System.out.println("number of integers in file " + inputFile + " = " + array.length);
for (int i = 0; i <array.length; i++)
{
System.out.println("index = " + i + ", element = "+ array[i]);
}
}
}