Error in java code

/***************************************************************
*Filename: Burs.java
*@author : Emmanuel Ayotunde 202100475
*Program to compile : javac Burs.java
*Program to execute : java Burs
*Input is reads from the keyboard */

//Import packages required for the program
import java.util.Scanner;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
///////////////////////////////

public class Burs
{
    public static void main(String[] args) throws IOException
    {
        Scanner kb = new Scanner(System.in);              //To read from the keyboard

        //Prompts below this code//////////////////////////////////////////////////////////////////
        System.out.print("Enter file input:");                      //ask the user to enter filename
        String filename = kb.next();                                  //reads the input

        System.out.print("Enter file output:");                    //asks the user to enter output file
        String outFile = kb.next();                                 //reads the output filename
        ///////////////////////////////////////////////////////////////////////////////////

        //Scanners and PrintWriter class
        Scanner inFile = new Scanner(new FileReader(filename));               //reads from the filename
        PrintWriter pw = new PrintWriter("outFile");                           //Print data onto outFile file

        //Variables for later use below line of code////////////////////////////////
        String fName = "";
        String fromContains = "";
        double fromSalary = 0.0;
        String fromNumber = "";
        int N = 0;
        int count = 0;
        double tax = 0.0;
        //////////////////////////////////////////////////////////////////////

        //Reading file contents
        if(inFile.hasNext())
        {

        }
        while(inFile.hasNextLine())                               //booleans to check if file has data
        {
            String contains = inFile.nextLine();                   //Reads the non-primitive data types
            double salary = inFile.nextDouble();                    //Reads the salaries
            String number = inFile.next();                         //reads employee ID number
            count++;

            //Compare and exchange
            if(salary > fromSalary)
            {
                fromContains = contains;                              
                fromSalary = salary;
                fromNumber = number;
            }
        //Conditions for different intervals   
        if(fromSalary >= 1 && fromSalary <= 226000)
        {
            tax = fromSalary * 18 / 100;
        }
         
        else if(fromSalary >= 226001 && fromSalary <= 353100)
        {
            tax = (fromSalary * 26 / 100) + 40680;
        }

        else if(fromSalary >= 353101 && fromSalary <= 488700)
        {
            tax = (fromSalary * 31 / 100) + 73726;
        }

        else if(fromSalary >= 488701 && fromSalary <= 641400)
        {
            tax = (fromSalary * 36 / 100) + 115672;
        }

        else if(fromSalary >= 641401)
        {
            tax = (fromSalary * 39) + 170734;
        }

        

        pw.printf("%s  %d  %.2f  %.2f",fromContains,fromNumber,fromSalary,tax);        //Prints onto new file

        }
        kb.close();
        inFile.close();
        pw.close();
    }
}

eror

How do I fix the issue?

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

I’m not a Java guy, but…

That is a problem with a scanner, right? Can you use log statements to isolate what line it is on? If it on the while condition, which iteration? What is the state of the scanner? What about that if before the while? What is that meant to do? Was that a safety check?

The program is supposed to read content from another file ,the employee name, last name, salary and tax.The program should later print the output to another file with sum of tax displayed at the bottom.

OK, but I was asking what steps you’ve taken to diagnose the problem.

A good coder is a good debugger. And a good debugger is a good detective. Get out your magnifying glass, deerstalker cap, and calabash pipe and look for clues. Where exactly in the code is it happening? Does it ever read from the file? How far does it get? What are the exact conditions when it fails?

I feel the issue is at the lexcographing, when file input is being read.

OK, test that theory, get data.

Whenever I have a bug I start logging data and try to find out where what I expect to happen starts doing something else. If you can pinpoint the line, you get that much closer. If you can figure out the exact conditions, you are close. Do some detective work.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.