字符串无法转换为double aaaaa

时间:2017-12-06 00:26:07

标签: java string

所以我试图读取txt文件中的一行(我工作了)但是 当我试图找到总数时,它会给我一个错误,说'&34;总不能 转换为双倍"虽然我已经看过应该在这方面起作用的例子 格式

编辑:我添加了Double.parseDouble(输入),但现在我得到"线程中的异常" main" java.lang.NumberFormatException:"

import java.util.Scanner;
import java.io.*;
import java.text.DecimalFormat;
import java.util.ArrayList;



public class AAAAA {
   public static void main (String[] args)throws IOException {

    final String fileName = "classQuizzes.txt";
 //1)
    Scanner sc = new Scanner(new File(fileName));

    String input;
    double total = 0;
    double num = 0;
    double count = 0;
    double average = 0;


//2) process rows    
          while (sc.hasNext()) {
             input = sc.nextLine();
            System.out.println(input);

   //format 2 decimal points
            DecimalFormat form = new DecimalFormat("#0.##");



   //find total
            total += Double.parseDouble(input);  //compile error on using input
            count++; 

            System.out.println(count); //test delete later
   //find average (decimal 2 points)
            System.out.println("hi");
            average = (double)total / count;
            System.out.println("Average = " + average);

//3) class statistics


         }
         System.out.println("Program created by MYNAME");
     }
}

1 个答案:

答案 0 :(得分:0)

该行

total += input;

基本相同
total = String.valueOf(total) + input;

您不能将total,double设置为结果字符串。用

替换该行
total += Double.parseDouble(input);