簡體   English   中英

從文本文件中讀取整數並將其放入已排序的數組中

[英]Reading integers from a text file and putting them into a sorted array

我整天都在撓頭,試圖弄清楚該怎么做。 我嘗試了多種不同的方法,但是我覺得我一直在做這件事。 我的任務是從.txt文件中讀取50個整數,然后將內容放入已排序的數組中,然后列出最高/最低/平均數,但我幾乎無法超越第一步。

這是文本文件中的50個數字

64 61 169 113 81 61 206 176 39 100 22 200 128 152 59 165 67 116 165 72 26 149 58 204 188 69 203 94 96 134 83 122 192 85 62 159 35 162 95 92 126 66 66 203 187 18 132 182 181 175

在此文件中,我設法至少要打印“ proj8”文件。

import java.util.Scanner;
import java.io.IOException;
import java.util.ArrayList;
import java.util.*;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.List;
import java.io.*;

public class ect7{

  public static void main(String[] args) throws IOException {

    BufferedReader br = new BufferedReader(new FileReader("proj8.txt"));

    String line = br.readLine();

    while ((line = br.readLine()) != null) {
      System.out.println(line);
    }
    br.close();
  } 
}

在這個文件中,我設法打印了“ proj8”文件並對其進行了某種排序,但如此瘋狂。

import java.util.Scanner;
import java.io.IOException;
import java.util.ArrayList;
import java.util.*;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.List;
import java.io.*;

public class ect73{

public static void main(String[] args) throws IOException {

  //int [] myArr = new int[50];  
  //FileReader fr = new FileReader("proj8.txt");    
  BufferedReader br = new BufferedReader(new FileReader("proj8.txt"));
  List<String> lines = new ArrayList<String>();
  String line = null;

  //String line = br.readLine();

   while ((line = br.readLine()) != null) {
     lines.add(line);
     Collections.sort(lines);
     //System.out.println(line);
     System.out.println(lines);
   }   
   br.close();
   //return lines.toArray(new String[lines.size()]);
  }
}

我知道我做錯了一切,但我不知道如何正確做。 我需要能夠將.txt文件輸入整數形式的排序數組,然后列出最高/最低/平均數。 任何幫助都是好的幫助,但是代碼越簡單越好。

您正在嘗試對文件的行進行排序,該行包含由空格分隔的多個數字。 您必須將每一行拆分為數字,並且對於每個數字,您都必須將其解析為Integer並將其存儲在ArrayList 最后,您可以對ArrayList進行排序。

    BufferedReader br = new BufferedReader(new FileReader("proj8.txt"));
    List<Integer> numbers = new ArrayList<Integer>();
    String line = null;

     //String line = br.readLine();

     while ((line = br.readLine()) != null) {
         String []strNumbers = line.split(" ");
         for(String strNumber : strNumbers){
             numbers.add(Integer.parseInt(strNumber));
         }

         //System.out.println(line);            
     }   
     br.close();

     Collections.sort(numbers);
     System.out.println(numbers);

您的路線正確,但查看文件似乎每行包含一個以上的數字,因此您需要進行處理。 您還需要將它們轉換為數字,否則它們將按字符串排序(這意味着“ 10”在“ 2”之前)。

我不會給您完整的答案,但是您可以使用一些方法/類來解決這個問題:

您不必使用所有這些,但是它們應該能夠幫助您找到解決方案。

您不必使用復雜的方法,只需打開文件即可創建掃描程序對象。 1 for循環從文本文件構造數組1 for循環查找最低/最高值以及所有值的總和。 然后計算平均值,然后輸出所有結果。

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

public class textToArray {

        public static void main(String[] args) throws FileNotFoundException{
        //Open the file 
        File f = new File("C:\\Users\\YOUR COMPUTER\\Desktop\\java.txt");//when the file directory contain \ you need to use escape ex "c:\\user\\desktop"
        //Create a new scanner to read the file
        Scanner s = new Scanner(f);
        //Declare an array with length of 100
        int[] array = new int[100];
        int i = 0, min, max, sum = 0;
        //Loop through the file if there is a next int to process it will continue

        for(i = 0; s.hasNextInt(); i++){
            //We store every int we read in the array
            array[i] = s.nextInt();                             
        }

        //Close the scanner object
        s.close();
        //As the for loop will stop when there is no new int to read so the i++ will stop at the number of int in the file
        //We use this number to calculate the average
        int nbInArray = i;
        //initialize min and max to the first array value which is for now empty
        min = max = array[0];
        for(int j = 0; j<nbInArray ; j++){
            //Now we test if the new value if bigger than the initial value (0 or empty) the new value will become the max
            if (array[j]>max){
                max = array[j];
                }
            //Otherwise if the the value is smaller than the initial value the new value will become the minimum
            //And so on every time we test if we have a smaller value the new one will become the min
            if (array[j]<min){
                min = array[j];
                }
            //The sum was initialised to 0 so we add every new int to it
            sum = sum + array[j];
        }


        //We compute the avg , i declared it double in case we have decimal results
        double avg = sum / nbInArray;
        //finally we print out the min, max and the average
        System.out.println("The minimum is: "+min);
        System.out.println("The maximum is: "+max);
        System.out.println("The averange is: "+avg);

}
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM