Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java Based questions. I just need help with one data validation Statement. This

ID: 646809 • Letter: J

Question

Java Based questions.

I just need help with one data validation Statement. This is a poker program. This is part of a discarding a card process. I want the program to throw the user an error if he trys to discard a card in which the arraylist position has already been deleted. I have a few comments in bold where im having trouble Here is my current code:

public void WhichCard()
{Scanner input = new Scanner(System.in);

int cardDiscarded;
int cardsInHand = 5;
    //Loop to go through this code for the amount of cards the user wants to discard
    for(int index = 0;index<this.amountDiscarded;index++)
    {
     //Prompts for user to see what cards to discard  
  
    DisplayUsersCards();
//Just to display grid for the user to know what cards he can choose from
    //if he has multiple cards after the user discards the first card it will erase the 5.
   for(int printer = 1; printer<=cardsInHand; printer++)
   {
       System.out.print(" " +printer + "   ");
     
   }
   System.out.print("What cards would you like to discard?: ");
       cardsInHand --;

do {
    
        while (!input.hasNextInt()) {
            System.out.println("That's not a number! TRY AGAIN");
            System.out.print("WHICH CARD WOULD YOU LIKE TO DISCARD?");
            input.next(); // this is important!
        }
    //Accept User Input
    cardDiscarded = input.nextInt();THIS NUMBER HAS BE LESS THAN THE AMOUNT OF CARDS IN HAND
    //Modifying the variable because this will be use for an array element
    cardDiscarded = cardDiscarded -1;
    //Discarding
    Hand.remove(cardDiscarded);

}while (!(isTrue));// do loop


    }
//Deals the cards that have been discarded  
UsersHand();

}

SOME OF THE OUTPUT

Would you like to discard any cards? (Y OR N)Y
How many cards would you like to discard? (Max 3 Cards)2
[10?, 10?, 3?, 8?, 3?]
1    2    3    4    5   What cards would you like to discard?: 3<~ NUMBER TO BE VALIDATED
[10?, 10?, 8?, 3?]
1    2    3    4   What cards would you like to discard?: 2<~ NUMBER TO BE VALIDATED because there is only 4 cards
Your final hand is: [10?, 8?, 3?, Q?, 7?]

Im just stuck because i need it to go back up to the first do loop if the user inputs a value greater than the amount of cards in hand.

Explanation / Answer

    //Modified java code
   public void WhichCard()
   {
       Scanner input = new Scanner(System.in);

       int cardDiscarded;
       int cardsInHand = 5;
       //Loop to go through this code for the amount of cards the user wants to discard
       for(int index = 0;index<this.amountDiscarded;index++)
       {
           //Prompts for user to see what cards to discard  
           DisplayUsersCards();
           //Just to display grid for the user to know what cards he can choose from
           //if he has multiple cards after the user discards the first card it will erase the 5.
           for(int printer = 1; printer<=cardsInHand; printer++)
           {
               System.out.print(" " +printer + "   ");
           }
           System.out.print("What cards would you like to discard?: ");
           cardsInHand --;

           do {

               while (!input.hasNextInt())
               {
                   System.out.println("That's not a number! TRY AGAIN");
                   System.out.print("WHICH CARD WOULD YOU LIKE TO DISCARD?");
                   input.next(); // this is important!
               }
              
              
               //Use do while loop that prompt for which card to discard
               //value and check if cardDiscarded value is greater than
               //the cardInHand then display a message and repeat
               //the loop until user enter card number which is
               //in between 0 and cardsInHand
               do
               {
                   //Accept User Input
                   cardDiscarded = input.nextInt();
                  
                   //show a message if cardDiscarded is less than zero
                   //or cardDiscarded is greater than cardsInHand
                   if(cardDiscarded<0||cardDiscarded>cardsInHand)
                       System.out.println("Card number must be less than number of cards" +
                               " in hand ");
                  
               }while(cardDiscarded<0||cardDiscarded>cardsInHand);
              
              

               //Modifying the variable because this will be use for an array element
               cardDiscarded = cardDiscarded -1;
               //Discarding
               Hand.remove(cardDiscarded);

           }while (!(isTrue));// do loop


       }
       //Deals the cards that have been discarded  
       UsersHand();

   }

Note : Only provided part of the code as total code for the program is not available for execution.

Hope this helps you.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote