帮我看看啊,大哥大姐们!

fiore 2006-08-17 05:47:43
import java.awt.*; import java.awt.event.*;
import java.io.*;
class Example20_5
{ public static void main(String args[])
{ char a[]="今天晚上十点发起总攻".toCharArray();
int n,m;
try{ File f=new File("D:\\program","secret.txt");
for(int i=0;i<a.length;i++)
{ a[i]=(char)(a[i]^'R');
}
FileWriter out=new FileWriter(f);
out.write(a,0,a.length);
out.close();
FileReader in=new FileReader(f);
int length=(int)f.length();
char tom[]=new char[length];
while((n=in.read(tom,0,tom.length))!=-1)
{ String s=new String(tom,0,n);

System.out.println("密文: "+s);
}
in.close();
m=n;
for(int i=0;i<m;i++)
{ tom[i]=(char)(tom[i]^'R');
}
String s1=new String(tom,0,m);
System.out.println("明文: "+s1);
}
catch(IOException e)
{ System.out.println("Error");
}
}
}
Exception in thread "main" java.lang.String Index OutOfBoundsException:String index out of range: -1
at java.lang.String<init><String.java:192>
at Example20_5.main<Example20_5.java:27>
明文的内容不能显示出来,初学者,大家帮我看看啊!
...全文
452 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
jimshen 2006-08-18
  • 打赏
  • 举报
回复
我第二次贴的程序已经解决了,返回来的n实际是读到的字符数量
fiore 2006-08-18
  • 打赏
  • 举报
回复
我再到外面定义一次s:String s=new String(tom,0,length-10);接着m = s.toCharArray().length;
这样行吗?
如果不减10,明文会多出10个R出来。
jimshen 2006-08-18
  • 打赏
  • 举报
回复
/*
* Main.java
*
* Created on 2006年7月25日, 下午1:13
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/

package test;

import java.awt.*;
import java.awt.event.*;
import java.io.*;

class Example20_5 {
public static void main(String args[]) {
char a[]="1分30秒后向a平台发起总攻".toCharArray();
int n,m;
try{
File f=new File("d:/secret.txt");
for(int i=0;i<a.length;i++) {
a[i]=(char)(a[i]^'R');
}
FileWriter out=new FileWriter(f);
out.write(a,0,a.length);
out.close();
FileReader in=new FileReader(f);
int length=(int)f.length();
char tom[]=new char[length];
n=in.read(tom,0,tom.length);
String s=new String(tom,0,n);
System.out.println("密文: "+s);
in.close();
for(int i=0;i<n;i++) {
tom[i]=(char)(tom[i]^'R');
}
String s1=new String(tom,0,n);
System.out.println("明文: "+s1);
} catch(IOException e) {
System.out.println("Error");
}
}
}
figoren 2006-08-18
  • 打赏
  • 举报
回复
把存放密文的s提到外面定义然后
m = s.toCharArray().length;
sxnucseven 2006-08-18
  • 打赏
  • 举报
回复
char tom[]=new char[length/2];
这句太轻率了吧
如果改成“1分30秒后向a平台发起总攻”
肯定会出错的
yijiang 2006-08-18
  • 打赏
  • 举报
回复
mark....
jimshen 2006-08-18
  • 打赏
  • 举报
回复
^是异或运算,A和一个数字B异或后变成C,如果C再用同一个数字B进行一次异或会恢复成原来的A

A^B=>C
C^B=>A
J_fox 2006-08-17
  • 打赏
  • 举报
回复
a[i]=(char)(a[i]^'R');是什么意思呀
还有
tom[i]=(char)(tom[i]^'R');又是什么意思呀,
学习学习,希望各位前辈讲解讲解!!!
jimshen 2006-08-17
  • 打赏
  • 举报
回复
/*
* Main.java
*
* Created on 2006年7月25日, 下午1:13
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/

package test;

import java.awt.*;
import java.awt.event.*;
import java.io.*;

class Example20_5 {
public static void main(String args[]) {
char a[]="今天晚上十点发起总攻".toCharArray();
int n,m;
try{
File f=new File("d:/secret.txt");
for(int i=0;i<a.length;i++) {
a[i]=(char)(a[i]^'R');
}
FileWriter out=new FileWriter(f);
out.write(a,0,a.length);
out.close();
FileReader in=new FileReader(f);
int length=(int)f.length();
char tom[]=new char[length/2];
while((n=in.read(tom,0,tom.length))!=-1) {
String s=new String(tom,0,n);
System.out.println("密文: "+s);
}
in.close();
m=tom.length;
for(int i=0;i<m;i++) {
tom[i]=(char)(tom[i]^'R');
}
String s1=new String(tom,0,m);
System.out.println("明文: "+s1);
} catch(IOException e) {
System.out.println("Error");
}
}
}
fishyqd 2006-08-17
  • 打赏
  • 举报
回复
String s1=new String(tom,0,m);
改成
String s1=new String(tom,0,m-1);
jimshen 2006-08-17
  • 打赏
  • 举报
回复
String index out of range 越界?

m=n;
for(int i=0;i<m;i++)
{ tom[i]=(char)(tom[i]^'R');
}

这里 m变成-1了

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