题目说明:
函数Rdata()实现从文件in.dat中读取一篇英文文章,存入到字符串数组
string中,请编写函数CharRight(),其函数的功能是:以行为单位把字
符串中的最后一个字符的ASCII值右移4位后加倒数第二个字符的ASCII值,
得到最后一个新的字符,倒数第二个字符的ASCII值右移4位后加倒数第
三个字符的ASCII值,得到倒数第二个新的字符,依次类推,一直处理到
第二个字符,第一个字符的ASCII值加原最后一个字符的ASCII值,得到
第一个新的字符,得到的新字符分别存放在原字符串对应得位置上。最后
已处理的字符串仍按行重新存入字符串数组string中,最后把结果string输出到文件out1.dat中。
原始数据文件存放的格式是:每行的宽度均小于80个字符(含标点符号和
空格)。
右移算法简单,不过用io时出了点问题。 处理这类问题没经验,请指点一二。

解决方案 »

  1.   

    package arithmetic;import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;public class ASCII {
    public static int maxLine=0; //保存最大行数
    public static String str1[];
    public static void change(String str[]){
    // str1=new String[100];
    int length=0;
    char ch;//保存每行的最后一个字符

    for(int i=0;i<maxLine;i++){
    length=str[i].length();
    ch=(Character)(Object)str[i].charAt(str[i].length()-1);
    for(int j=length-1;j>=0;j--){
    try{
    str[i].replace(str[i].charAt(j), (char)((str[i].charAt(j)>>4)+str[i].charAt(j-1)));
    str[i].replace(str[i].charAt(0), ch);
    }catch(StringIndexOutOfBoundsException e){
    e.printStackTrace();
    }

    }

    }

    }
    public static void main(String[] args) {
      BufferedReader br=null;
      try {
    br=new BufferedReader(new FileReader("E:\\IO\\in.dat"));
    } catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch(Exception e){
    e.printStackTrace();
    }
    String str[]=new String[100];//保存读取到的数据。

    int i=0;
    try {
    while((str[i]=br.readLine())!=null){
    str[i]=br.readLine();
    maxLine++;
    i++;
    }
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }finally{
    try {
    br.close();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }catch(Exception e){
    e.printStackTrace();
    }
    }
    ASCII.change(str);
    BufferedWriter bw=null;
    try {
    bw=new BufferedWriter(new FileWriter("E:\\IO\\out1.dat"));
    while(str[i]!=null){
    bw.write(str[i]);
    bw.newLine();
    }
    bw.flush();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }finally{
    try {
    bw.close();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    }

    }
    我的代码如上 及出现的异常如下java.lang.StringIndexOutOfBoundsException: String index out of range: -1
    at java.lang.String.charAt(String.java:686)
    at arithmetic.ASCII.change(ASCII.java:23)
    at arithmetic.ASCII.main(ASCII.java:66)
    java.lang.StringIndexOutOfBoundsException: String index out of range: -1
    at java.lang.String.charAt(String.java:686)
    at arithmetic.ASCII.change(ASCII.java:23)
    at arithmetic.ASCII.main(ASCII.java:66)
    java.lang.StringIndexOutOfBoundsException: String index out of range: -1
    at java.lang.String.charAt(String.java:686)
    at arithmetic.ASCII.change(ASCII.java:23)
    at arithmetic.ASCII.main(ASCII.java:66)
    java.lang.StringIndexOutOfBoundsException: String index out of range: -1
    at java.lang.String.charAt(String.java:686)
    at arithmetic.ASCII.change(ASCII.java:23)
    at arithmetic.ASCII.main(ASCII.java:66)
    java.lang.StringIndexOutOfBoundsException: String index out of range: -1
    at java.lang.String.charAt(String.java:686)
    at arithmetic.ASCII.change(ASCII.java:23)
    at arithmetic.ASCII.main(ASCII.java:66)
    java.lang.StringIndexOutOfBoundsException: String index out of range: -1
    at java.lang.String.charAt(String.java:686)
    at arithmetic.ASCII.change(ASCII.java:23)
    at arithmetic.ASCII.main(ASCII.java:66)
    java.lang.StringIndexOutOfBoundsException: String index out of range: -1
    at java.lang.String.charAt(String.java:686)
    at arithmetic.ASCII.change(ASCII.java:23)
    at arithmetic.ASCII.main(ASCII.java:66)
    java.lang.StringIndexOutOfBoundsException: String index out of range: -1
    at java.lang.String.charAt(String.java:686)
    at arithmetic.ASCII.change(ASCII.java:23)
    at arithmetic.ASCII.main(ASCII.java:66)
    java.lang.StringIndexOutOfBoundsException: String index out of range: -1
    at java.lang.String.charAt(String.java:686)
    at arithmetic.ASCII.change(ASCII.java:23)
    at arithmetic.ASCII.main(ASCII.java:66)
    java.lang.StringIndexOutOfBoundsException: String index out of range: -1
    at java.lang.String.charAt(String.java:686)
    at arithmetic.ASCII.change(ASCII.java:23)
    at arithmetic.ASCII.main(ASCII.java:66)
    java.lang.StringIndexOutOfBoundsException: String index out of range: -1
    at java.lang.String.charAt(String.java:686)
    at arithmetic.ASCII.change(ASCII.java:23)
    at arithmetic.ASCII.main(ASCII.java:66)
    java.lang.StringIndexOutOfBoundsException: String index out of range: -1
    at java.lang.String.charAt(String.java:686)
    at arithmetic.ASCII.change(ASCII.java:23)
    at arithmetic.ASCII.main(ASCII.java:66)
    Exception in thread "main" java.lang.NullPointerException
    at arithmetic.ASCII.change(ASCII.java:19)
    at arithmetic.ASCII.main(ASCII.java:66)