在while循环中使用.readLine()方法跳跃对线

时间:2016-04-19 08:34:51

标签: java

我是Java新手,我遇到了一些麻烦:

我正在尝试阅读化学样本以在X-Y图上表示它们。

输入文件如下所示:

La 0.85678

Ce 0.473

Pr 62.839 ... ...

我的代码只存储了非配对行值(0.85678,跳转行,示例中为62.839),我无法意识到问题所在:

public class Procces {

   public void readREE() throws IOException {

           try{

           rEE = new BufferedReader (new FileReader ("src/files/test.txt"));

           while   ( (currentLine = rEE.readLine() ) != null) {

               try  { 

                   for (int size = 3;size<10;size++) {

                       String valueDec=(currentLine.substring(3,size));
                       //char letra =(char)c;
                       if ((c=rEE.read()) != -1) {
                           System.out.println("Max size");
                       } else

                       valueD = Double.parseDouble(valueDec);
                       System.out.println(valueDec);
                       }
                   }
             catch (Exception excUncertainDecimals) {
                    }
                }              
           }finally {   
               try {  rEE.close();
                           } catch (Exception exc) {
                              }
           } 
   }
       String line;
       int c = 0;
       int counter = 0;
       String valueS = null;
       String valueSimb = null;
       Double valueD = null;
       Double logValue = null;
       Double YFin=450.0;
       String currentLine;
       BufferedReader rEE;

}

提前感谢您,因为我无法理解为什么程序会跳过配对线。

3 个答案:

答案 0 :(得分:1)

请查看Scanner

一般来说,Java是一种成熟的语言,在大多数情况下,您不必以低级方式重新实现“常见”(例如,阅读自定义文本文件)。

答案 1 :(得分:1)

使用Java Scanner类。

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

public class MyClass {
    public static void main(String[] args) throws IOException {

        try (Scanner s = new Scanner(new BufferedReader(new FileReader("file.txt"))){

            while (s.hasNext()) {
                System.out.println(s.next());
            }
        }
    }
}

答案 2 :(得分:0)

我明白了。谢谢。

这里是代码:

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

public class Process implements Samples{

   public void readREE() throws IOException {

           try
               (Scanner rEE = new Scanner(new BufferedReader(new FileReader("src/files/test.txt")))){
                   while (rEE.hasNext()) {

                       element = rEE.next();

                       if (element.equals("La")) {
                            String elementValue = rEE.next();
                            Double value = Double.parseDouble(elementValue); 
                            Double valueChond = 0.237;
                            Double valueNorm= value/valueChond;
                            Double logValue = (Math.log(valueNorm)/Math.log(10));
                            Double yLog = yOrd - logValue*133.33333333;
                            Sample NormedSampleLa=new Sample("La",yLog);
                            sampleREE.add(NormedSampleLa);
                       }
                    } 

       } finally {
       }
}

   public String LaS, CeS, PrS, NdS, PmS, SmS, EuS, GdS, TbS, DyS, HoS, ErS, TmS, YbS, LuS;
   public String element, elementValue;
   public Double yOrd=450.0;

}