0

我对java很陌生-

我有一个文本文件,其中包含有关员工工资和每周工作时间的数据。我需要弄清楚如何在跳过每个部分上方的文本标题的同时阅读数据,然后准备好数据,以便我可以对其执行一些简单的计算。

这是输入文件的示例:

Shop One
4
26 8
13 6.5
17 6
32 7.5

Shop Two
1
42 8

Shop Three
0

第一个数字是员工人数,然后是每个员工的工时和工资。

这是我到目前为止所得到的:

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"); }

我想我需要一种方法来跳过文件的“Shop One”部分并阅读数字。我似乎无法弄清楚如何,我只是不断收到 InputMismatchException。任何帮助将不胜感激。

4

1 回答 1

1

由于您的记录中似乎没有任何序列,因此可以根据一些假设来读取它。以下代码示例可能会帮助您弄清楚如何继续您的实施。

    // 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++;
        }
    }
于 2013-11-28T18:19:46.523 回答