如何从用户在Java中输入的文件中获取数字列表?

时间:2014-05-26 14:11:45

标签: java input filenames average

我正在使用Java eclipse,我希望用户输入文件名以从文件中检索分数列表。我的目标是取这些数字的平均值。我需要什么代码行才能让用户输入文件名,程序是否可以使用这些代码来计算?目前我可以获得用户输入分数,但我需要从文件中获取数字。我访问了这个网站上的众多资源。以下是一些:

BufferedReaderError finding fileGetting a list from a file

package Average;
/**An average of scores*/
import java.util.Scanner;

public class Average2 {

    public static void main(String[] args) {
        int grade = 0;
        int students = 0;
        float total = 0;
        double average = 0;

        Scanner input = new Scanner(System.in);

        System.out.println("Enter number of students: ");
        students = input.nextInt();

        if (students <= 10) {
            System.out.println("Enter the grades of the students: ");

            for(int i = 0; i < students; i++) {
                do {
                    grade = input.nextInt();

                } while(grade < 0 || grade > 100);

                total += grade;
            }

            average = (total/students);
            int median = ((82+84)/2);

            System.out.println("The average is " + average);
            System.out.println("The mean is " + median);


        }
    }
}

自上面发布后更新!

package trials;

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

public class trials2 {

public static void main(String[] args) throws IOException {
    // TODO Auto-generated method stub
    // Create new Scanner object to read from the keyboard
    Scanner in = new Scanner(System.in);

    // Grab the name of the file
    System.out.println("Enter filename: ");
    String fileName = in.next();

    // Access the file
    Scanner fileToRead = new Scanner(new File(fileName));

    // While there is still stuff in the file...
    double sum = 0;
    while (fileToRead.hasNext()) { 
                if (fileToRead.hasNextDouble()) {
                    sum += fileToRead.nextDouble();
                } else {
                    fileToRead.next();
                }   
            }
   {
            fileToRead.close();
        }

        System.out.println(sum);
}
}

我从中得到的结果:

输入文件名:

esp.txt&lt; - 由我输入

501.0

3 个答案:

答案 0 :(得分:0)

Scanner userInput = new Scanner(System.in);
System.out.println("ENter filename");
String fileName = userInput.next();
Scanner fileScan = new Scanner(new File(fileName));

然后使用scanner方法处理您感兴趣的行或字符串标记。

答案 1 :(得分:0)

查看有关扫描的Java教程:http://docs.oracle.com/javase/tutorial/essential/io/scanning.html,尤其是ScanSum示例。它显示了如何从文本文件中扫描双值并添加它们。您应该能够为您的项目修改此示例。

答案 2 :(得分:0)

鉴于您想要查找文件中数字的平均值,您可以执行以下操作:

// Create new Scanner object to read from the keyboard
Scanner in = new Scanner(System.in);

// Grab the name of the file
System.out.println("Enter filename: ");
String fileName = in.next();

// Access the file
Scanner fileToRead = new Scanner(new File(fileName));

// Initialize our relevant counters
double sum = 0.0;
int numStudents = 0;

// While there is still stuff in the file...
while (fileToRead.hasNext()) { 
    // Is this next line consisting of just a number?
    if (fileToRead.hasNextDouble()) {
        // If it is, accumulate
        sum += fileToRead.nextDouble();
        numStudents++;
     } 
     else { // Else, just skip to the next line
         fileToRead.next();
     }   
}  
// Close the file when finished
fileToRead.close();

// Print the average:
System.out.println("The average mark is: " + (sum / numStudents));

此代码将创建一个新的Scanner对象,该对象将从键盘读取输入。完成后,键入文件名,然后打开另一个Scanner对象即可访问此文件。之后,我们初始化一些计数器以帮助我们计算平均值。这些是sum,它会累计我们遇到的所有标记和numStudents,它会跟踪文件中有多少学生。

while循环将循环遍历此文件的每一行,直到它到达结尾。重要的是,您要检查下一行中读取的行是否包含一个数字(double)。如果是,则将其添加到我们的总和中。如果不是,请跳到下一行。

完成后,关闭文件,然后显示平均值,取总和除以我们在读取文件中的数字时遇到的学生总数。

只是查看了您的个人资料和个人资料信息。你前面有很长的路要走。祝你好运!