简体   繁体   中英

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]);

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(); will be "Progressive" and 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.

Try : "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:

1) As already pointed out by others you have too many leading strings before the first int. So change your input to have only two as expected.

2) In my Locale I have , (comma) instead of . (dot) for decimal values, so Scanner is using "\\\\," as decimalSeparator and it fails with InputMismatchException on nextDouble when parsing your input. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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