从另一种方法显示Arraylist

时间:2017-01-21 06:11:03

标签: java for-loop arraylist methods indexing

我在这里的问题是,我想在一个单独的文件中输出三个学生分数的lettergrade,格式为:

name1   score1
name2   score2
name3    score3

在分数和名称之间加上标签。

我在这里遇到的问题是,如果你向下滚动到我的toString2方法,我将运行我的程序,它应该取三个分数并根据我的计算输出正确的字母等级。相反,我的程序进入一个不受控制的循环,以[]结束。我该如何解决这个问题?

 package file;

 import java.io.File;
 import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;

 public class ReadData2 
{
ArrayList<String> name = new ArrayList<String>();
ArrayList<Double> score  = new ArrayList<Double>();
ArrayList<String> lettergrade = new ArrayList<String>();
double mean;
double std;

public ReadData2()
{
    mean= 0;
    std=0;
}
public  void readData(File file) throws FileNotFoundException 
{
    //File file = new File("data.txt");
    Scanner scanner = new Scanner(file);
    int l = 0;


    while (scanner.hasNextLine()) { 
        String line = scanner.nextLine();
        String [] words = line.split("\t"); 
        name.add(words[0]); 
        score.add(Double.parseDouble(words[1]));
    }
}

public String toString()
{

    StringBuilder out = new StringBuilder();
    for(String n: name)
    {
        out.append(n);
        out.append("\n");

    }
    for(double n: score)
    {
        out.append(n);
        out.append("\n");   
    }
    return out.toString();      
}   

public double fndMean()
{
    double mean  = 0;
    double sum = 0;
    for(Double n: score)
    {
        sum= sum+n; 
    }
    mean= sum/score.size();
    return mean;
}

public double fndStandard()
{
    double sumOfDifferences = 0;
    double mean = fndMean();
    for(Double n: score)
    {
        sumOfDifferences += Math.pow((n- mean),2);  
    }
    double temp = sumOfDifferences/ score.size();
    return Math.sqrt(temp);
}
public  ArrayList<String>  toString2(File file) throws FileNotFoundException
{
    StringBuilder grades = new StringBuilder();
    double mean1 = fndMean();
    double std1 = fndStandard();

    Scanner scanner = new Scanner(file);
    int l = 0;

    while (scanner.hasNextLine()) { 
        String line = scanner.nextLine();
        String [] words = line.split("\t"); 
        name.add(words[0]); 
        score.add(Double.parseDouble(words[1]));

        for(double n: score)
        {
            grades.append(n);
            grades.append("\n");    
        }


        for(int i = 0; i < ((score.size())) && i < name.size(); i++)
        {
            if(mean1 +std1 < score.get(i))
            {
                System.out.println("A");
            }
            else if( (score.get(i) >= mean1+(std1/3)) &&  (mean1 +std1)> score.get(i))
            {
                System.out.println("B");
            }
            else if( (score.get(i) >= mean1-(std1/3)) &&  (mean1 +(std1/3))> score.get(i))
            {
                System.out.println("C");

            }
            else if( (score.get(i) >= mean1-(std1)) &&  (mean -(std1/3))> score.get(i))
            {
                System.out.println("D");
            }   
            else if(mean1 - std1 > score.get(i))
            {
                System.out.println("F");
            }

        }
    }
    return lettergrade;
}
}

1 个答案:

答案 0 :(得分:1)

  

相反,我的程序进入一个不受控制的循环,以一个结束   []。我该如何解决这个问题?

你得到一个空的[],因为你并没有真正修改你的&#34; lettergrade&#34;阵列。您只是将等级打印到控制台,而不是真正将其添加到阵列。除了sysout语句之外,您还需要将等级添加到数组中。 E.g。

lettergrade.add("B");