Java将文本文件的内容放入数组然后显示和操作数组内容的问题

时间:2015-11-11 19:00:20

标签: java arrays inputstream

好的,所以这是一个学校项目,我似乎无法弄清楚我做错了什么,我意识到有更好的方法来做很多这个但是必须这样做(我会注意到)特别是什么)。

我需要写两个非常简单的程序。

  1. 第一个程序创建一个300个随机整数的文件,范围为1到54,然后打印到文本文件。

  2. 第二个程序读取该文本文件并将内容转换为数组。然后对数组进行操作以输出一些内容,数字的平均值以及数字在特定范围内出现的次数。我也试图把整个阵列放在一边。不幸的是,所有部分基本上都不起作用,或者我不理解某些事情,我很确定它们都是lol。

  3. 计划1如下

    import java.util.*;
    import java.io.*;
    public class OutStream{ 
    public static void main(String[] args){
    int Counter = 0,
        Value;
    Random Gen = new Random();
    System.out.println("Generating Numbers and Text File Now");
       try{
            File out = new File("F:\\Text.txt");
            PrintWriter P = new PrintWriter(out);
            while(Counter <= 300){
                Value = Gen.nextInt(54)+1;
                P.println(Value);
                ++Counter;
                System.out.println("Value: " + Value);
            } 
            P.close();
      }catch(Exception E){ 
      E.printStackTrace();
      System.out.println(E.getMessage());
      } 
      System.out.println("The Program has ended");
     }   
    }
    

    第二个计划是全部崩溃的地方。

    import java.util.*;
    import java.io.*;
    public class InStream {
    
    public static void main(String[] args) {
    int [] NumArray = new int[301];
    int PosNum = 0,
        Total = 0,
        Average = 0,
        Range1 = 0,
        Range2 = 0,
        Range3 = 0,
        Range4 = 0,
        Range5 = 0,
        Range6 = 0;
    
        try{
            File Fout = new File("F:\\Text.txt");
            PosNum = 0;
            Scanner Fin = new Scanner(Fout);
            while(Fin.hasNextInt()){
                NumArray[++PosNum] = Fin.nextInt();
    
    
            }
            Fin.close();
    
           }catch(Exception E){
               E.printStackTrace();
               System.out.println("***ERROR ***** " + E.getMessage());
           } 
    
    
    for(NumArray[PosNum] = 0; NumArray[PosNum] <= 301;){
        Total += NumArray[PosNum++];
    
        if(NumArray[Range1] >= 1 && NumArray[Range1] <= 10){
           Range1++;
        }
    
        if (NumArray[Range2] >= 11 && NumArray[Range2] <= 20){
            Range2++;
        }
        if(NumArray[Range3] >= 21 && NumArray[Range3] <= 30){
           Range3++;
        }
        if(NumArray[Range4] >= 31 && NumArray[Range4] <= 40){
           Range4++;
        }
        if(NumArray[Range5] >= 41 && NumArray[Range5] <= 50){
           Range5++;
        }
        if(NumArray[Range6] >= 51 && NumArray[Range6] <= 54){
           Range6++;
        }  
    
    }
    Average = Total / 301;
    System.out.println("The Average Is: " + Average);
    System.out.println("1 Through 10 appear: " + Range1 + " Times");
    System.out.println(" 11 Through 20 appear: " + Range2 + "Times");
    System.out.println("21 Through 30 appear: " + Range3 + " Times");
    System.out.println("31 Through 40 appear: " + Range4 + " Times");
    System.out.println("41 Through 50 appear: " + Range5 + " Times");
    System.out.println("51 Through 54 appear: " + Range6 + " Times");
     }
    
    } 
    

    这是我最新的尝试。

    import java.util.*;
    import java.io.*;
    public class FixedInstream {
    
    public static void main(String[] args) {
        int [] MyArray = new int[301];
        int PosNum = 0,
            Average,
            Total = 0,
            R1 = 0,
            R2 = 0,
            R3 = 0,
            R4 = 0,
            R5 = 0,
            R6 = 0,
            R7 = 0;
    
        try{
            File Fout = new File("F:\\Text.txt");
            PosNum = 0;
            Scanner Fin = new Scanner(Fout);
            while(Fin.hasNextInt()){
                MyArray[PosNum] = Fin.nextInt();
                Total += MyArray[PosNum];
            }
            Fin.close();
    
        }catch(Exception E){
               E.printStackTrace();
               System.out.println("***ERROR ***** " + E.getMessage());
           } 
        for(MyArray[PosNum] = 0; MyArray[PosNum] <= 300; ++MyArray[PosNum]){
    
    
            if(MyArray[R1] >= 1 && MyArray[R1] <= 10){
                R1++;
            }
    
            if (MyArray[R2] >= 11 && MyArray[R2] <= 20){
                R2++;
            }
            if(MyArray[R3] >= 21 && MyArray[R3] <= 30){
                R3++;
            }
            if(MyArray[R4] >= 31 && MyArray[R4] <= 40){
                R4++;
            }
            if(MyArray[R5] >= 41 && MyArray[R5] <= 50){
                R5++;
            }
            if(MyArray[R6] >= 51 && MyArray[R6] <= 54){
                R6++;
            }  
        }
    
        Average = Total / 301;
        System.out.println("The Average Is: " + Average);
        System.out.println("1 Through 10 appear: " + R1 + " Times");
        System.out.println("11 Through 20 appear: " + R2 + " Times");
        System.out.println("21 Through 30 appear: " + R3 + " Times");
        System.out.println("31 Through 40 appear: " + R4 + " Times");
        System.out.println("41 Through 50 appear: " + R5 + " Times");
        System.out.println("T51 Through 54 appear: " + R6 + " Times");
      }
    
    }
    

    该程序没有正确放置下面的示例我显然忘记在我的初始评论中添加。

    输出:

    The Average Is: 27
    The Numbers 1 Through 10 appear: 1 Times
    The Numbers 11 Through 20 appear: 1 Times
    The Numbers 21 Through 30 appear: 1 Times
    The Numbers 31 Through 40 appear: 1 Times
    The Numbers 41 Through 50 appear: 1 Times
    The Numbers 51 Through 54 appear: 1 Times
    

    文本文件中有300个数字1 - 54这些数字的重复方式不同但通常是10 - 50倍,因为你可以看到它只显示一次,这让我相信我不会增加一些东西正确。

