Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用Scanner类时如何忽略.txt的第一行_Java_Java.util.scanner - Fatal编程技术网

Java 使用Scanner类时如何忽略.txt的第一行

Java 使用Scanner类时如何忽略.txt的第一行,java,java.util.scanner,Java,Java.util.scanner,我有一个文本文件,内容如下: Description|SKU|Retail Price|Discount Tassimo T46 Home Brewing System|43-0439-6|17999|0.30 Moto Precise Fit Rear Wiper Blade|0210919|799|0.0 我得到了它,所以我可以读取所有内容,它工作得非常完美,除了它读取第一行,这是一种.txt文件的图例,必须忽略它 public static List<Item> read(F

我有一个文本文件,内容如下:

Description|SKU|Retail Price|Discount
Tassimo T46 Home Brewing System|43-0439-6|17999|0.30
Moto Precise Fit Rear Wiper Blade|0210919|799|0.0
我得到了它,所以我可以读取所有内容,它工作得非常完美,除了它读取第一行,这是一种.txt文件的图例,必须忽略它

public static List<Item> read(File file) throws ApplicationException {
    Scanner scanner = null;
    try {
        scanner = new Scanner(file);
    } catch (FileNotFoundException e) {
        throw new ApplicationException(e);
    }

    List<Item> items = new ArrayList<Item>();

    try {
        while (scanner.hasNext()) {
            String row = scanner.nextLine();
            String[] elements = row.split("\\|");
            if (elements.length != 4) {
                throw new ApplicationException(String.format(
                        "Expected 4 elements but got %d", elements.length));
            }
            try {
                items.add(new Item(elements[0], elements[1], Integer
                        .valueOf(elements[2]), Float.valueOf(elements[3])));
            } catch (NumberFormatException e) {
                throw new ApplicationException(e);
            }
        }
    } finally {
        if (scanner != null) {
            scanner.close();
        }
    }

    return items;
}
公共静态列表读取(文件)引发ApplicationException{
扫描器=空;
试一试{
扫描仪=新扫描仪(文件);
}catch(filenotfounde异常){
抛出新的ApplicationException(e);
}
列表项=新建ArrayList();
试一试{
while(scanner.hasNext()){
字符串行=scanner.nextLine();
String[]elements=row.split(“\\\\”);
如果(elements.length!=4){
抛出新的ApplicationException(String.format(
“需要4个元素,但得到了%d”,elements.length);
}
试一试{
添加(新项(元素[0],元素[1],整数
.元素[2]),浮动。元素[3])的价值;
}捕获(数字格式){
抛出新的ApplicationException(e);
}
}
}最后{
如果(扫描器!=null){
scanner.close();
}
}
退货项目;
}

如何使用Scanner类忽略第一行?

在任何处理之前只需调用Scanner.nextLine()一次就可以了。

在循环之外调用Scanner.nextLine()如何

scanner.nextLine();//this would read the first line from the text file
 while (scanner.hasNext()) {
            String row = scanner.nextLine();

只需在while循环之前启动一个空的
扫描仪。nextLine
?只需在处理之前调用nextLine()函数一次。
scanner.nextLine();
while (scanner.hasNext()) {
      String row = scanner.nextLine();
      ....