从文本文件中解析并保存行

时间:2014-04-12 21:27:54

标签: java java.util.scanner

我正在尝试扫描具有以下格式的文本文件:

reviewers: 0
open: Sunday 08:00 16:00,Monday 06:00 20:00,Tuesday 06:00 20:00,Wednesday 06:00 20:00,Thursday 06:00 20:00,Friday 06:00 20:00,Saturday 06:00 01:00
name: The Lyre of Orpheus
city: San Francisco
cost: $$
category: Greek,Breakfast & Brunch,Seafood,Salad,Soup
rank: 0

并将每一行保存为字符串或双精度,但我继续使用我的forloop中的sys.out.println获取null,我可以更改任何内容吗?我的想法是,我在我的尝试中过早地重置我的变量或者其他东西。

 public class Yulp {
//instance vars
ArrayList<Restaurant> resList = new ArrayList<Restaurant>();

public static void main(String args[]) {
    Yulp yelp = new Yulp();
    yelp.scan();
    for(int i = 0; i < yelp.resList.size(); i++){
        System.out.println(yelp.resList.get(i).getCity());
    }

}

public void scan() {
    try {
        Restaurant tempRes = new Restaurant();
        String name, city, category, cost;
        double rank, reviewers;
        Scanner scan = new Scanner(new File("randomizedList.txt"));
        while (scan.hasNext()) {
            //name = null; city = null; category = null; cost = null; rank = 0.0; reviewers = 0;
            String line = scan.nextLine();
            String rest = omitPrefix(line, "reviewers:");
            if (rest != null) {
                reviewers = Double.parseDouble(rest);
                tempRes.setReviewers(reviewers);
            }
            rest = omitPrefix(line, "rank:");
            if (rest != null) {
                rank = Double.parseDouble(rest);
            }
            rest = omitPrefix(line, "name:");
            if (rest != null) {
                name = rest;
            }
            rest = omitPrefix(line, "city:");
            if (rest != null) {
                city = rest;
            }
            rest = omitPrefix(line, "category:");
            if (rest != null) {
                category = rest;
            }
            rest = omitPrefix(line, "cost:");
            if (rest != null) {
                cost = rest;
            }
            resList.add(tempRes);
        }
        scan.close();
    } catch (FileNotFoundException e) {
        System.out.println("File not found");
    }
}

private String omitPrefix(String line, String prefix) {
    if (line.startsWith(prefix))
        return line.substring(prefix.length());
    return null;
}

}

2 个答案:

答案 0 :(得分:0)

这是我拥有的

import java.io.InputStream;
import java.util.Scanner;

public class Snippet {
  public void scan() throws Exception {
    Scanner scan = new Scanner(new File("randomizedList.txt"));
    while (scan.hasNext()) {
      String line = scan.nextLine();
      String rest = omitPrefix(line, "reviewers:");
      if (rest != null) {
        System.out.println(rest);
      }
      rest = omitPrefix(line, "name:");
      if (rest != null) {
        System.out.println(rest);
      }
      rest = omitPrefix(line, "city:");
      if (rest != null) {
        System.out.println(rest);
      }
      rest = omitPrefix(line, "category:");
      if (rest != null) {
        System.out.println(rest);
      }    
    }
    scan.close();
  }

  private String omitPrefix(String line, String prefix) {
    if (line.startsWith(prefix)) 
      return line.substring(prefix.length());
    return null;
  }

  public static void main(String[] args) throws Exception {
    new Snippet().scan();
  }
}

要点:

  • 您可以line = new Scanner(scan.nextLine()); temp = line.nextLine();而不是scan.nextLine
  • 为了得到一行的剩余部分(例如,&#34;名称:&#34;部分之后的部分),我引入了一个名为omitPrefix(line, prefix)的辅助方法,它返回line sans {如果prefixline开头,则为{1}},否则为prefix

答案 1 :(得分:0)

这是另一种做法。

public class FileParser {
    public void scan() {
      try {
        File list = new File("randomizedList.txt");
        Scanner scan = new Scanner(list);
        String temp;
        Scanner line;
        while (scan.hasNext()) {
            line = new Scanner(scan.nextLine());
            temp = line.nextLine();
            if (temp.startsWith("reviewers:")) {
                System.out.println(temp.split(":",2)[1]);
            }

            if (temp.startsWith("name:",1)) {
                System.out.println(temp.split(":",2)[1]);
            }

            if (temp.startsWith("open:")) {
                System.out.println(temp.split(":",2)[1]);
            }
            if (temp.startsWith("city:")) {
                System.out.println(temp.split(":",2)[1]);
            }

            if (temp.startsWith("category:")) {
                System.out.println(temp.split(":",2)[1]);
            }
        }
        scan.close();
    }
    catch(FileNotFoundException e){

        System.out.println("File's MISSIN!");
    }
    catch(NoSuchElementException e){
        System.out.println("NoSuchElementException dangus.");
    }
}

public static void main(String[] args) {
    new FileParser().scan();
}

}

选中startsWith行,找到介绍性关键字。

然后在split周围使用:字符串,返回两个字符串的数组。

返回数组中的第二个字符串由[1]到达,这将产生上述输出

虽然可以对此代码进行进一步的改进。

if语句应该重构并作为单独的方法提取。

应提取字符串常量,并且每行只应检查一次。

一旦匹配,您应该转到下一行。