验证文本文件内容

时间:2017-11-10 10:29:57

标签: java arrays string validation

我正在创建一个读取.txt文件内容的程序,它会在一个漂亮的格式化表中验证然后输出该文件的内容。我的程序当前读取文件然后输出内容,我试图实现所述文件内容的验证。

我现在解释我的程序读取文件的方式:

.txt内容:

firstname lastname
Gameone : 120 : 1428 
Gametwo : 20 : 10 
Gamethree : 90 : 800 
Gamefour : 190 : 2001 
Gamefive : 25 : 80 
Gamesix : 55 : 862

txt文件包含以下格式的数据:

  

{gamename}:{gamescore}:{minutesplayed}

要阅读我使用的.txt:

 System.out.println("*FILE HAS BEEN LOCATED*");
    System.out.println("*File contains: \n");
    while(scan.hasNext()) 
    {
        a = scan.nextLine();
        b = scan.nextLine();
        c = scan.nextLine();
        d = scan.nextLine();
        e = scan.nextLine();
        f = scan.nextLine();
        g = scan.nextLine();
    }

然后将我使用此数据的数据拆分为a-to-g中的每个字母,如下所示:

    //~~~~a-line-split~~~~//
    String[] aline;

    aline = a.split(":");

    for (int i=0; i<aline.length; i++)
    {
        aline[i] = aline[i].trim();
        //System.out.println(aline[i]);
    }

要通过验证澄清,我需要告知用户一行中是否缺少数据,例如,如果第一行有:

  

gameone:{missing}:1428

目前我对每一行都有这个简单的if语句:

            if (bline.length < 3)
            {
                System.out.println("There is an error in row one!");
            }

但是,我需要程序确切地知道每行丢失数据的位置。不只是一般的回应:

     System.out.println("There is an error in row one!");

但相反的是:

     System.out.println("There is data missing, row: 1 column: 2");

