我正在尝试使用扫描仪和定界符来获取某些文本,但我一直失败。

来自文件的样本:


  CROSSPLANE_AXIS = X
  
  DEPTH_AXIS =深度
  
  INPLANE_AXIS_DIR = GUN_TARGET
  
  CROSSPLANE_AXIS_DIR = LEFT_RIGHT
  
  DEPTH_AXIS_DIR = UP_DOWN
  
  能量= 6.00
  
  NOMINAL_DMAX = 13.00
  
  固态硬盘= 1000.00
  
  SCD = 450.00
  
  块= 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();
                        }

最佳答案

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

 // 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]);
     }

关于java - 使用扫描仪选择特定文本,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22317056/

10-10 19:16