Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6894913
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:50:31+00:00 2026-05-27T06:50:31+00:00

I am trying to write a program for class that reads a list of

  • 0

I am trying to write a program for class that reads a list of hours worked annually from a .txt file, assigns/reads (A little fuzzy on the proper terminology :)) them into an array, and then calculates overages and prints them.

When I run, console output is as follows:

Employee 1 worked 1600 hours, an under-run of 160 or 0%
Employee 3 worked 1680 hours, an under-run of 80 or 0%

Why are the percentages zero?

import java.io.*;
import javax.swing.*;

class Calculator 
{
 public static void main(String args[])
  {

  try{

      String filePathString=JOptionPane.showInputDialog(null,"What is the file path?");  

      FileInputStream fstream = new FileInputStream("C:\\hello.txt");
      BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
      String strLine;
      int counter = 0;
      long[] Array = new long[5];   


      //Read File Line By Line
      while (counter <= 4)

        {
            strLine = br.readLine();
            Array[counter] = Long.parseLong(strLine); 
            OverageUnderage(Array[counter], counter);
            counter = counter + 1;
        }

  //Close the input stream
  in.close();

    }

  catch (Exception e){//Catch exception if any
  System.err.println("Error: " + e.getMessage());
  }
  }

 public static long OverageUnderage(long hours, long counter)
 {
 long overageAmount = 0;
 long overagePercent = 0;

     if (hours < 1760)
     {    
        overageAmount = 1760 - hours;
        overagePercent = ((overageAmount/1760)*100);

        System.out.println("Employee "+counter+" worked "+hours+" hours, an under-run of "+overageAmount+" or "+overagePercent+"%");
    }

    return counter;
 }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T06:50:31+00:00Added an answer on May 27, 2026 at 6:50 am

    The number ( overageAmount / 1760 ) is being treated as a long, not a double, which you’ll need. Try:

    overagePercent = ( ( overageAmount / 1760.0 ) * 100 );
    

    using the .0 on 1760 will have it treated as a decmial value. This will prevent the rounding to 0 issue you were seeing, and allow the calculation to have decimals for the rest of the calculations, providing you with your desired result.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a little C# program that reads from a text file
I'm trying to write a program that reads text from external file (string string
I'm trying to write a program that reads 2 numbers from the user and
I'm trying to write a program that reads 2 numbers from the user and
I'm trying to write a program that prints all numbers from 0 to 1,000
I am trying to write a program that uses file I/O to score personality
I'm trying to write a program that accepts type comparable into the DataSet class
I'm trying to write a program to process the BSD-style process accounting file under
I'm trying to write a program that uses sockets to connect with other instances
I'm trying to write a program in C (on Linux) that loops until the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.