从用户输入外部文件(数组)输出整数

时间:2016-08-28 21:09:19

标签: java arrays null

所以我目前在输出每个索引及其元素

的整数时遇到了麻烦

我的指示是匹配这两个文本文件的输出:

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从索引0到1000

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 = 1000, element = 7919

这是我的代码:

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 PaulGeorge03
{
     /*************************************************************
     *  Outputs integers from user input external files.
     ********************************************************************************/
    
   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 = PaulGeorge03.readFileReturnIntegers(commandlineArguments[0]);
         PaulGeorge03.printArrayAndIntegerCount(array, commandlineArguments[0]);
      }
   }	
   public static Integer []readFileReturnIntegers(String inputFile)
   {
        
      Integer [] array = new Integer [10000];
        
      
      File file = new File(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)
      {
         int i=0; // counter 
         while (scanFile.hasNextLine()) 
         {   
            try
            {                       
               int element = scanFile.nextInt(); 
               array[i++]=element;
               	                          
            }
            catch (InputMismatchException exception)
            {
               scanFile.next();
            } 
         }
      }         
      return array;          
   }
      
   public static void printArrayAndIntegerCount(Integer [] array, String inputFile)
   { 
      int num = 0;
      Integer lengthOfArray = array.length;
      
      System.out.println("number of integers in file " + inputFile + " = " + lengthOfArray);
      
      for (int i = 0; i < lengthOfArray; i++)
            
         
      {
            
         System.out.println("index = " + i + ", element = "+ array[i]);
         
            
      }
      
      
   }
      	
      																																																																								
      	     
}

1000.txt有效,但它会超过极限= 4并转到10 以来 for (int i = 0; i <10; i++)

electric.txt在= 3之后显示为空,因为所需的是= 10

number of integers in file electricity.txt = 10000
index = 0, element = 1877
index = 1, element = 1923
index = 2, element = 1879
index = 3, element = 2000
index = 4, element = null
index = 5, element = null
index = 6, element = null
index = 7, element = null
index = 8, element = null
index = 9, element = null

有没有办法让它不显示两者的空值,是否有办法匹配电力的大小= 4,如果我运行文件,则匹配1000.txt的= 1001?

1 个答案:

答案 0 :(得分:0)

而不是for循环使用像

这样的while循环
while(array[i]!=null) {
    System.out.println(array[i++]);
}

这是用于打印。

我强烈建议您使用ArrayList来存储列表。 ArrayList具有动态大小,这意味着当您向其添加项目时,它将增加大小。在不知道你有多少元素的情况下创建一个10000大小的数组并不是一个好主意。