我試圖與有方法的代碼轉移我的代碼,而不是錯誤:陣列必需的,但字符串發現
我傳送所有的東西,但我得到的錯誤遍佈
編輯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)
{
\t //This array will serve as the storage for the integers.
Integer [] listOfIntegers = new Integer[10000];
Integer index = new Integer(0);
int count = 0;
\t \t
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
{
\t //This will create a connection to the file.
inputData = new Scanner(data);
}
catch (FileNotFoundException exception)
{
\t \t //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
{
\t //reads an integer in a line of text
Integer element = inputData.nextInt();
listOfIntegers[count] = element;
\t \t //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)
{
\t \t //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 \t
}//end of else
}//end of main
}//end of class
現在的工作代碼,但顯示元素的空值,並不停止在輸入文件的預期索引。
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
{
\t //This will create a connection to the file.
inputReader = new Scanner(data);
}
catch (FileNotFoundException exception)
{
\t \t //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
{
\t //reads an integer in a line of text
Integer element = inputReader.nextInt();
listOfIntegers[count]=element;
\t \t //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)
{
\t \t //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
我想輸出整數從輸入文件electricity.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
現在還不清楚你在問什麼。請閱讀[問]和[mcve]。 – xenteros
這個標題將適合99%的帖子在這裏。你能想出一個更具描述性的標題嗎? – Henry
'公共靜態整數[] readFileReturnIntegers(String inputData)'是導致錯誤'File data = new File(inputData [0]);'(first errror) – emotionlessbananas