In my program I need to have the user get two chances before ending the program. Currently if the condition is not satisfied then they only get one try. I would like some help in getting a second try.

// 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();
         boolean found = false;
         for (int i = 0; i < names.length; i++)
            if (str.equalsIgnoreCase(names[i])) {
               found = true;
               break;
            }
         if (found) {
            System.out.println("You may participate in the meeting!");
         }
         else {     
            System.out.println("Sorry name not found, please try again!");
// Need help here!!!!
            System.out.println("After second attempt your name is not on the list!");
         }		
      }  
   }

If you fix it to 2 times, you could just repeat it again in your code???

Hi Taywin,

Not sure I follow...to what do you refer I should fix to 2 times??

It is depended on how you want it to be implemented. If you want a nice and reusable solution, use do-while loop around your code below. Just count the number you allow it to appear. If you just want a simple but not well construct code, just add your code portion below inside your last else.

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();
         boolean found = false;
         for (int i = 0; i < names.length; i++)
            if (str.equalsIgnoreCase(names[i])) {
               found = true;
               break;
            }
         if (found) {
            System.out.println("You may participate in the meeting!");
         }
         else {
            System.out.println("Sorry name not found, please try again!");
         }

It is depended on how you want it to be implemented. If you want a nice and reusable solution, use do-while loop around your code below. Just count the number you allow it to appear. If you just want a simple but not well construct code, just add your code portion below inside your last else.

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();
         boolean found = false;
         for (int i = 0; i < names.length; i++)
            if (str.equalsIgnoreCase(names[i])) {
               found = true;
               break;
            }
         if (found) {
            System.out.println("You may participate in the meeting!");
         }
         else {
            System.out.println("Sorry name not found, please try again!");
         }

Thanks once again Taywin for your suggestion. I am new at this learning the subject. Doing as you suggest would cause errors in the sense there would be variables that are already created. Secondly, the object is to keep the first initiated input alive/active because the logic of the program is:
1. If name on list found then all is well and program ends.
2. If name not found the first time, then give the user a second chance and print an error message to try again. Here the system should allow this as it stays active and not terminate. I need help on how to do this.
3. Once the user gets a second chance, if the name is found then all is well and the program ends. If the name is not found a second time then a different message to tell the user he/she failed on second attempt and then program ends.

Jems:

I am sorry for assuming that you have already understood how to deal with variables in a loop. What you need to do with variables is to declare them outside the loop, but you may assign a value to each of them inside the loop. You may read about 'scope' for further information about how variable works.

// declare all variables that you want them to be used both inside & outside
// the loop
int timeAllow = 2;
bool found = false;
ShareHolder out;
Scanner user = new Scanner(System.in);

// start the loop which user must go through this at least once
do {
  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
  // ** Not sure why you want to create an object first before knowing that
  // the name may or may not exist in your list. If you want to sanitize
  // the input name, you could implement a validate function instead???**
  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])) {
      found = true;
      break;
    }
  timeAllow--;

  if (found) {
    System.out.println("You may participate in the meeting!");
  }
  else {     
    System.out.println("Sorry name not found, please try again!"); // Need help
  }
} while (!found && timeAllow>0);

if (!found) {
  System.out.println("After second attempt your name is not on the list!");
}

Great! thanks for understanding my position. I understand what you have suggested and it provided the solution need for my question. Problem solved.

Jems

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.