I would like to compare user input against and entire file and print out message when item is found or item not found. My program below appears to do that but it is doings so one line at a time. Please help.

Jems

import java.util.*;
   import java.text.*;
   import java.io.*;
   import java.lang.*;

//------------------------------------------------------------------------//
// class ShareHolder                                                      //
// Provides information about a specific shareholder                      //
//------------------------------------------------------------------------//
   public class ShareHolder implements Comparable
   {
      String firstName;
      String lastName;
   
   //------------------------------------------------------------------------//
   // ShareHolder()                                                          //
   // Default Constructor. Sets the first and last name to an empty string   //
   //------------------------------------------------------------------------//
      public ShareHolder()
      {
         firstName = "";
         lastName = "";
      }
   
   //------------------------------------------------------------------------//
   // ShareHolder(String,String)                                             //
   // Constructor. Sets the shareholders first name and last name to the     //
   // specified values                                                       //
   //------------------------------------------------------------------------//
      public ShareHolder(String first, String last)
      {
         firstName = first;
         lastName = last;
      }
   
   //------------------------------------------------------------------------//
   // int CompareTo(Object)                                                  //
   // Returns a negative number if this is less than other, zero if          //
   // this is equal to other, and a positive number if this is greater       //
   //------------------------------------------------------------------------//
      public int compareTo(Object other)
      {
         String thisString = lastName+firstName;
         String otherString = ((ShareHolder)(other)).lastName +
                         ((ShareHolder)(other)).firstName;
         return thisString.compareTo(otherString);
      }
   
   //------------------------------------------------------------------------//
   // String getFirstName()                                                  //
   // Returns the first name of the shareholder                              //
   //------------------------------------------------------------------------//
      public String getFirstName()
      {
         return firstName;
      }
   
   //------------------------------------------------------------------------//
   // String getLastName()                                                   //
   // Returns the last name of the shareholder                               //
   //------------------------------------------------------------------------//
   
      public String getLastName()
      {
         return lastName;
      }
   
   // This is the driver
      public static void main (String[] args) throws IOException {
         ArrayList<String> members = new ArrayList<String>();
         BufferedReader br = null;
         br = new BufferedReader( new FileReader( "owners.txt" ) );
         String allNames = "";
         while ((allNames = br.readLine()) != null)
            members.add(allNames);
      // Create an array
         String[] names = new String[members.size()];
         members.toArray(names);
      // Takes user entry
         Scanner user = new Scanner(System.in);
         System.out.println("Please enter your first name:"); // Enter first
         String first = user.next();
         System.out.println("Please enter your last name:"); // Enter last
         String last = user.next();	
      // Create new object
         ShareHolder out = new ShareHolder(first,last);
         System.out.println(out.getFirstName()+" "+out.getLastName());
         String str = "";
         str = out.getFirstName()+" "+out.getLastName();
         for (int i = 0; i < names.length; i++)
            if (str.equalsIgnoreCase(names[i])) {
               System.out.println("You may participate in the meeting!");
            }
            else {
               System.out.println("Sorry your name is not on the list!");
            }
      }				
   }

1. Create a boolean variable found and initialize it to false just before your loop on line 90.
2. Replace your print statement on line 92 with these 2 statements:

found = true;
   break;

3. Get rid of lines 94-96.
4. After the for loop ends, put your if/else statement, but just check whether the name was found like this:

if (found)
      // print message 1
   else
      // print message 2

Thanks KramerD...my problem is solved.

I would like to also give the user 2 tries at input and am thinking of a counter loop. In other words, if the user's first attempt did not find his name on the list then he gets another try. Somehow I would like to us the following but need to keep the scanner input open.

for(int limitcounter=0; limitcounter <= 1; limitcounter++) {

Thanks,
Jems

That looks like it will work. Do you have another question?

I basically, needed a hint as to how to go about inserting it into the logic. As recommended by Taywin in another related question, the suggestions was to enclose the initiation of the scanner and user inputs into a "do" loop and setup variables outside the loop. Where I really got stuck was on how to keep the initiated scanner and user input active so I can provide the necessary conditions to throw appropriate messages. What would happen was once I get past the "if" and then the "else" my execution ends prematurely (although as it should since I wrote it that way in the first place). Using a do loop to encapsulate the scanner and user input actions did provide the necessary control needed to follow a logic of - if OK at any instance of user input then end execution and print message accordingly. If NOT OK at any instance of user inputs given two attempts, then print first message to say sorry try again at which point the scanner input remains active for the user to get a second try. If failure on second try then simply end execution otherwise all is well and is OK

Thanks again KramerD.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.