简体   繁体   English

Java使用Scanner类从文本文件中读取每行数字,找到最大值

[英]Java read each lines of numbers from text file using Scanner class, finding max value

Hello I'm a java beginner and I'm working on an assignment. 您好,我是Java初学者,正在从事一项作业。 I need to read in numbers from text file, then print out message if they are invalid 我需要从文本文件中读取数字,如果无效则打印出消息

1st number (Int) needs to be 0~99999 第一个数字(整数)必须为0〜99999

2nd number (Int) needs to be 0~5 第2个数字(整数)必须为0〜5

3rd number (double) needs to be 0~100.00 第三位数(双数)必须为0〜100.00

the txt file looks like this, the numbers represent student ID, extra credit, grade
51673 0 98.85 
19438 5 95.00 
00483 3 73.16

and here is what I have so far... any hint is greatly appericiated 这是我到目前为止所拥有的...任何暗示都非常适合

public static void validateDate(File inputFile) 
        x = new Scanner(new File("xxx.txt");
        while(x.hasNext())!= null) {    
        int a = x.next();
        int b = x.next();
        double c = x.next();
)


if (a < 0 || a > 99999){ 
System.out.print("Ignoring student with invalid ID number " + a);
}
if ( b < 0 || b > 5){ 
System.out.println("Ignoring student " + a + " with invalid extra credit" + b);
}
if ( c < 0 || c > 100){
System.out.println("Ignoring student " + a + " with invalid grade " + c);
}

I also need to write another method to find max value of all the 3rd numbers, any suggestions? 我还需要编写另一种方法来查找所有第三个数字的最大值,有什么建议吗?

Read the documentation of Scanner . 阅读Scanner的文档。 Check what functions return int and double and use them instead of next(); 检查哪些函数返回int并加倍,然后使用它们代替next(); . Also for maximum value simply take another variable max=0.0; 同样对于最大值,只需采用另一个变量max = 0.0即可; and check if every c is greater than that. 并检查每个c是否都大于该值。 If yes then assign max with the value of c . 如果是,则为max分配c的值。

Please Note: I could have spoiled by spoonfeeding you but I chose giving hints because you'll learn that way. 请注意:用汤匙喂养我可能会宠坏您,但我选择给出提示,因为您会以这种方式学习

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM