mercredi 5 décembre 2018

I am unable to delete elements from within my array when i ask for user input

When i ask the user to input a number to delete from the array it simply puts out 0 and than asks to try again i want the number to be deleted completely until the array is empty here is the code i have so far:

import java.util.Scanner;
import java.util.Random;
public class DeleteElements
{
public static void main(String[]args)
{
     Scanner keyboard = new Scanner(System.in);
     int arr[] = new int[20];
     int num, found = 0,
     arrSize = 10;
     String choice;

     Random randomGenerator = new Random();
     for (int i = 0; i<10; i++)
     {
         arr[i] = randomGenerator.nextInt(100);
     }  

     for(int i = 0; i<10; i++)
     {
        System.out.print("" + arr[i] + " "); 
     }

     do 
     {
         System.out.print("Number to Delete: ");
         num = Integer.parseInt(keyboard.nextLine());

         if(arrSize <=0)
         {
           System.out.println("The array is now empty");
           break;
         }
         else
         {
             for (int i = 0; i<10; i++)
             {
                 if(arr[i] == num)  
                 {
                   found = 1;  
                 }


             if (found == 1)
              arr[i] = arr[i + 1];
            }
                if (found == 0)

                System.out.println("Number not found,");

            else
            {
                arrSize--;
                int i = 0;
                for ( i = 0; i <arrSize; i++);
                {
                    System.out.print("" + arr[i] + " ");
                }
                found = 0;
            }
            System.out.println(" Try again (y/n) ? ");
            choice = keyboard.nextLine();
             }

     }while (choice.charAt(0) == 'y' || choice.charAt(0) == 'Y');
}
}

i want it to look something like this: Array: 3, 63, 45 Delete NUmber: "User inputs 45" Array: 3, 63




Aucun commentaire:

Enregistrer un commentaire