扫描仪没有读取最后一个号码

时间:2013-11-24 08:07:12

标签: java arrays file split java.util.scanner

如果程序混乱或难以理解,请原谅我,我是急着做的。无论如何,我有这个程序应该得到一组数字的均值,中位数和模式。出于某种原因,程序会读取除每行中最后一个数字之外的所有数字 示例输入:

3
1 2 3 3                       //doesn't read the last number "3"
10 10 10 10 10 10 10 10 10 10 //doesn't read the last number "10"
2 3 4 5 5 9 8 7 5 5 3 2 1     //doesn't read the last number "1"

示例输出:

mean median mode
 2.3    2.5  3.0
10.0   10.0 10.0
 4.5    5.0  5.0

    public class Mean     
{    
    public static void main(String[] args) throws IOException    
    {    
        out.print("\f");        
        Scanner scan = new Scanner (new File ("mean.dat"));    
        DecimalFormat deci = new DecimalFormat("##.0");    
        String x = scan.nextLine();    
        double mean = 0, median = 0, mode = 0;    
        String numbers;    
        String[] nums;    
        double[] num;    
        double sum = 0;    
        int y;     
        double mid;    
        int count2;    
        int HighestCount = 0;    
        int p;    
        out.println("mean median mode");    
        while (scan.hasNext())     
        {    
            mean = 0.0;    
            median = 0.0;    
            mode = 0.0;    
            HighestCount = 0;    
            sum = 0.0;    
            numbers = scan.nextLine();    
            nums = numbers.split("[ ]");    
            double l = nums.length;    
            num = new double[(int)l];    
            for (p = 0; p < l - 1; p++)    
            {    
                num[p] = Double.parseDouble(nums[p]);    
                sum += num[p];    
            }    
            mean = (sum / l);    
            Arrays.sort(num);    
            mid = l/2.0;    
            mid = Math.round(mid);    
            if (l%2 == 1)     
            {    
                median = (num[(int)mid]);    
            }     
            else     
            {    
                median = ((num[((int)mid) + 1] + num[((int)mid)]) / 2.0);    
            }    

        for (int i = 0; i < l; ++i)     
        {     
            count2 = 0;     
            for (int j = i; j < l; ++j)       
            {     
                double z = num[i];     
                double zz = num[j];    
                int d = (int)z;    
                int f = (int)zz;     
                if (d == f)      
                    ++count2;     
            }  
            if (count2 > HighestCount)     
            {
                HighestCount = count2;
                mode = num[i];
            }
            else if (count2 == HighestCount)
            {
               mode = num[i];
            }
        }
        out.printf("%4.1f \t %4.1f \t %4.1f", mean, median, mode);
        out.println();
    }
}
}

1 个答案:

答案 0 :(得分:2)

  

由于某种原因,该程序会读取除最后一个之外的所有数字   在每一行。

更改for循环
for (p = 0; p < l - 1; p++)  {// It skip last one    
  num[p] = Double.parseDouble(nums[p]);
  ..............
 }   

 for (p = 0; p < l; p++)  { //Use l insted of l-1
  num[p] = Double.parseDouble(nums[p]);
  ..............
 }