1 个答案:

答案 0 :(得分:2)

我认为这里有很多令人困惑的事情......以下是答案,但我建议你自己尝试调试这个片段以了解问题所在。

  1. 你正在通过一个包含301个单元格的数组。数组索引从0变为length-1。当posNum初始化为0时,当你使用numArray[++posNum]时,第一个索引将是++0 = 1。所以你永远不会到达阵列的第一个单元格。这导致out of range exception,因为最后一个索引将是301,但实际上最后一个索引是300 ...只需阅读异常消息就可以引导您找到解决方案。

  2. 我建议你看一下for loop如何使用NumArray[Range1] 来理解如何逐个单元地使用增量索引浏览数组。然后你会像这样纠正你的for循环:

    for(int idx = 0; idx&lt; NumArray.length; idx ++)

  3. 您正在使用错误的变量访问当前单元格。这可以附加,但你会在调试模式下非常快地检测到它......

    NumArray [IDX]

  4. 而不是

    variables
    1. 这对于使您的程序工作并不重要,但在Java中有一些约定(如在每个语言中)和camelCase都是用public static void main(String[] args) { int [] NumArray = new int[301]; int PosNum = 0, Total = 0, Average = 0, Range1 = 0, Range2 = 0, Range3 = 0, Range4 = 0, Range5 = 0, Range6 = 0; try{ File Fout = new File("F:\\Text.txt"); PosNum = 0; Scanner Fin = new Scanner(Fout); while(Fin.hasNextInt()){ NumArray[PosNum++] = Fin.nextInt(); } Fin.close(); }catch(Exception E){ E.printStackTrace(); System.out.println("***ERROR ***** " + E.getMessage()); } for(int idx = 0; idx < NumArray.length; idx++){ Total += NumArray[idx]; if(NumArray[idx] >= 1 && NumArray[idx] <= 10){ Range1++; } if (NumArray[idx] >= 11 && NumArray[idx] <= 20){ Range2++; } if(NumArray[idx] >= 21 && NumArray[idx] <= 30){ Range3++; } if(NumArray[idx] >= 31 && NumArray[idx] <= 40){ Range4++; } if(NumArray[idx] >= 41 && NumArray[idx] <= 50){ Range5++; } if(NumArray[idx] >= 51 && NumArray[idx] <= 54){ Range6++; } } Average = Total / 301; System.out.println("The Average Is: " + Average); System.out.println(" 1 Through 10 appear: " + Range1 + " Times"); System.out.println("11 Through 20 appear: " + Range2 + " Times"); System.out.println("21 Through 30 appear: " + Range3 + " Times"); System.out.println("31 Through 40 appear: " + Range4 + " Times"); System.out.println("41 Through 50 appear: " + Range5 + " Times"); System.out.println("51 Through 54 appear: " + Range6 + " Times"); } 编写的。

      公共类InStream {

      {{1}}

      }

    2. 不要犹豫,调试你的程序,这是学习语言,享受Java的最好方法