StringIndexOutOfBoundsException错误 [英] StringIndexOutOfBoundsException Error

查看:132
本文介绍了StringIndexOutOfBoundsException错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想解析一个数字文件。我实现解析,但是当我更改文件内容时,我得到一个StringIndexOutOfBoundsException。任何帮助将不胜感激。



我的代码如下所示:

  public void fileParse()throws IOException 
{



文件f = new File(180214.txt);
文件f1 =新文件(salelog.txt);

String s = null;

FileWriter fw = new FileWriter(f1);
PrintWriter pw = new PrintWriter(fw);

BufferedReader br = new BufferedReader(new FileReader(f)); $($ s

) '0')
{
pw.println(< receipt>);
pw.println(s);
继续;

if(s.charAt(0)=='8')
{
pw.println(s);
pw.println(< / receipt>);
继续;
}
pw.println(s);

}

// br.close();
pw.close();
br1.close();



$ b code
$ b

我的文件内容是这样的:

$ $ $ $ $ $ c $ 001021402180000000000000000
4869359340051000000011001000000100000000000
58693593400510800000000008000 0
600000001100100
800300000001070002075700000001100000000008
001021402180000000000000000
4869697119156000000003001000000100000000000
58696971191560800000000002000 0
600000000300100
800500000001060002080000000000300000000002
001021402180000000000000000
4869069850477000000010001500000200000000000
58690698504770800000000015000 0
4000008691497000000012001500000100000000000
50000086914970800000000009000 0
4000004082260000000008001000000100000000000
50000040822600800000000006000 0
600000004000100
8 00500000001060003080100000004000000000030
001021402180000000000000000
4000008090548000000028501400000100000000000
50000080905480800000000021000 0
600000002850100
800500000001060004080900000002850000000021
001021402180000000000000000
4869088520157000000049501200000100000000000
58690885201571800000000076000 0
600000004950100
800500000001060005080900000004950000000076
001021402180000000000000000
4869749799821000000019501400000100000000000
58697497998210800000000014000 0
4869076767108000000049501500000100000000650
58690767671080800000000032000 0
986907676710801 $○○○○○○○○六五○○○○○一
600000006250100
800500000001060006081200000006250000000046
001021402180000000000000000
497721462356100000000 7503200000100000000000
59772146235610000000000000000 0
600000000750100
800500000001060007081300000000750000000000


解决方案

这里唯一可以引发 IndexOutOfBoundsException 的是

  s.charAt(0)

code。

这意味着在第一个位置的这一行没有空行。



在循环开始时尝试以下操作: / p>

  if(s == null || s.trim()。length()== 0)
{
System.out.println(Empty line found。);
继续;
}


I want to parse a numeric file. I achieve parsing but when I changed the file content, I get a StringIndexOutOfBoundsException. Any help will be appreciated.

My code is like below:

public void fileParse() throws IOException
{



    File f = new File("180214.txt");
    File f1 = new File("salelog.txt");

    String s = null;

    FileWriter fw = new FileWriter(f1);
    PrintWriter pw = new PrintWriter(fw);

    BufferedReader br = new BufferedReader(new FileReader(f));


    while((s=br.readLine())!=null)
    {

            if(s.charAt(0) == '0')
            {
                pw.println("<receipt>");
                pw.println(s);
                continue;
            }
            if(s.charAt(0)=='8')
            {
                pw.println(s);
                pw.println("</receipt>");
                continue;
            }
            pw.println(s);

    }

//  br.close();
    pw.close();
    br1.close();

}

}

My file content is like this:

001021402180000000000000000                
4869359340051000000011001000000100000000000
58693593400510800000000008000    0         
600000001100100                            
800300000001070002075700000001100000000008 
001021402180000000000000000                
4869697119156000000003001000000100000000000
58696971191560800000000002000    0         
600000000300100                            
800500000001060002080000000000300000000002 
001021402180000000000000000                
4869069850477000000010001500000200000000000
58690698504770800000000015000    0         
4000008691497000000012001500000100000000000
50000086914970800000000009000    0         
4000004082260000000008001000000100000000000
50000040822600800000000006000    0         
600000004000100                            
800500000001060003080100000004000000000030 
001021402180000000000000000                
4000008090548000000028501400000100000000000
50000080905480800000000021000    0         
600000002850100                            
800500000001060004080900000002850000000021 
001021402180000000000000000                
4869088520157000000049501200000100000000000
58690885201571800000000076000    0         
600000004950100                            
800500000001060005080900000004950000000076 
001021402180000000000000000                
4869749799821000000019501400000100000000000
58697497998210800000000014000    0         
4869076767108000000049501500000100000000650
58690767671080800000000032000    0         
986907676710801$0000000065000001           
600000006250100                            
800500000001060006081200000006250000000046 
001021402180000000000000000                
4977214623561000000007503200000100000000000
59772146235610000000000000000    0         
600000000750100                            
800500000001060007081300000000750000000000 

解决方案

The only thing here, which can throw an IndexOutOfBoundsException is the

s.charAt(0)

code.

That means that in this line on the first position there is nothing = empty line.

Try the following in the beginning of your loop:

if(s == null || s.trim().length() == 0)
{
    System.out.println("Empty line found.");
    continue;
}

这篇关于StringIndexOutOfBoundsException错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