Java 错误:需要数组,但找到字符串

Java 错误:需要数组,但找到字符串,java,arrays,methods,Java,Arrays,Methods,我试图用一个有方法的代码来传输我的代码 我转移了所有的东西,但是我到处都是错误 编辑4: 我的原始代码: import java.util.Scanner; 导入java.io.File; 导入java.io.FileNotFoundException; 导入java.util.InputMismatchException; 导入java.util.NoSuchElementException; 公共类测试1 { 公共静态void main(字符串[]myFile) { //此数组将用作整数的

我试图用一个有方法的代码来传输我的代码

我转移了所有的东西,但是我到处都是错误

编辑4:

我的原始代码:

import java.util.Scanner;
导入java.io.File;
导入java.io.FileNotFoundException;
导入java.util.InputMismatchException;
导入java.util.NoSuchElementException;
公共类测试1
{
公共静态void main(字符串[]myFile)
{
//此数组将用作整数的存储。
整数[]listOfIntegers=新整数[10000];
整数索引=新整数(0);
整数计数=0;
如果(myFile.length==0)
{
//当在命令行参数上未检测到输入时,将显示此提示。
System.out.println(“请输入文件名。”);
}
其他的
{
//否则,它将读取文本文件。
文件数据=新文件(myFile[0]);
扫描仪输入数据=空;
尝试
{
//这将创建到该文件的连接。
inputData=新扫描仪(数据);
} 
捕获(FileNotFoundException异常)
{
//当输入的文本文件不存在时,将显示此消息。
System.out.print(“错误:找不到文件:\”);
System.out.println(myFile[0]+“\”);
} 
//如果文本文件确实存在,它将遍历该文件。
如果(inputData!=null)
{
System.out.print(“文件“+”\”中的整数数);
System.out.println(myFile[0]+“\”);
while(inputData.hasNextLine())
{
尝试
{  
//读取文本行中的整数
整型元素=inputData.nextInt();
ListofInteger[计数]=元素;
//打印整数及其在数组中的索引
System.out.println(“index=“+index+”,element=“+element”);
索引++;
计数++;
}
捕获(输入不匹配异常e)
{
字符串字=inputData.next();
}
捕获(无接触元素例外e)
{
//以避免程序因此异常而崩溃。
}
}//结束
System.out.print(“\n文件中的整数总数:“+”\”);
System.out.println(myFile[0]+“\”+“=”+计数);
}//if结束
}//别有用心
}//干管末端

}//课后测试1.java:40,删除[0]

File data = new File(inputData);

inputData本身就是您的文件名,因此使用inputData而不是inputData[0]在System.out.println中的任何地方

在这种情况下最好使用ArrayList,这样我们可以创建动态数组。
使用数组无法实现的位置

import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.InputMismatchException;
import java.util.NoSuchElementException;
import java.util.ArrayList;

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
      {
         ArrayList<Integer> array = Testing1.readFileReturnIntegers(args[0]);
         Testing1.printArrayAndIntegerCount(array, args[0]);
      }
   }// end of main

   public static ArrayList<Integer> readFileReturnIntegers(String inputData)
   { 
      ArrayList<Integer> listOfIntegers = new ArrayList<Integer>(); 
      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.add(element);
                    //prints out the integer and and its index in the array
               System.out.println("index = " + count + ", element = " + element);
               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(ArrayList<Integer> array, String filename)
   {  
      System.out.print("\nTotal number of integers in file: " + " \"");
      System.out.println(filename + "\"" + " = " + array.size());

   }//end of printArrayAndIntegerCount

}//end of class
import java.util.Scanner;
导入java.io.File;
导入java.io.FileNotFoundException;
导入java.util.InputMismatchException;
导入java.util.NoSuchElementException;
导入java.util.ArrayList;
公共类测试1{
publicstaticvoidmain(字符串[]args)抛出inputmatchException
{
如果(args.length==0)
{
//当在命令行参数上未检测到输入时,将显示此提示。
System.out.println(“请输入文件名”);
}
其他的
{
ArrayList数组=Testing1.readFileReturnIntegers(args[0]);
测试1.PrintArrayandIndegerCount(数组,args[0]);
}
}//干管末端
公共静态ArrayList ReadFileReturnInteger(字符串inputData)
{ 
ArrayList listOfIntegers=新的ArrayList();
整数计数=0;
文件数据=新文件(inputData);
扫描仪输入读取器=空;
尝试
{
//这将创建到该文件的连接。
inputReader=新扫描仪(数据);
} 
捕获(FileNotFoundException异常)
{
//当输入的文本文件不存在时,将显示此消息。
System.out.print(“错误:找不到文件:\”);
System.out.println(inputData+“\”);
} 
//如果文本文件确实存在,它将遍历该文件。
如果(inputData!=null)
{
System.out.print(“文件“+”\”中的整数数);
System.out.println(inputData+“\”“);
while(inputReader.hasNextLine())
{
尝试
{  
//读取文本行中的整数
整数元素=inputReader.nextInt();
添加(元素);
//打印整数及其在数组中的索引
System.out.println(“index=“+count+”,element=“+element”);
计数++;
}
捕获(输入不匹配异常e)
{
String word=inputReader.next();
}
捕获(无接触元素例外e)
{
//以避免程序因此异常而崩溃。
}
}//结束
}
返回整数列表;
}//ReadFileReturnInteger的结尾
公共静态void printaryandintegercount(ArrayList数组,字符串文件名)
{  
System.out.print(“\n文件中的整数总数:“+”\”);
System.out.println(文件名+“\”+“=”+array.size());
}//打印阵列和数字计数结束
}//下课

不清楚你在问什么。请仔细阅读,这个标题适合99%的文章。你能想出一个更具描述性的标题吗?
publicstaticinteger[]readFileReturnIntegers(stringinputdata)
是导致错误的原因
filedata=newfile(inputData[0])(第一个错误