1.番茄 4元/个
2.鸡蛋 0.5元/个
3.螃蟹 5元/只
4.康师傅方便面 4元/盒
以上为TXT文件里的数据,现在我要使用 4 0.5 5 4 这四个数据,要怎么写
下面是我写的一部分代码
try
{
BufferedReader br = new BufferedReader(new FileReader("d:\\menu.txt"));
String s;
while ((s = br.readLine()) != null)
{
public int [] array = new int [5];/* 保存五个数据 */
}
br.close();
} catch (FileNotFoundException e1)
{
e1.printStackTrace();
} catch (IOException e2)
{
e2.printStackTrace();
}请问要怎么加,或者怎么修改?
2.鸡蛋 0.5元/个
3.螃蟹 5元/只
4.康师傅方便面 4元/盒
以上为TXT文件里的数据,现在我要使用 4 0.5 5 4 这四个数据,要怎么写
下面是我写的一部分代码
try
{
BufferedReader br = new BufferedReader(new FileReader("d:\\menu.txt"));
String s;
while ((s = br.readLine()) != null)
{
public int [] array = new int [5];/* 保存五个数据 */
}
br.close();
} catch (FileNotFoundException e1)
{
e1.printStackTrace();
} catch (IOException e2)
{
e2.printStackTrace();
}请问要怎么加,或者怎么修改?
解决方案 »
- 求高手点拨:关于Java抽象类的“多态”功能的替代问题。
- java swing 图片翻转带来的问题
- 问下,任何提供写代码效率?
- 高分求...如何用JAVA实现抽取随机数0到100,并能统计相应的概率 .....急啊
- Jframe的问题。
- java中如何进行自定义线型的绘制?
- javax.xml.parsers.*;import org.w3c.dom;
- 问大家一个简单问题关于APPLET JAPPLET
- 将文件转成把bat文件的工具叫什么名字?
- 简单问题,用JCreator的请赐教!
- 用eclipse需要配置环境变量吗
- 短信猫Linux平台JAVA二次开发包RXTX发送短信出现No response from devic异常
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.*;
public class test {
public static void main(String[] args) {
try
{
BufferedReader br = new BufferedReader(new FileReader("d:\\menu.txt"));
String s;
String str2 = "";
String strNum = "";
while ((s = br.readLine()) != null)
{
String s2[] = s.split(" ");
if(s2[1] != null){
str2 = s2[1];
}
String s3[] = str2.split("元");
if(s3[1] != null){
strNum = strNum + s3[0];
} /* 保存五个数据 */
}
System.out.println("result:"+strNum);
br.close();
} catch (FileNotFoundException e1)
{
e1.printStackTrace();
} catch (IOException e2)
{
e2.printStackTrace();
} }
}简单的 实现下。
result:4
result:40.5
result:40.55
result:40.554
result:40.554900
你为什么要把我的输出放到循环里面啊? 我的system.out.println()明明是在while之外的
if(s3[1] != null){
strNum = strNum +" "+ s3[0];
}
加个空格
String str2 = "";
String strNum = "";
String Num[] = new String[1000];
int i = 0;
while ((s = br.readLine()) != null)
{
String s2[] = s.split(" ");
if(s2[1] != null){
str2 = s2[1];
}
String s3[] = str2.split("元");
if(s3[1] != null){
strNum = strNum + s3[0];
Num[i] = s3[0];
i++;
} /* 保存五个数据 */
}
System.out.println("result:"+strNum);
for(int j = 0; j < i;j++){
System.out.println("result:"+Num[j]);
}