【发布时间】:2016-08-30 06:26:50
【问题描述】:
我正在尝试使用具有方法的代码来转移我的代码
我正在转移所有的东西,但我到处都收到错误
编辑 4:
我的原始代码:
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.InputMismatchException;
import java.util.NoSuchElementException;
public class Testing1
{
public static void main(String[] myFile)
{
//This array will serve as the storage for the integers.
Integer [] listOfIntegers = new Integer[10000];
Integer index = new Integer(0);
int count = 0;
if(myFile.length == 0)
{
//This prompt will show when no input detected on the command line arguments.
System.out.println("Please input the file name.");
}
else
{
//Otherwise, it will read the text file.
File data = new File(myFile[0]);
Scanner inputData = null;
try
{
//This will create a connection to the file.
inputData = new Scanner(data);
}
catch (FileNotFoundException exception)
{
//This message will appear when the text file entered doesn't exist.
System.out.print("ERROR: File not found for: \"");
System.out.println(myFile[0]+ "\"");
}
//If the text file does exist, it will go through the file.
if (inputData != null)
{
System.out.print("number of integers in file " + " \"");
System.out.println(myFile[0] + "\"");
while (inputData.hasNextLine())
{
try
{
//reads an integer in a line of text
Integer element = inputData.nextInt();
listOfIntegers[count] = element;
//prints out the integer and and its index in the array
System.out.println("index = " + index + ", element = " + element);
index ++;
count ++;
}
catch (InputMismatchException e)
{
String word = inputData.next();
}
catch (NoSuchElementException e)
{
//to avoid program crashing against this exception.
}
}//end of while
System.out.print("\nTotal number of integers in file: " + " \"");
System.out.println(myFile[0] + "\"" + " = " + count);
}//end of if
}//end of else
}//end of main
}//end of class
现在可以工作的代码,但显示元素的 null 值,并且不会在输入文件的预期索引处停止。
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.InputMismatchException;
import java.util.NoSuchElementException;
public class Testing1
public static void main(String[] args)throws InputMismatchException
{
if(args.length == 0)
{
//This prompt will show when no input detected on the command line arguments.
System.out.println("Please input the file name.");
}
else
{
Integer[] array = Testing1.readFileReturnIntegers(args[0]);
Testing1.printArrayAndIntegerCount(array, args[0]);
}
}// end of main
public static Integer []readFileReturnIntegers(String inputData)
{
Integer [] listOfIntegers = new Integer[10000];
Integer index = new Integer(0);
int count = 0;
File data = new File(inputData);
Scanner inputReader = null;
try
{
//This will create a connection to the file.
inputReader = new Scanner(data);
}
catch (FileNotFoundException exception)
{
//This message will appear when the text file entered doesn't exist.
System.out.print("ERROR: File not found for: \"");
System.out.println(inputData+ "\"");
}
//If the text file does exist, it will go through the file.
if (inputData != null)
{
System.out.print("number of integers in file " + " \"");
System.out.println(inputData + "\"");
while (inputReader.hasNextLine())
{
try
{
//reads an integer in a line of text
Integer element = inputReader.nextInt();
listOfIntegers[count]=element;
//prints out the integer and and its index in the array
System.out.println("index = " + index + ", element = " + element);
index ++;
count ++;
}
catch (InputMismatchException e)
{
String word = inputReader.next();
}
catch (NoSuchElementException e)
{
//to avoid program crashing against this exception.
}
}//end of while
}
return listOfIntegers;
}//end of readFileReturnIntegers
public static void printArrayAndIntegerCount(Integer [] array, String inputData)
{
int count = 0;
System.out.print("\nTotal number of integers in file: " + " \"");
System.out.println(inputData + "\"" + " = " + count);
}//end of printArrayAndIntegerCount
}//end of class
我的错误:
Testing1.java:81: error: cannot find symbol
return array;
^
symbol: variable array
location: class Testing1
1 error
我想从输入文件electrical.txt 和 1000txt 中输出整数。
number of integers in file "electricity.txt" = 4
index = 0, element = 1877
index = 1, element = 1923
index = 2, element = 1879
index = 3, element = 2000
还有这个
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
index = 9, element = 23
to index 1000 and element 7919
【问题讨论】:
-
不清楚你在问什么。请阅读How to Ask 和minimal reproducible example。
-
这个标题适合 99% 的帖子。你能想出一个更具描述性的标题吗?
-
public static Integer []readFileReturnIntegers(String inputData)是错误File data = new File(inputData[0]);的原因(第一个错误) -
抱歉大家重命名了标题
-
哦,拍摄已修复@asteriskninja!我注意到我正在尝试在其上使用数组