Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从文本文件中读取整数并将其放入排序数组_Java_Sorting_File Io - Fatal编程技术网

Java 从文本文件中读取整数并将其放入排序数组

Java 从文本文件中读取整数并将其放入排序数组,java,sorting,file-io,Java,Sorting,File Io,我整天都在琢磨这个问题,想知道怎么做。我尝试了多种不同的方法,但我觉得我一直在做这一切都错了。我的任务是从一个.txt文件中读取50个整数,并将内容放入一个排序数组,然后列出最高/最低/平均数,但我很难通过第一步 这些是文本文件中的50个数字 六十四 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

我整天都在琢磨这个问题,想知道怎么做。我尝试了多种不同的方法,但我觉得我一直在做这一切都错了。我的任务是从一个.txt文件中读取50个整数,并将内容放入一个排序数组,然后列出最高/最低/平均数,但我很难通过第一步

这些是文本文件中的50个数字

六十四 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()]);
  }
}
import java.util.Scanner;
导入java.io.IOException;
导入java.util.ArrayList;
导入java.util.*;
导入java.io.BufferedReader;
导入java.io.FileReader;
导入java.util.List;
导入java.io.*;
公开课{
公共静态void main(字符串[]args)引发IOException{
//int[]myArr=新int[50];
//FileReader fr=新的FileReader(“proj8.txt”);
BufferedReader br=新的BufferedReader(新文件读取器(“proj8.txt”);
列表行=新的ArrayList();
字符串行=null;
//String line=br.readLine();
而((line=br.readLine())!=null){
行。添加(行);
集合。排序(行);
//系统输出打印项次(行);
系统输出打印项次(行);
}   
br.close();
//返回lines.toArray(新字符串[lines.size()]);
}
}

我知道我做错了,但我不知道怎么做才对。我需要能够将.txt文件输入到整数排序数组中,然后列出最高/最低/平均数。任何帮助都是很好的帮助,但代码越简单越好。

您正在尝试对文件中包含多行以空格分隔的数字进行排序。您必须将每行拆分为数字,对于每个数字,您必须将其解析为
整数
,并将其存储在
数组列表中。最后,您可以对
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);
BufferedReader br=newbufferedreader(newfilereader(“proj8.txt”);
列表编号=新的ArrayList();
字符串行=null;
//String line=br.readLine();
而((line=br.readLine())!=null){
字符串[]strNumbers=line.split(“”);
for(字符串strNumber:strNumber){
add(Integer.parseInt(strNumber));
}
//系统输出打印项次(行);
}   
br.close();
集合。排序(编号);
系统输出打印项次(数字);

您正在尝试对文件中包含多个以空格分隔的数字的行进行排序。您必须将每行拆分为数字,对于每个数字,您必须将其解析为
整数
,并将其存储在
数组列表中。最后,您可以对
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);
BufferedReader br=newbufferedreader(newfilereader(“proj8.txt”);
列表编号=新的ArrayList();
字符串行=null;
//String line=br.readLine();
而((line=br.readLine())!=null){
字符串[]strNumbers=line.split(“”);
for(字符串strNumber:strNumber){
add(Integer.parseInt(strNumber));
}
//系统输出打印项次(行);
}   
br.close();
集合。排序(编号);
系统输出打印项次(数字);

您的思路是正确的,但查看您的文件,它似乎每行包含多个数字,因此您需要处理此问题。您还需要将它们转换为数字,否则它们将被排序为字符串(这意味着“10”在“2”之前)

我不会给出全部答案,但这里有一些方法/类可以用来解决这个问题:


您不必使用所有这些,但它们应该能够帮助您找到解决方案。

您的思路是正确的,但查看您的文件,它似乎每行包含多个数字,因此您需要处理这些问题。您还需要将它们转换为数字,否则它们将被排序为字符串(这意味着“10”在“2”之前)

我不会给出全部答案,但这里有一些方法/类可以用来解决这个问题:


您不必使用所有这些方法,但它们应该能够帮助您找到解决方案。

您不必使用复杂的方法,只需打开文件即可 创建扫描仪对象。 1 for循环从文本文件构造数组 1用于循环查找最低/最高值以及所有值的总和。 然后计算平均值并输出所有结果

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

}
}
导入java.io.File;
导入java.io.FileNotFoundException;
导入java.util.Scanner;
公共类textToArray{
公共静态void main(字符串[]args)引发FileNotFoundException{
//打开文件
文件f=新文件(“C:\\Users\\YOUR COMPUTER\\Desktop\\java.txt”);//当文件目录包含\时,需要使用escape ex“C:\\user\\Desktop”
//创建新的扫描仪以读取文件
扫描器s=新扫描器(f);
//声明长度为100的数组
int[]数组=新的int[100];
int i=0,最小值,最大值,和=0;
//如果有下一个int要处理,则循环遍历该文件。它将继续
对于(i=0;s.hasNextInt();i++){
//我们在数组中存储读取的每个int
数组[i]=s.nextInt();
}
//关闭扫描仪对象
s、 close();
//由于for循环将在没有新的int可读取时停止,因此i++将在文件中的int数处停止
//我们使用