简体   繁体   中英

Printing and matching values in java

I have a program that will read a text file starting on line number 29. If the line contains the words "na" or "Total" the program will skip those lines. The program will get the elements [2] and [6] from the array. I need to get element [6] of the array and print it underneath its corresponding value. Element[2] of the array is where all the analytes are and element[6] contains the amount of each analyte.

The files that the program will read look like this:

12      9-62-1                      

Sample Name:        9-62-1          Injection Volume:       25.0  
Vial Number:        37              Channel:                ECD_1
Sample Type:        unknown         Wavelength:             n.a.
Control Program:    Anions Run      Bandwidth:              n.a.
Quantif. Method:    Anions Method   Dilution Factor:        1.0000  
Recording Time:     10/2/2013 19:55 Sample Weight:          1.0000  
Run Time (min):     14.00           Sample Amount:          1.0000  




No.     Ret.Time            Peak Name   Height  Area        Rel.Area    Amount  Type 
        min     µS  µS*min  %   mG/L    
1   2.99        Fluoride    7.341       1.989   0.87        10.458      BMB
2   3.88        Chloride    425.633     108.551 47.72       671.120     BMb
3   4.54        Nitrite     397.537     115.237 50.66       403.430     bMB
4   5.39        n.a.        0.470       0.140   0.06        n.a.        BMB
5   11.22       Sulfate     4.232       1.564   0.69        13.064      BMB
    Total:      835.213                 227.482 100.00      1098.073    

The program needs to read that type of files and stores the element[6] of the array under a heading in a separate file in a folder. That file will have a heading like this:

Fluoride,Chloride,Nitrite,Sulfate,

The amount of fluoride should go under fluoride, the amount of chloride should go under chloride and so on and if there isn`t Nitrite or any other analyte it should put a zero for each analyte. I just need to know how to match that and then I know I have to make write to the file which I will do later, but for know I need help matching.

The final output should looe like this. The first line will be written in the textfile and then the second line will be values that will be match under its corresponding analyte like this:

Sample#,Date,Time,Fluoride,Chloride,Nitrite,Sulfate,9-62-1,10/2/2013,19:55,10.458,671.120,403.430,13.064,

Also again if an analyte isnt present on the file or it is null it should put a 0.

Here is my code:

//Get the sample#, Date and time.

String line2;
    while ((line2 = br2.readLine()) != null) {
        if (--linesToSkip2 > 0) {
            continue;
        }
        if (line2.isEmpty() || line2.trim().equals("") || line2.trim().equals("\n")) {
            continue;
        }
        if (line2.contains("n.a.")) {
            continue;
        }
        if (line2.contains("Total")) {
            continue;
        }

        String[] values2 = line2.split("\t");
        String v = values2[2];//Stored element 2 in a string.
        String v2 = values2[6];//Stored element 6 in a string.
        String analytes = "Fluoride,Chloride,Nitrite,Sulfate";//Stored the analytes into an array.

        if (analytes.contains(v)) {
            System.out.println(v2);
        }


        int index2 = 0;
        for (String value2 : values2) {
            /*System.out.println("values[" + index + "] = " + value);*/
            index2++;
        }
        System.out.print(values2[6] + "\b,");
        /*System.out.println(values[6]+"\b,");*/


        br.close();

    }

Thanks in advance!

So if i understand your task right and every element is in new line. Where is a lot of ways how to solve this, but with your code simpliest way to solve it in my opinion would be with StringBuffer.

//In your code i saw you have to arrays one of them with element name 
//other with element code or smth
StringBuffer firstLine = new StringBuffer();
StringBuffer secondLine = new StringBuffer();
public static void printResult(String[] Name, String[] Code){
    //First we gona make first line
    //Here we are adding data before Names
    firstLine.append("Stuff before Names");
    for(int i =0;i<name.length;i++){
        //Here we gona add all names in the list which is good
        //Dont forget spaces
        firstLine.append(name[i]+ " ");
    }
    //And same goes for second line just change loop array and data added before loop.

    //And in the end this should print out your result
    System.out.println(firstLine+"\n" + secondLine);
}

Call this method after all file reading is done. Hope it helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM