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



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

的整数时遇到了麻烦

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

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

是否有一种方法可以让它不显示空值,并且如果我运行文件,是否有一种方法可以匹配power .txt =4和1000.txt =1001的大小?

使用while循环代替for循环,如

while(array[i]!=null) {
    System.out.println(array[i++]);
}

这是用来打印的

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

最新更新