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); 
          }
    }
}

I have fruit.txt

apple 20 good
orange 30 good
banana 40 needmore

how do I retrieve how many oranges I have from the array list. I want the program to read the user input in this case "orange" and display out 30 and the status is not good.

ideal output is

You have orange 30 of them and status is good
0

5 Answers 5

1

Try the following updated class.

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");
      }
   }
}
Sign up to request clarification or add additional context in comments.

Comments

1

You need to split your Strings in your List, and then print each elements of your array obtained within your specified string format: -

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;
    }
}

Or, you can use: -

System.out.format("You have %s %s of them and status is %s", 
                   tokens[0], tokens[1], tokens[2]);

1 Comment

@AmitD. Yeah I like using it when it comes to formatting output. :)
0

You will need to split up the lines into the three fields using a StringTokenizer. Then I would create a new class to hold that information.

Comments

0

When you read a line, split the values into String array like this:

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

                list.add("You have "+values[0] + " " + values[1] " of them and status is "+values[2]  );
            }

Comments

0

Not tested but should work, try:

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());
    }

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.