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

时间:2016-08-30 06:26:50

标签: java arrays methods

我正在尝试使用具有方法的代码转移我的代码

我正在转移所有的东西,但我收到的错误

编辑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

现在正在运行代码,但显示元素的空值并且不会停留在输入文件的预期索引处。

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

我想从输入文件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

2 个答案:

答案 0 :(得分:0)

Testing1.java:40,删除[0]

File data = new File(inputData);

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

答案 1 :(得分:0)

以下是更新的答案

在这种情况下最好使用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