Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/361.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_Input_Filenames_Average - Fatal编程技术网

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

如何从用户在Java中输入的文件中获取数字列表?,java,input,filenames,average,Java,Input,Filenames,Average,我使用的是JavaEclipse,我希望用户输入文件名以从文件中检索分数列表。我的目标是取这些数字的平均值。我需要哪行代码才能让用户输入一个文件名,让程序获取这些数字,以便我可以用它们进行计算?目前,我可以拥有用户输入的分数,但我需要从文件中获取数字。我访问了这个网站上的许多资源。以下是一些: , 我从中得到的结果是: 输入文件名: esp.txt 然后使用scanner方法来处理您感兴趣的行或字符串标记。请查看有关扫描的Java教程,尤其是ScanSum示例。它显示了如何从文本文件中扫描双值并

我使用的是JavaEclipse,我希望用户输入文件名以从文件中检索分数列表。我的目标是取这些数字的平均值。我需要哪行代码才能让用户输入一个文件名,让程序获取这些数字,以便我可以用它们进行计算?目前,我可以拥有用户输入的分数,但我需要从文件中获取数字。我访问了这个网站上的许多资源。以下是一些:

,

我从中得到的结果是:

输入文件名:

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

请查看有关扫描的Java教程,尤其是ScanSum示例。它显示了如何从文本文件中扫描双值并添加它们。您应该能够为您的项目修改此示例。

如果您希望查找文件中数字的平均值,可以执行以下操作:

// 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));
此代码将创建一个新的
扫描仪
对象,用于从键盘读取输入。一旦它这样做了,你输入文件名,它就可以通过打开另一个
扫描器
对象来访问这个文件。之后,我们初始化一些计数器以帮助我们计算平均值。这些是
sum
,将我们遇到的所有分数相加,以及
numStudents
,用于跟踪文件中有多少学生

while
循环将继续循环此文件的每一行,直到到达末尾。重要的是检查下一行是否由单个数字组成(
double
)。如果是的话,那么把它加到我们的总数上。如果不是,请跳到下一行

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


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

他们将在哪里输入?进入控制台?这看起来像是家庭作业。我对Java完全陌生。我现在正在网上学习一门关于它的课程,并且没有相关背景知识:-/我已经尝试了以下代码:Scanner in=new Scanner(System.in);System.out.println(“文件名是什么?”);字符串输入=in.nextLine();它允许我获取用户输入,但我不知道如何让它从那里读取。while(in.hasNext()){System.out.println(in.next());}您需要设置另一个计数器,计算您读取数字的次数。完成后,用这个数字除以你的总数。
后双和=0,位置
int numStudents=0
if(filetoRead…
语句中,放置
numStudents++。最后,您的最后一个
System.out.println()
语句执行以下操作:
System.out.println(sum/numStudents)感谢您的帮助和理解@rayryeng:)我尝试了上面的代码,但它找不到有问题的文件。我也尝试过使用这个解决方案(),它得到了这个文件,但是,它有很多文本不在文件中(文本大小信息、文本样式等),即使当我打开文件时,它看起来很正常,只列出了一些数字。我已经尝试使用整个文件路径来定位文件以及“esp.txt”。上面的代码假设文件位于与调用程序相同的位置。这可能就是它不起作用的原因,我可能应该在我的帖子中提出一个小小的警告。你能把我链接到这个文件的位置吗?我想亲自看看。我的直觉是你读错了文件。我相信我把它和程序放在了同一个位置。我希望这是您需要我提供的信息…/Users/stephaniegross/Documents/workspace/Exam Statistics Program/src/AverageOh,我最终通过下载textwrangler(mac)并删除我在使用TextEdit时看不到的内容删除了所有显示的格式数据。我想查看实际文本文件本身的内容。如果这个程序可以运行,那么每一行应该只有一个数字。谢谢@newbiee。我尝试了上面的代码,看看我是否可以清除一些不必要的文本,这些文本是在我获取文件以导入数字后出现的,但它在红色的“new Scanner(new file(fileName));”下加了下划线,并告诉我需要添加抛出声明或用try/catch括起来。我很困惑。我问我的教授我是否应该放弃上课并做介绍,但他向我保证我会没事的……现在放弃已经太晚了,我需要尽快学习所有这些:(
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);
}
}
Scanner userInput = new Scanner(System.in);
System.out.println("ENter filename");
String fileName = userInput.next();
Scanner fileScan = new Scanner(new File(fileName));
// 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));