【问题标题】:Issue with reading from a file in java [duplicate]从java中读取文件的问题[重复]
【发布时间】:2013-11-20 04:29:15
【问题描述】:

我正在尝试通读文件并以不同的格式创建一些对象/打印信息。

我的大部分内容都是正确的,但是由于某种原因,当它循环时,每次完成一个 if 语句时,我都会得到“字符串索引超出范围:0”(它会在它发生之前打印第一个对象)。

我已经对这个问题进行了一些研究,我认为这是因为下一行再次在位置 0 处寻找一个字符 (ID.charAt(0))。但是,我有另一个以这种方式完成的程序,它运行良好。你们有没有机会看看它并告诉我我可能在哪里搞砸了?

感谢您的帮助。我以前在这里问过几个问题,你总是帮我学习!

代码

import java.io.File;
import java.util.Scanner;
public class PayrollSystemTest2 {

    /**
     * @param args
     */
    public static void main(String[] args) throws Exception {
        // TODO Auto-generated method stub
        Scanner input;
        input =  new Scanner(new File("EmployeePayrollInfo.txt"));
        Employee[] Emp = new Employee[20];

        while(input.hasNext())
        {
            String ID = input.nextLine();

            if (ID.charAt(0) == 'S')
            {
                String first = input.nextLine();
                String last = input.nextLine();
                String ssn = input.nextLine();
                Date DayOfBirth = new Date(input.nextInt(),input.nextInt(),input.nextInt());
                double salary = input.nextDouble();
                Emp[0] = new SalariedEmployee(first, last, ssn, DayOfBirth, ID, salary);
                System.out.println(Emp[0].toString());
            }
            else if (ID.charAt(0) == 'H')
            {
                String first = input.nextLine();
                String last = input.nextLine();
                String ssn = input.nextLine();
                Date DayOfBirth = new Date(input.nextInt(),input.nextInt(),input.nextInt());
                double hourlyWage = input.nextDouble();
                double hoursWorked = input.nextDouble();
                Emp[1] = new HourlyEmployee(first,last,ssn,DayOfBirth,ID,hourlyWage,hoursWorked);
                System.out.println(Emp[1].toString());
            }
            else if (ID.charAt(0) == 'C')
            {
                String first = input.nextLine();
                String last = input.nextLine();
                String ssn = input.nextLine();
                Date DayOfBirth = new Date(input.nextInt(),input.nextInt(),input.nextInt());
                Double sales = input.nextDouble();
                Double rate = input.nextDouble();
                Emp[2] = new CommissionEmployee(first,last,ssn,DayOfBirth,ID,sales,rate);
                System.out.println(Emp[2].toString());
            }
            else if (ID.charAt(0) == 'B')
            {
                String first = input.nextLine();
                String last = input.nextLine();
                String ssn = input.nextLine();
                Date DayOfBirth = new Date(input.nextInt(),input.nextInt(),input.nextInt());
                Double sales = input.nextDouble();
                Double rate = input.nextDouble();
                Double salary = input.nextDouble();
                Emp[3] = new BasePlusCommissionEmployee(first,last,ssn,DayOfBirth,ID,sales,rate,salary);
                System.out.println(Emp[3].toString());
            }
            else if (ID.charAt(0) == 'P')
            {
                String first = input.nextLine();
                String last = input.nextLine();
                String ssn = input.nextLine();
                Date DayOfBirth = new Date(input.nextInt(),input.nextInt(),input.nextInt());
                Double Wage = input.nextDouble();
                Double Pieces = input.nextDouble();
                Emp[4] = new PieceWorker(first,last,ssn,DayOfBirth,ID,Wage,Pieces);
                System.out.println(Emp[4].toString());
            }

        }
        input.close();


    }
}

我正在尝试读取的文件

S100
Sully
Ross
111-11-1111
8 15 1979
900.00
H205
Joe
Aggie
222-22-2222
3 6 1993
15.25
40
C102
Rev
Elee
333-33-3333
11 22 1985
20000
.065
B115
Johnny
Football
444-44-4444
06 28 1965
12000
.05
400
P206
Miss
Bizbee
555-55-5555
11 06 1977
1.25
1000
X

【问题讨论】:

    标签: java string indexing


    【解决方案1】:

    这适用于你所有的块,但基本上当你这样做时......

    double salary = input.nextDouble();
    

    它将换行符留在扫描仪中,因此当您执行下一个String ID = input.nextLine(); 时,它正在读取该行的其余部分,基本上是空白...

    尝试在每个部分的末尾添加input.nextLine();,例如...

    double salary = input.nextDouble();
    input.nextLine();
    

    另外,您的DayOfBirth 是错误的。除了被弃用之外,Date 构造函数还需要year, month, day order 中的值;)

    【讨论】:

    • 非常感谢!现在这更有意义了,一切都完美无缺。我不知道 nextDouble() 留在同一行。再次感谢!
    • @user2774647 你的出生日期是错误的,小心那些。输入似乎是月、日、年
    • 是的,我实际上为此创建了自己的 Date 类,它按月、日、年的顺序排列,因此它实际上在我的端正确输出。很抱歉没有提及!
    • 只要它不会突然吓到你 ;)
    【解决方案2】:

    您输入的第一行可能没有内容,而只是一个空行(换行符)。尝试在第一个 if 语句之前打印出 ID 变量,看看你会得到什么。

    【讨论】:

      【解决方案3】:

      我的原因类似于 MadProgrammer 原因,但不是在每个部分的末尾添加input.nextLine();,我们可以添加

      if(input.hasNext()){

      input.nextLine(); 
      

      }

      在while循环结束时只有一次。

      【讨论】:

        猜你喜欢
        • 2011-11-23
        • 2013-08-29
        • 1970-01-01
        • 1970-01-01
        • 2012-09-12
        • 2017-11-05
        • 2014-07-14
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多