Search code examples
javatextdoubleinputmismatchexception

How to read ints and doubles in a .txt, while skipping the text?


I'm very new to java -

I've got a text file with data about employee wages and hours worked per week. I need to figure out how I can read through the data whilst skipping the text headings above each section, and then have the data ready so that I can perform a couple of simple calculations on it.

This is a sample of the input file:

Shop One
4
26 8
13 6.5
17 6
32 7.5

Shop Two
1
42 8

Shop Three
0

The first number is the employee count, followed by each employee's hours and wage.

Here's what I've got so far:

import java.io.*;
import java.util.*;

public class program3 
{
static Scanner console = new Scanner(System.in);    

public static void main(String[] args) throws FileNotFoundException
{
    System.out.println("Please enter your recommended maximum total staff cost per week: ");
    double RM = console.nextDouble();   

    Scanner inFile = new Scanner(new FileReader("inFile.txt")); 

    int shopStaff;

    for (int i = 0; i < (shopStaff = inFile.nextInt()); i++) //number of times loop executes will be equal to number of staff
    {
        double shop1Tot = 0;
        double shop1SH = inFile.nextDouble(); //shop1SH = Shop One Staff Hours
        double shop1SW = inFile.nextDouble(); //shop1SW = Shop One Staff Wage               
        shop1Tot = shop1Tot + (shop1SH * shop1SW); //calculates total staff wages per week
        {
            if (shop1Tot <= RM) {
                System.out.println("details written to outFile"); 
                PrintWriter outFile = new PrintWriter ("outFile.txt");
                outFile.println("details"); }

            else if (shopTot > RM){
                System.out.println("details written to screen only"); }

I guess I need a way to skip over the "Shop One" part of the file and just read the numbers. I cant seem to figure out how, I just keep getting an InputMismatchException. Any help would be greatly appreciated.


Solution

  • Since there doesn't seem any sequence in your records, it can be read based on some assumptions. The following code example might help you to figure out how you can proceed with your implementation.

        // provide physical location of your file, I have assumed it is present
        // in current directory
        FileReader fis = new FileReader("test.txt");
        BufferedReader br = new BufferedReader(fis);
        String line = null;
        // to keep count of shops in the file
        int shopCount = 1;
        // read line by line till you reach end of file
        while ((line = br.readLine()) != null) {
            // assuming each string starts with Shop
            if (line.startsWith("Shop")) {
                System.out.println("Shop " + shopCount);
                // assuming first line after string heading is number of
                // employees
                int numberOfEmployees = Integer.parseInt(br.readLine());
                System.out.println("Number of employees " + numberOfEmployees);
                // for each employee read their wage and salary
                for (int i = 0; i < numberOfEmployees; i++) {
                    // assuming wage and number of hours are separated by a
                    // space
                    // wage is of type int and number of hours is of type double
                    StringTokenizer st = new StringTokenizer(br.readLine());
                    while (st.hasMoreTokens()) {
                        // convert wage to int
                        int wages = Integer.parseInt(st.nextToken());
                        // convert number of hours to double
                        double hours = Double.parseDouble(st.nextToken());
                        System.out
                                .println("Wages " + wages + " Hours " + hours);
    
                    }
                }// end reading details of each employee
                    // increment shop count to read details for next shop
                shopCount++;
            }
        }