Java 对从数据文件导入的数组中的元素进行计数
我正在编写一个程序,将txt文件中的值导入数组,然后我需要计算这些元素中有多少大于或等于36。数据导入很好,它显示的值总量是正确的,但我无法让它显示文件中找到数字36的次数。谢谢你的帮助Java 对从数据文件导入的数组中的元素进行计数,java,arrays,sorting,Java,Arrays,Sorting,我正在编写一个程序,将txt文件中的值导入数组,然后我需要计算这些元素中有多少大于或等于36。数据导入很好,它显示的值总量是正确的,但我无法让它显示文件中找到数字36的次数。谢谢你的帮助 public static void main(String[] args) throws Exception { int[] enrollments = new int [100]; int count; int FullClass;
public static void main(String[] args) throws Exception {
int[] enrollments = new int [100];
int count;
int FullClass;
double ClassPercentage;
return count (number of data items)
count = CreateArray(enrollments);
System.out.println (count );
FullClass = AddValues (enrollments);
System.out.println (FullClass)
ClassPercentage= FullClass/count;
System.out.print(ClassPercentage +"% of classes are full");
}//end main
/**
*
* @param classSizes
*/
public static int CreateArray(int[] classSizes) throws Exception{
int count = 0;
File enrollments = new File("enrollments.txt");
Scanner infile = new Scanner (enrollments);
while (infile.hasNextInt()){
classSizes[count] = infile.nextInt();
count++}//end while
return count; //number of items in an array
} // end CreateArray
/**************************************************************************/
/**
*
* @throws java.lang.Exception
*/
public static int AddValues (int[] enrollments) throws Exception{
{
int number = 0;
int countOf36s = 0;
while (infile.hasNextInt()) {
number = infile.next();
classSizes[count] = number;
if(number>=36) {
countOf36s++;
}
count++;
}
return countOf36s;
}// end AddValues
}//end main
当您仅读取文件时,尝试使用此代码计算大于或等于36的数字。更改
createArray
方法中的代码,或者在需要的地方编写以下逻辑
我试着执行这个程序。它按预期工作。请参阅下面的代码
import java.util.*;
import java.io.*;
public class Test { //Name this to your actual class name
public static void main(String[] args) throws Exception {
int[] enrollments = new int [100]; //assuming not more than 100 numbers in the text file
int count; //count of all the numbers in text file
int FullClass; //count of numbers whose value is >=36
double ClassPercentage;
count = CreateArray(enrollments);
System.out.println (count);
FullClass = AddValues (enrollments);
System.out.println (FullClass);
ClassPercentage= FullClass/count;
System.out.print(ClassPercentage +"% of classes are full");
}
//Method to read all the numbers from the text file and store them in the array
public static int CreateArray(int[] classSizes) throws Exception {
int count = 0;
File enrollments = new File("enrollments.txt"); //path should be correct or else you get an exception.
Scanner infile = new Scanner (enrollments);
while (infile.hasNextInt()) {
classSizes[count] = infile.nextInt();
count++;
}
return count; //number of items in an array
}
//Method to read numbers from the array and store the count of numbers >=36
public static int AddValues (int[] enrollments) throws Exception{
int number = 0;
int countOf36s = 0;
for(int i=0; i<enrollments.length; i++) {
number = enrollments[i];
if(number>=36) {
countOf36s++;
}
}
return countOf36s;
}
}
import java.util.*;
导入java.io.*;
公共类测试{//将其命名为您的实际类名
公共静态void main(字符串[]args)引发异常{
int[]enrollments=new int[100];//假设文本文件中的数字不超过100
int count;//文本文件中所有数字的计数
int FullClass;//值大于等于36的数字计数
双班百分比;
计数=创建数组(注册);
System.out.println(计数);
FullClass=AddValues(注册);
System.out.println(完整类);
ClassPercentage=完整类/计数;
System.out.print(类百分比+%的类已满);
}
//方法从文本文件中读取所有数字并将其存储在数组中
公共静态int CreateArray(int[]类大小)引发异常{
整数计数=0;
File enrollments=新文件(“enrollments.txt”);//路径应正确,否则会出现异常。
扫描仪填充=新扫描仪(登记);
while(infle.hasNextInt()){
classsize[count]=infle.nextInt();
计数++;
}
return count;//数组中的项数
}
//方法从数组中读取数字并存储大于等于36的数字计数
公共静态int AddValues(int[]注册)引发异常{
整数=0;
int countOf36s=0;
对于(int i=0;i=36){
countOf36s++;
}
}
返回计数为36秒;
}
}
您的代码表明您可能误解了一些概念和风格。正如您在评论中所说的,您在这方面是新手,希望得到一些指导以及问题的答案-如下所示:
风格
if else
块时放在单独的行上抛出异常
非常普遍-通常尽可能限制代码实际抛出的异常-在您的情况下抛出FileNotFoundException
应该足够了,因为这是扫描仪
或文件
在运行时可以抛出的异常。这种特殊性对于将来使用任何代码的任何代码都很有用CreateArray
,当文件中有更多整数时,它从文件中读取。您的代码不知道这是多少-我们称之为N
。如果N文件中是否只有数字?您希望所有数字的计数都等于36?如果读取的文件至少有36行,则此if(count>=36)
将为真,因为count
是行数,而不是文件中的特定数字。将其更改为if(注册[i]>=36)
Tom的注释将有效,并且是最简单的修复方法。你可能需要解决一些风格上的问题——你想要一个指向其中一些的答案吗?@Tom谢谢你!我试着把它输入我的代码,得到了同样的答案。我应该把它放在我的AddValues方法中,对吗?@JRichardSnape那太好了,总是想了解更多,我对这个非常陌生。好的,太好了,我应该用这个替换我在方法中已经有的东西吗?此外,是的,该文件仅包含数字。是。这一逻辑运作良好。如果您在执行此操作时遇到任何问题,请告诉我。好的,我替换了addValues方法,但仍然遇到相同的问题,是否可能我调用的方法不正确?谢谢您的帮助,我非常感谢!谢谢@JRichardSnape!不客气-如果有帮助的话,请随意按下向上投票按钮-这是一个很好的感谢:)我试过了,不幸的是,我还没有足够高的代表,但一旦我这样做了,我会向你发送一个。
...
classSizes[count] = number;
if(number>=36) {
...
...
number = classSizes[count];
if(number>=36) {
...
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
public class ClassCounter
{
public static void main(String[] args) throws FileNotFoundException
{
int count;
int fullClass;
double classPercentage;
ArrayList<Integer> enrollments = createArray();
count = enrollments.size();
System.out.println(count);
fullClass = addValues(enrollments);
System.out.println(fullClass);
classPercentage = fullClass / count;
System.out.print(classPercentage + "% of classes are full");
}
/**
* scans file "enrollments.txt", which must contain a list of integers, and
* returns an ArrayList populated with those integers.
*
* @throws FileNotFoundException
*/
public static ArrayList<Integer> createArray() throws FileNotFoundException
{
ArrayList<Integer> listToReturn = new ArrayList<Integer>();
File enrollments = new File("enrollments.txt");
Scanner infile = new Scanner(enrollments);
while (infile.hasNextInt())
{
listToReturn.add(infile.nextInt());
}
return listToReturn;
}
/**
* returns the number of cases where enrollments >= 36 from the list of
* all enrollments
*
* @param enrollments - the list of enrollments in each class
* @throws FileNotFoundException
*/
public static int addValues(ArrayList<Integer> enrollments)
{
int number = 0;
int countOf36s = 0;
int i = 0;
while (i < enrollments.size())
{
number = enrollments.get(i);
if (number >= 36)
{
countOf36s++;
}
}
return countOf36s;
}
}