【问题标题】:How to use split() and read the first character of a string of a file?如何使用 split() 并读取文件字符串的第一个字符?
【发布时间】:2017-07-20 00:14:21
【问题描述】:

比如我想把这个文件的逗号分开,读取每一行的第一个字符。基于第一个字符(#,*,@),我想使用每个字符之后的数据创建一个对象(Steve Davis 2000)。

4     
#Steve,Davis,2000
*John,Kanet,800,7000,0.10
@Thomas,Hill,20,50
*Lisa,Green,800,6000,0.10

4 用作我的数组的大小

到目前为止,这是我的代码:

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

public class PayRoll3{
public static void main(String[] args) throws FileNotFoundException{

    Scanner input=new Scanner(new File(args[0]));  
    int size=input.nextInt();
    input.nextLine();

    Employee[] employees = new Employee[size];
    int index = 0;

    while(input.hasNext()){
        String tmp= input.next();
        String[] commas = tmp.split(",");

        if(tmp.substring(0,1).equals("#")){
        employees[index++]=new Manager2(input.next(), input.next(), input.nextDouble() );
        }
        else if(tmp.substring(0,1).equals("*")){
        employees[index++]=new CommissionEmployee2(input.next(), input.next(), input.nextDouble(), input.nextDouble(), input.nextDouble());
        }
        else if(tmp.substring(0,1).equals("@")){
                employees[index++]=new HourlyWorker2(input.next(), input.next(), input.nextDouble(), input.nextDouble()); 
        }
         }

      input.close();

      for ( Employee currentEmployee : employees ){
          System.out.println( currentEmployee );
          }               
}

当我运行我得到的代码时

 Exception in thread "main" java.util.InputMismatchException 
 at java.util.Scanner.throwFor(Unknown Source) 
 at java.util.Scanner.next(Unknown Source) 
 at java.util.Scanner.nextDouble(Unknown Source) 
 at PayRoll3.main(PayRoll3.java:19) 

【问题讨论】:

  • 欢迎来到本站!这是一个很好的问题的一个很好的开始——你有一个问题的描述,你也展示了你试图解决它的代码。缺少的一件事是对运行代码时发生的情况的描述。点击edit,并在您的问题底部添加此描述。
  • 能否粘贴堆栈跟踪以便我们定位错误?

标签: java arrays file polymorphism


【解决方案1】:

改用charAt() 获取字符串的第一个字符来决定要创建哪个对象。

使用nextLine() 代替next() 得到整行,然后使用split 方法用逗号分割整行。

使用substring(1) 去掉第一个字符。这将假定您希望从第二个索引(即 1)开始到字符串末尾的整个字符串逐个字符地为您提供您想要的字符串,而不需要它的第一个字符。

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

public class PayRoll3{
public static void main(String[] args) throws FileNotFoundException{

    Scanner input=new Scanner(new File(args[0]));  
    int size=input.nextInt();
    input.nextLine();

    Employee[] employees = new Employee[size];
    int index = 0;

    while(input.hasNext()){
        String tmp= input.nextLine();
        String[] commas = tmp.split(",");

        if(commas[0].charAt(0) == '#'){
            employes[index++] = new Manager2(commas[0].substring(1), commas[1], Double.parseDouble(commas[2]));
        }
        else if (commas[0].charAt(0) == '*'){
            employes[index++] = new ComissionEmployee2(commas[0].substring(1), commas[1], Double.parseDouble(commas[2]), Double.parseDouble(commas[3]), Double.parseDouble(commas[4]));
        }
        else if (commas[0].charAt(0) == '@'){
        employes[index++] = new HourlyWorkey2(commas[0].substring(1), commas[1], Double.parseDouble(commas[2]), Double.parseDouble(commas[3]));
        }

    }

    input.close();

    for (Employee currentEmployee : employees ){
        System.out.println( currentEmployee );
    }

【讨论】:

  • 这个有点小问题。 commas[0] 仍将包含构造函数字符(#*@)。我假设员工姓名实际上不是#John#Mark - 就像拥有一个标签名字一样酷。
  • 我已经进行了更改。
  • str.substring(1) 等价于str.substring(1, str.length())。但除此之外,太棒了。
  • 我仍然收到错误消息。线程“main”中的异常 java.lang.ArrayIndexOutOfBoundsException: 3 at PayRoll3.main(PayRoll3.java:19)
  • 现在可以试试吗?在第一个 if 语句中,我写了 Double.parseDouble(commas[3]) 这是错误的,因为下一个索引是 2 ,所以我像这样编辑了那篇文章 Double.parseDouble(commas[2])
猜你喜欢
  • 2018-06-29
  • 2015-06-29
  • 2017-09-13
  • 1970-01-01
  • 2011-03-26
  • 2015-07-02
  • 1970-01-01
相关资源
最近更新 更多