使用扫描仪选择特定文本

时间:2014-03-11 05:20:02

标签: java java.util.scanner delimiter

我试图使用扫描仪和分隔符抓取某些文本,但我一直在失败。

来自档案的样本:

  

CROSSPLANE_AXIS = X

     

DEPTH_AXIS =深度

     

INPLANE_AXIS_DIR = GUN_TARGET

     

CROSSPLANE_AXIS_DIR = LEFT_RIGHT

     

DEPTH_AXIS_DIR = UP_DOWN

     

ENERGY = 6.00

     

NOMINAL_DMAX = 13.00

     

SSD = 1000.00

     

SCD = 450.00

     

BLOCK = 0

     

WEDGE = App25x25

public static Double energy;
for (int i = 0; i < lengthOfFiles; i++) {
                Scanner readAndStore = new Scanner(content);
                int j = 0;

                while (readAndStore.hasNextLine()) {

                     if (readAndStore.hasNext("ENERGY=")) {
                        readAndStore.useDelimiter("=");
                        energy = readAndStore.nextDouble();
                        }

1 个答案:

答案 0 :(得分:2)

由于您的所有行都是相同的格式,因此更容易

 // read all lines
 while (readAndStore.hasNextLine()) {

     String line = readAndStore.readLine ();


     // split using =
     String [] words = line.split ("=");     // assuming you are going to parse all lines

     if (words[0].equals ("ENERGY")) {
         energy = Double.parseDouble (words[1]);
     }