newbieprogrammer
newbieprogrammer

Reputation: 878

method to read array list to find certain string and value

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

Upvotes: 2

Views: 3769

Answers (5)

Rohit Jain
Rohit Jain

Reputation: 213321

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

Upvotes: 1

Paulius Matulionis
Paulius Matulionis

Reputation: 23413

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

}

Upvotes: 0

tjg184
tjg184

Reputation: 4686

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

Upvotes: 1

thiago.lenz
thiago.lenz

Reputation: 409

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

Upvotes: 0

Darren
Darren

Reputation: 792

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.

Upvotes: 0

Related Questions