我的ArrayList代码出错(Brackets Error)

时间:2016-08-29 05:27:27

标签: arrays curly-brackets

我编译文件时出现错误

我一直在检查我的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的原因)

electricity.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]);
           
      }
     
     
   }
       
                                                                                                                                                                                                                                                                                                   
            
}

1 个答案:

答案 0 :(得分:1)

我采取了一些自由,但我认为这接近你所需要的。

import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;

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 {

        List<Integer> array = InputFiler.readFileReturnIntegers(args[0]);
        InputFiler.printArrayAndIntegerCount(array, args[0]);
    }

}//end of main

private static List<Integer> readFileReturnIntegers(String filename) {
    File file ;
    Scanner inputFromFile = null;
    List<Integer> array = new ArrayList<>();
    if (filename.length() == 0) {
        System.out.println("Please enter the file name as the 1st commandline argument.");
    } else {   //attempt connect and read file
        file = new File(filename);
        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 \"" + filename + "\":\n");
            //loop and print to check if file connected

            //read next integer and store into array
            while (inputFromFile.hasNextLine()) {
                try {
                    int x = Integer.parseInt(inputFromFile.nextLine());
                    array.add(x);
                    System.out.println(x);
                } catch (InputMismatchException ime) {
                    inputFromFile.next();
                } catch (NoSuchElementException nsee) {
                }
            }
        }
    }

    return array;
}//end of readFileReturnIntegers

private static void printArrayAndIntegerCount(List<Integer> array, String inputFile) {
    System.out.println("Number of integers in file \"" + inputFile + "\" = " + array.size());
    //print array index and elements
    for (int i : array) {
        System.out.print("element = " + i);
    }
}//end of printArrayAndIntegerCount
}//end of class

theser是显示的错误

&#13;
&#13;
InputFiler.java:25: error: cannot find symbol
   private static List<Integer> readFileReturnIntegers(String filename) {
                  ^
  symbol:   class List
  location: class InputFiler
InputFiler.java:65: error: cannot find symbol
   private static void printArrayAndIntegerCount(List<Integer> array, String inputFile) {
                                                 ^
  symbol:   class List
  location: class InputFiler
InputFiler.java:19: error: cannot find symbol
         List<Integer> array = InputFiler.readFileReturnIntegers(args[0]);
         ^
  symbol:   class List
  location: class InputFiler
InputFiler.java:28: error: cannot find symbol
      List<Integer> array = new ArrayList<>();
      ^
  symbol:   class List
  location: class InputFiler
InputFiler.java:28: error: cannot find symbol
      List<Integer> array = new ArrayList<>();
                                ^
  symbol:   class ArrayList
  location: class InputFiler
5 errors
&#13;
&#13;
&#13;

我在顶部添加了这些导入

&#13;
&#13;
import java.util.Scanner;
import java.io.File;
import java.util.StringTokenizer;
import java.util.InputMismatchException;
import java.io.FileNotFoundException;
import java.util.NoSuchElementException;
&#13;
&#13;
&#13;