简体   繁体   English

从文件中读取数据以在程序运行时进行计算

[英]reading data from file to calculate it when the program runs

this code should be calculating the progressive tax by reading the brackets and the tax rates from a file, but it shows an mismatch input error. 该代码应通过从文件中读取括号和税率来计算累进税,但它显示输入不匹配错误。

EDIT: Example that can be compiled and run (but still broken) 编辑:可以编译和运行的示例(但仍然损坏)

import  java.io.FileNotFoundException;
import  java.io.FileReader;
import  java.io.IOException;
import  java.util.Scanner;

/**
   <P>{@code java CalculateProgressiveXmpl R:\\jeffy\\programming\\sandbox\\xbnjava\\xbn\\z\\xmpl\\lang\\Taxes.txt}</P>
 **/
public class CalculateProgressiveXmpl  {
   private static double baseSalary = 0.0;
   private static double tax = 0.0;
   public static final void main(String[] as_1RqdPathToInput)  {
      try  {
         calculateProgressive(as_1RqdPathToInput[0]);
      }  catch(RuntimeException rtx)  {
         if(as_1RqdPathToInput.length == 0)  {
            throw  new IllegalArgumentException("One required parameter missing: Path to Taxes.txt");
         }
         throw  rtx;
      }  catch(IOException iox)  {
         throw  new RuntimeException(iox);
      }
   }
   public static final void calculateProgressive(String s_inputFile) throws FileNotFoundException,
           IOException {
       try {
           Scanner readtax = new Scanner(new FileReader(s_inputFile));
           String taxType = readtax.next();
           String brackets = readtax.next();
           int ammount = readtax.nextInt() - 1;
           double tax1[] = new double[ammount + 1];
           while (readtax.hasNext()) {
               for (int i = 0; i <= ammount - 1; i++) {
                   double bottombracket = readtax.nextDouble();
                   double topbracket = readtax.nextDouble();
                   double ptax = readtax.nextDouble();
                   if ((bottombracket <= baseSalary)
                           && (baseSalary >= topbracket)) {
                       tax1[i] = (topbracket - bottombracket) * ptax;
                   } else if ((baseSalary >= bottombracket)
                           && (baseSalary <= topbracket)) {
                       tax1[i] = (baseSalary - bottombracket) * ptax;
                   } else {
                       if (i == ammount - 1) {
                           double fbottombracket = readtax.nextDouble();
                           double fptax = readtax.nextDouble();
                           tax1[i] = (baseSalary - fbottombracket) * ptax;
                       }
                   }
                   tax = tax + tax1[i];
               }
           }
       } catch (FileNotFoundException e) {
           e.printStackTrace();
       }
   }  
}

Stack trace: 堆栈跟踪:

Exception in thread "main" java.util.InputMismatchException at java.util.Scanner.throwFor(Unknown Source) at java.util.Scanner.next(Unknown Source) at java.util.Scanner.nextInt(Unknown Source) at java.util.Scanner.nextInt(Unknown Source) at CalculateProgressiveXmpl.calculateProgressive(CalculateProgressiveXmpl.java:30) at CalculateProgressiveXmpl.main(CalculateProgressiveXmpl.java:14)

Line in question: calculateProgressive(as_1RqdPathToInput[0]); 有问题的行: calculateProgressive(as_1RqdPathToInput[0]);

any suggestion on how to fix it? 关于如何解决它的任何建议?

If this is your data: 如果这是您的数据:

 "Progressive Tax Ammount of brackets: 2 0.0 30000.0 0.0 45001.0 45000.0 0.2 " 

Then String taxType = readtax.next(); 然后String taxType = readtax.next(); will be "Progressive" and String brackets = readtax.next(); 将是“ Progressive”,并且String brackets = readtax.next(); will be "Tax". 将是“税”。 You should see the issue from there :) 您应该从那里看到问题:)

Stepping through the code with a debugger should make it obvious also. 用调试器单步执行代码也应该很明显。

readtax.nextInt() is called early on when it should actually be next() since Ammount fails the pattern match for integer. readtax.nextInt()在实际上应为next()早期就被调用,因为Ammount失败了整数的模式匹配。

Try : "Progressive Tax 2 0.0 30000.0 0.0 45001.0 45000.0 0.2" 尝试: "Progressive Tax 2 0.0 30000.0 0.0 45001.0 45000.0 0.2"

I found two different reasons for InputMismatchException when testing your code: 测试您的代码时,我发现InputMismatchException的两个不同原因:

1) As already pointed out by others you have too many leading strings before the first int. 1)正如其他人已经指出的那样,在第一个int之前您有太多的前导字符串。 So change your input to have only two as expected. 因此,将您的输入更改为只有两个像预期的那样。

2) In my Locale I have , (comma) instead of . 2)在我的语言环境中,我用,而不是。 (dot) for decimal values, so Scanner is using "\\\\," as decimalSeparator and it fails with InputMismatchException on nextDouble when parsing your input. (点)表示十进制值,因此Scanner使用“ \\\\”作为decimalSeparator,并且在解析输入时会在nextDouble上出现InputMismatchException失败。 I suggest you check your locale too to verify your settings. 我建议您也检查语言环境以验证设置。

Note: Once I fixed these points the code still fails (with java.util.NoSuchElementException this time) so I think you will have to rework your algorithm anyway once you get rid of InputMismatchException. 注意:一旦我解决了这些问题,代码仍然会失败(这次是java.util.NoSuchElementException),所以我认为一旦摆脱InputMismatchException,您仍然必须重新编写算法。

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

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