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();
      }请问要怎么加,或者怎么修改?

解决方案 »

  1.   

    import java.io.BufferedReader;
    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();
      } } 
    }简单的 实现下。
      

  2.   

    不对啊,输出的结果是
    result:4
    result:40.5
    result:40.55
    result:40.554
    result:40.554900
      

  3.   


    你为什么要把我的输出放到循环里面啊? 我的system.out.println()明明是在while之外的
      

  4.   

    忘记补充一点了 String s3[] = str2.split("元");
    if(s3[1] != null){
    strNum = strNum +" "+ s3[0];

    加个空格
      

  5.   

     String s;
              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]);
              }