java - 如何在 Java 中验证文本文件内容?

标签 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 到 g 的每个字母使用这个 for 循环,如下所示:

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



}


 }

最佳答案

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) 这样如果信息丢失它将返回一个空字符串

还没有测试过,不过应该没问题

关于java - 如何在 Java 中验证文本文件内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47220687/

相关文章:

java - 字符串连接时 Java 字符串池如何工作?

Java getClass().equals() 比较

java - 在 java 中使用 Ldap 获取组

java - 部署 Jar 文件找不到主类 eclipse

arrays - 生成不重复的组合(排列)矩阵(数组超出最大数组大小首选项)

c++ - 重新分配 C 样式字符串会导致内存泄漏吗?

java - 小数据的选择排序与快速排序

javascript - 填充对象时,神秘的 "remove" key 插入到我的对象中

php - 从单个 PDO 数组实例化多个 PHP 对象(无样板)

R如何用新的子字符串替换较长字符串中的子字符串