修正错误?从文件中读取

时间:2013-12-05 01:48:32

标签: java

我的输出是:

  

IKE Dinner 25.00 04/15/1993

     

猎人晚餐50.00 DATE1

     

STEVE Lodging 25.00 DATE2

     

ROY Lodging 50.00 DATE3

     

MAX Conference 25.00 DATE4File内容无效。

如何修复错误? 它正在从文件“hotel”读取内容:

  

IKE;晚餐; 25.00; 1993年4月15日;

     

HUNTER;晚餐; 50.00; DATE1;

     

史蒂夫;寄宿; 25.00; DATE2;

     

ROY;寄宿; 50.00; DATE3;

     

MAX;会议; 25.00; DATE4;

预期输出:

  

IKE Dinner 25.00 04/15/1993

     

猎人晚餐50.00 DATE1

     

STEVE Lodging 25.00 DATE2

     

ROY Lodging 50.00 DATE3

     

MAX Conference 25.00 DATE4

     

晚餐:TOTAL会议:TOTAL Lodging:TOTAL

CODE

import java.io.File;
import java.io.FileNotFoundException;

import java.util.NoSuchElementException;
import java.util.Scanner;

public class P7Point16 {
    public static void main(String[] args) {
    double dinner = 0;
    double conference = 0;
    double lodging = 0;
    String name = null, service = null, amount = null, date = null;

    File file = new File(
            "DIRECTORY\\src\\hotel");

    Scanner read = null;
    try {
        read = new Scanner(file);

        read.useDelimiter(";");

        while (read.hasNextLine()) {
            if(read.hasNext()) {
            name = read.next();
            service = read.next();
            amount = read.next();
            date = read.next();

            if (service.equalsIgnoreCase("Lodging")) {
                lodging += Double.parseDouble(amount);
            } else if (service.equalsIgnoreCase("Conference")) {
                conference += Double.parseDouble(amount);
            } else if (service.equalsIgnoreCase("Dinner")) {
                dinner += Double.parseDouble(amount);
            }
            System.out.print(name + " " + service + " " + amount + " "
                    + date);
            }
        }
        System.out.println("");
        System.out.println("Dinner: " + dinner + " Conference: "
                + conference + " Lodging: " + lodging);
    } catch (FileNotFoundException exception) {
        System.out.println("File not found.");
    } catch (NoSuchElementException exception) {
        System.out.println("File contents invalid.");
    } finally {
        read.close();
    }
}
}

工作代码

import java.io.File;
import java.io.FileNotFoundException;

import java.util.NoSuchElementException;
import java.util.Scanner;

public class P7Point16 {
public static void main(String[] args) {
    double dinner = 0;
    double conference = 0;
    double lodging = 0;
    String name = null, service = null, amount = null, date = null;

    File file = new File(
            "DIRECTORY\\src\\hotel");

    Scanner read = null;
    try {
        read = new Scanner(file);

        read.useDelimiter(";");

        while (read.hasNext()) {

            name = read.next();
            service = read.next();
            amount = read.next();
            date = read.next();

            if (service.equalsIgnoreCase("Lodging")) {
                lodging += Double.parseDouble(amount);
            } else if (service.equalsIgnoreCase("Conference")) {
                conference += Double.parseDouble(amount);
            } else if (service.equalsIgnoreCase("Dinner")) {
                dinner += Double.parseDouble(amount);
            }
            System.out.print(name + " " + service + " " + amount + " "
                    + date);
            }
        System.out.println("");
        System.out.println("Dinner: " + dinner + " Conference: "
                + conference + " Lodging: " + lodging);
    } catch (FileNotFoundException exception) {
        System.out.println("File not found.");
    } catch (NoSuchElementException exception) {
        System.out.println("File contents invalid.");
    } finally {
        read.close();
    }
}
}

3 个答案:

答案 0 :(得分:0)

您的文件最后可能有一个空行。当你打电话

name = read.next();

您会收到一个异常,告诉您“此序列中没有更多元素!”

如果要处理文件中的格式错误的行,可以使用以下代码:

    while (read.hasNextLine()) 
    {
    ...
        if(read.hasNext())
        {
            // read a token
        }
        else
        {
            // exit gracefully
        }
    ...
    }

答案 1 :(得分:0)

输入文件的最后一行末尾可能有换行符,因此read.hasNextLine()会在最后一行数据后找到一条额外的(空白)行。

答案 2 :(得分:0)

更改

while (read.hasNextLine()) {

while (read.hasNext()) {