2
public class array {
    public static void main(String[] args) throws IOException {

         BufferedReader reader = new BufferedReader(new FileReader("fruit.txt"));

          System.out.println("enter the fruit you want to search");     
          Scanner input =  new Scanner(System.in);
          String fruit = input.nextLine();
          String line;

          List<String> list = new ArrayList<String>();
          while((line=reader.readLine()) !=null)
          {
              list.add(line);
          }

          reader.close();

          for (String s : list) {    
              System.out.println(s); 
          }
    }
}

我有水果.txt

apple 20 good
orange 30 good
banana 40 needmore

如何从数组列表中检索我有多少橙子。我希望程序在这种情况下读取用户输入“橙色”并显示 30 并且状态不好。

理想的输出是

You have orange 30 of them and status is good
4

5 回答 5

1

您需要拆分您StringsList,然后打印在您指定的字符串格式中获得的数组的每个元素:-

for (String s : list) { 

    String[] tokens = s.split(" ");
    if (tokens[0].equals(fruit)) {
          System.out.println("You have " + tokens[0] + " " + tokens[1] +  
                             " of them and status is " + tokens[2]);
          break;
    }
}

或者,您可以使用: -

System.out.format("You have %s %s of them and status is %s", 
                   tokens[0], tokens[1], tokens[2]);
于 2012-10-25T14:02:19.843 回答
1

尝试以下更新的课程。

public class array
{

   public static void main(String[] args) throws IOException
   {

      BufferedReader reader = new BufferedReader(new FileReader("fruit.txt"));

      System.out.println("enter the fruit you want to search");
      Scanner input = new Scanner(System.in);
      String fruit = input.nextLine();

      String line;

      boolean found = false;
      int count = 0;

      List<String> list = new ArrayList<String>();
      while ((line = reader.readLine()) != null)
      {
         String[] items = line.split(" ");

         if (fruit.equals(items[0]))
         {
            found = true;
            count = Integer.parseInt(items[1]);
            break;
         }

         list.add(line);
      }

      reader.close();

      if (found)
      {
         System.out.println("You have " + fruit + " " + count + " of them and status is good");
      }
   }
}
于 2012-10-25T14:05:33.773 回答
0

您需要使用 StringTokenizer 将行拆分为三个字段。然后我会创建一个新类来保存这些信息。

于 2012-10-25T14:02:38.173 回答
0

当您读取一行时,将值拆分为字符串数组,如下所示:

while((line=reader.readLine()) !=null)
            {
                String [] values = line.split(" ");

                list.add("You have "+values[0] + " " + values[1] " of them and status is "+values[2]  );
            }
于 2012-10-25T14:05:25.860 回答
0

未经测试但应该可以工作,请尝试:

public class array {

    public static class Fruit {
        private String name;
        private String count;
        private String status;

        public Fruit(String name, String count, String status) {
            this.name = name;
            this.count = count;
            this.status = status;
        }

        public String getName() {
            return name;
        }

        public String getCount() {
            return count;
        }

        public String getStatus() {
            return status;
        }
    }

    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new FileReader("fruit.txt"));
        System.out.println("enter the fruit you want to search");
        Scanner input = new Scanner(System.in);
        String fruit = input.nextLine();
        String line= "";
        HashMap<String, Fruit> map = new HashMap<String, Fruit>();
        while ((line = reader.readLine()) != null) {
            String[] strings = line.split(" ");
            map.put(strings[0], new Fruit(strings[0], strings[1], strings[2]));
        }
        reader.close();
        System.out.print("You have " + fruit + " " + map.get(fruit).getCount() + " of them and status is: " + map.get(fruit).getStatus());
    }

}
于 2012-10-25T14:06:39.790 回答