所要求的完整代码:

    package readfile;

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

    public class readfile 
     {

     public static void main(String[] args)

     {
    Scanner scan = new Scanner (System.in);
    String FileN = " ";
    String a = " ";
    String b = " ";
    String c = " ";
    String d = " ";
    String e = " ";
    String f = " ";
    String g = " ";

    boolean fileExists = false;
    File newFile = null;
    while(!fileExists) 
    {
      System.out.println("Enter the name of the file you want to open: ");
      FileN = scan.nextLine();
      newFile = new File(FileN);
      fileExists = newFile.exists();
      if (!fileExists)

      {
        System.out.println(FileN + " not found...");
      }

    }
    try {
        Scanner scan2;
        scan = new Scanner(newFile);
    }
    catch(FileNotFoundException fnfe) {
      System.out.println("sorry but the file doesn't seem to exist");
    }


   //++++++++++++++=FILE READ=++++++++++++++++++++
    System.out.println("*FILE HAS BEEN LOCATED*");
    System.out.println("*File contains: \n");
    while(scan.hasNext()) 
    {
        a = scan.nextLine();
        b = scan.nextLine();
        c = scan.nextLine();
        d = scan.nextLine();
        e = scan.nextLine();
        f = scan.nextLine();
        g = scan.nextLine();
    }

      //+++++++++++++++ARRAYS FOR THE LINES+++++++++++++++++++

    //~~~~A-LINE~~~~//
    String[] aline;

    aline = a.split(":");

    for (int i=0; i<aline.length; i++)
    {
        aline[i] = aline[i].trim();
        //System.out.println(aline[i]);
    }

    //~~~~B-LINE~~~~//

    String[] bline;
    bline = b.split(":");

    for (int i=0; i<bline.length; i++)
    {
        bline[i] = bline[i].trim();
        //System.out.println(bline[i]);
    }

    //~~~~C-LINE~~~~//

    String[] cline;
    cline = c.split(":");

    for (int i=0; i<cline.length; i++)
    {
        cline[i] = cline[i].trim();
        //System.out.println(cline[i]);
    }

    //~~~~D-LINE~~~~//

    String[] dline;
    dline = d.split(":");

    for (int i=0; i<dline.length; i++)
    {
        dline[i] = dline[i].trim();
        //System.out.println(dline[i]);
    }

    //~~~~e-LINE~~~~//

    String[] eline;
    eline = e.split(":");

    for (int i=0; i<eline.length; i++)
    {
        eline[i] = eline[i].trim();
        //System.out.println(eline[i]);
    }

    //~~~~f-LINE~~~~//

    String[] fline;
    fline = f.split(":");

    for (int i=0; i<fline.length; i++)
    {
        fline[i] = fline[i].trim();
        //System.out.println(fline[i]);
    }

    //~~~~g-LINE~~~~//

    String[] gline;
    gline = g.split(":");

    for (int i=0; i<gline.length; i++)
    {
        gline[i] = gline[i].trim();
        //System.out.println(gline[i]);
    }

    String user       =    aline    [0];
    //~~~~~~~~~GAME NAMES~~~~~~~~~~~~~//

    //GTA
    String gameone    =    bline    [0];

    //MINECRAFT
    String gametwo  =    cline  [0];

    //ASSASSIN'S CREED IV
    String gamethree   =    dline   [0];

    //PAYDAY2
    String gamefour   =    eline    [0];

    //WOLFENSTEIN 
    String gamefive   =    fline    [0];

    //FARCRY 4
    String gamesix  =    gline  [0];




    //~~~~~~~~~~Achievement Score~~~~~~~~~~~~//


    //GTA SCORE
    String scoreone     =    bline  [1];

    //MINECRAFT SCORE
    String scoretwo     =    cline  [1];

    //ASSASSIN'S CREED IV SCORE
    String scorethree   =    dline  [1];

    //PAYDAY2 SCORE
    String scorefour    =    eline  [1];

    //WOLFENSTEIN SCORE
    String scorefive    =    fline  [1];

    //FARCRY 4 SCORE
    String scoresix     =    gline  [1];

//+++++++++++++++++++++TOTAL~~CALC++++++++++++++++++++++//
    double totalcount = 79.566; // change to the amount played.
    int totalhours = (int)totalcount;
    int totalmin = (int)(totalcount*60)%60;
    int totalsec = (int)(totalcount*(60*60))%60;
    System.out.println("TOTAL TIME PLAYED:");
    System.out.println(String.format("%s(hours) %s(minutes) %s(seconds)", 
    totalhours, totalmin, totalsec));       

     //~~~~~~~~~~Minutes Played~~~~~~~~~~~~//

    //GTA min
    String minone     =    bline    [2];

    //MINECRAFT min
    String mintwo     =    cline    [2];

    //ASSASSIN'S CREED IV min
    String minthree   =    dline    [2];

    //PAYDAY2 min
    String minfour    =    eline    [2];

    //WOLFENSTEIN min
    String minfive    =    fline    [2];

    //FARCRY 4 min
    String minsix     =    gline    [2];

    //~~~~~~~~~GAMES TEST~~~~~~~~~~~~//     

            System.out.println("\nUSER: "+user);
            System.out.println("\nDATA: ");
            System.out.println("1: "+gameone+"       | score: "+scoreone+"  
            | minutes played: "+minone);
            System.out.println("2: "+gametwo+"       | score:  "+scoretwo+" 
             | minutes played: "+mintwo);
            System.out.println("3: "+gamethree+"     | score: "+scorethree+"    
           | minutes played: "+minthree);
            System.out.println("4: "+gamefour+"          | score: 
            "+scorefour+"   | minutes played: "+minfour);
            System.out.println("5: "+gamefive+"      | score: "+scorefive+" 
           | minutes played: "+minfive);
            System.out.println("6: "+gamesix+"       | score: "+scoresix+"  
           | minutes played: "+minsix);

            if (bline.length < 3)
            {
                int column = 0;
                for(int i = 0; i<bline.length; i++){
                    column = i;
                    if(bline[i] == null || bline[i].trim() == ""){
                        System.out.println("There is an error in row two 
                  column "+(i+1));
                    }
                }
             }



}


 }

2 个答案:

答案 0 :(得分:1)

if (bline.length < 3)
{
    int column = 0;
    for(int i = 0; i<bline.length; i++){
        column = i;
        if(bline[i] == null || bline[i].trim() == ""){
            System.out.println("There is an error in row two column "+(i+1));
        }
    }
 }

没有测试过,但它应该可以正常工作

修改

查看完整代码bline[2], cline[2]...等将为您提供索引超出范围异常,如果首先在文件中缺少这些值,那么在进行该调用之前,您应首先进行检查创建一个静态方法来进行检查

public static String getAtIndex(String[] array, int indexToCkeck){
    if(indexToCkeck >=array.length){
        return "";
    }
    else{
        return array[indexToCkeck];
    }
}

所以不要使用bline[2], cline[2] ...使用readfile.getAtIndex(bline, 2),这样如果信息丢失就会返回一个空字符串

也未经过测试,但应该没问题

答案 1 :(得分:1)

使用经过良好测试的图书馆,例如supercsvany other。一旦文本格式变得更复杂,这将为您节省一些时间。它还提供了一组良好的内置数据类型,用于验证数据的每一列。您也可以将数据直接映射到POJO,这在某些情况下可以很方便。

要将每行加载到地图中,您可以执行以下操作:

在stack47220687.txt中准备文件:

Gamename: Gamescore: Minutestoplay
Gameone : 120 : 1428 
Gametwo : 20 : 10 
Gamethree : 90 : 800 
Gamefour : 190 : 2001 
Gamefive : 25 : 80 
Gamesix : 55 : 862

使用类似的东西

package stack47220687;

import java.io.FileReader;
import java.util.Map;

import org.junit.Test;
import org.supercsv.cellprocessor.constraint.NotNull;
import org.supercsv.cellprocessor.ift.CellProcessor;
import org.supercsv.io.CsvMapReader;
import org.supercsv.io.ICsvMapReader;
import org.supercsv.prefs.CsvPreference;

public class HowToReadACSVFile {

private static final CsvPreference COLON_DELIMITED = new CsvPreference.Builder('"', ':', "\n").build();

private static CellProcessor[] getProcessors() {
    final CellProcessor[] processors = new CellProcessor[] { new NotNull(), // gamename
                    new NotNull(), // gamescore
                    new NotNull(), // minutestoplay
    };
    return processors;
}

@Test
public void read() throws Exception {
    ICsvMapReader mapReader = null;
    try {
        mapReader = new CsvMapReader(new FileReader(
                        Thread.currentThread().getContextClassLoader().getResource("stack47220687.txt").getPath()),
                        COLON_DELIMITED);

        // the header columns are used as the keys to the Map
        final String[] header = mapReader.getHeader(true);
        final CellProcessor[] processors = getProcessors();

        Map<String, Object> oneRecordInAMap;
        while ((oneRecordInAMap = mapReader.read(header, processors)) != null) {
            System.out.println(String.format("lineNo=%s, rowNo=%s, this line stored in a map=%s",
                            mapReader.getLineNumber(), mapReader.getRowNumber(), oneRecordInAMap));
            /**
             * oneRecordInAMap.get("Gamescore");
             */

        }

    } finally {
        if (mapReader != null) {
            mapReader.close();
        }
    }
}

}

适用于super-csv 2.4.0

<dependency>
        <groupId>net.sf.supercsv</groupId>
        <artifactId>super-csv</artifactId>
        <version>2.4.0</version>
    </dependency>

将打印

lineNo=2, rowNo=2, this line stored in a map={ Gamescore= 120 , Gamename=Gameone ,  Minutestoplay= 1428 }
lineNo=3, rowNo=3, this line stored in a map={ Gamescore= 20 , Gamename=Gametwo ,  Minutestoplay= 10 }
lineNo=4, rowNo=4, this line stored in a map={ Gamescore= 90 , Gamename=Gamethree ,  Minutestoplay= 800 }
lineNo=5, rowNo=5, this line stored in a map={ Gamescore= 190 , Gamename=Gamefour ,  Minutestoplay= 2001 }
lineNo=6, rowNo=6, this line stored in a map={ Gamescore= 25 , Gamename=Gamefive ,  Minutestoplay= 80 }
lineNo=7, rowNo=7, this line stored in a map={ Gamescore= 55 , Gamename=Gamesix ,  Minutestoplay= 862}

如果格式不正确,它还会提供有意义的错误消息。