请求高手帮忙解决下问题?

xuexi100 2010-02-17 12:32:54
package lesson2;

import java.io.*;

public class Main {
public static void main(String[] args) throws Exception {
// TODO code application logic here
RandomAccessFile raf = new RandomAccessFile("a.txt", "rw");
RandomAccessFile rab = new RandomAccessFile("b.txt", "rw");
String s = raf.readLine();
String s1 = "";
String s2 = "";
String s3 = "";
int d = 0;
int e = 0;
int g = 0;
String f;
Boolean n = false;
for (int i = 0; i < s.length(); i++) {
char a = s.charAt(i);
if (a == '+' | a == '-' | a == '*' | a == '/') {
n = true;
s3 = String.valueOf(a);
continue;
}
if (a == '=') {
break;
}
if (n) {
s2 += a;
} else {
s1 += a;
}
}
d = Integer.parseInt(s1);
e = Integer.parseInt(s2);
if (s3.equals("+")) {
g = d + e;
} else if (s3.equals("-")) {
g = d - e;
} else if (s3.equals("*")) {
g = d * e;
} else if (s3.equals("/")) {
g = d / e;
}
f = String.valueOf(g);
System.out.println(g);
rab.setLength(0);
rab.write(s1.getBytes());
rab.write(s3.getBytes());
rab.write(s2.getBytes());
rab.write('=');
rab.write(f.getBytes());
rab.write('\n');
rab.close();
raf.close();
}
}
这是我写的代码,请大家帮我看下,我想实现可以在a.txt文件中输入多行1+1=并且要求加减乘除都可以,答案结果写入另外一个b.txt文件中。
...全文
110 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
usherlight 2010-02-18
  • 打赏
  • 举报
回复
你只有一个for循环,读取了一行文本的所有字符。所以不能处理多行文本
musiclee 2010-02-18
  • 打赏
  • 举报
回复
找到原因了

发现像“.”,“|”这类属于正则表达式的通配字符,不可以直接当做一个字符来用。如果要用,则要在外面加上“[]”,如下:

public void test_split(){
String str="部门.审核";
String[] str_arr = str.split("[.]");
Assert.assertEquals(str_arr[0], "部门");
Assert.assertEquals(str_arr[1], "审核");
}


这样既可以用split 主要处理异常 就ok了
musiclee 2010-02-18
  • 打赏
  • 举报
回复

String string="324234234*3423234234";
String tempString[]=string.split("*");
System.out.println(tempString[0]);

这样会异常 谁知到原因?
musiclee 2010-02-18
  • 打赏
  • 举报
回复
完整的程序

public class Main {
public static void main(String[] args) throws Exception {
// TODO code application logic here
RandomAccessFile raf = new RandomAccessFile("d:/a.txt", "rw");
RandomAccessFile rab = new RandomAccessFile("d:/b.txt", "rw");
String s=null;
int res=0;
String s1 = "";
String s2 = "";
String s3 = "";
String sign []={"+","-","*","/"};
int index=-1;
int i=0;
while ((s=raf.readLine() )!= null) {
System.out.println(s);
for(i=0;i<sign.length;i++)
{
index=s.indexOf(sign[i]);
if(-1!=index) //找到了* / + -
break;

}
s1=s.substring(0,index);
s2=s.substring(index+1,s.length()-1);
System.out.println(i);
switch (i) {
case 0:
res=Integer.parseInt(s1)+Integer.parseInt(s2);
break;
case 1:
res=Integer.parseInt(s1)-Integer.parseInt(s2);
break;
case 2:
res=Integer.parseInt(s1)*Integer.parseInt(s2);
break;
case 3:
res=Integer.parseInt(s1)/Integer.parseInt(s2);
break;
default:
break;
}
s3=s+String.valueOf(res);
rab.writeBytes(s3);
rab.writeBytes("\r\n");
}

rab.close();
raf.close();
}
}

比较有意思的现象是 我先打算用split 可是发现 split ("*")(+,—,/)是不可以的
比如说 s="23423423213*23423423423"
s.split (*)居然异常,很让我郁闷 我找找原因吧
SambaGao 2010-02-18
  • 打赏
  • 举报
回复
split splitsplitsplitsplitsplitsplit
xuexi100 2010-02-18
  • 打赏
  • 举报
回复
引用 3 楼 musiclee 的回复:
加个while(raf.readLine()!=null) 就可以了 然后 读一行处理一行

能写下代码吗?谢谢!
musiclee 2010-02-18
  • 打赏
  • 举报
回复
加个while(raf.readLine()!=null) 就可以了 然后 读一行处理一行
xuexi100 2010-02-18
  • 打赏
  • 举报
回复
对,那该如何才能实现处理多行文本呢!请高手解决下,谢谢!

62,615

社区成员

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

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