0

現在、ファイルをコンパイルするときにエラーの問題が発生しています

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を使用した理由です)

電気.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 ファイルの特定の数に達すると null が表示されます

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

4

1 に答える 1

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

これらは表示されていたエラーでした

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

これらのインポートを上部に追加しました

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

于 2016-08-29T06:09:50.867 に答える