Question

(Java) how can i remove a element of a array if it contains a specific word?...

(Java)
how can i remove a element of a array if it contains a specific word?
—> ( ex: word= hi) , so any element that has “hi” in it will be removed.
and then recreate the array with the remaining elements?

0 0
Add a comment Improve this question Transcribed image text
Answer #1
import java.util.Scanner;

public class RemoveFromArray {
    public static void main(String[] args) {
        int size;
        Scanner scanner = new Scanner(System.in);

        // Reading size of array
        System.out.print("Enter size of array: ");
        size = scanner.nextInt();

        // Reading strings for array
        String[] array = new String[size];
        System.out.println("Enter "+size+" words for array:");
        for(int i = 0;i<size;i++){
            array[i] = scanner.next();
        }

        // Reading key to remove from array
        System.out.print("Enter word to remove: ");
        String key = scanner.next();

        // Creating new array to store strings that does not contains key
        int count = 0;
        String[] result = new String[size];

        // Storing stings that does not contan key to array result
        for(int i = 0;i<size;i++){
            if(!array[i].contains(key)){
                result[count++] = array[i];
            }
        }

        // Recreating the array
        array = new String[count];
        for(int i = 0;i<count;i++){
            array[i] = result[i];
        }

        // Printing the contents of array
        System.out.println("Content of array after removal:");
        for(int i = 0;i<count;i++){
            System.out.println(array[i]);
        }
    }
}

Add a comment
Know the answer?
Add Answer to:
(Java) how can i remove a element of a array if it contains a specific word?...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • How do remove an object from list and add to another with indexes (java) Ex: I...

    How do remove an object from list and add to another with indexes (java) Ex: I have two separate list classes, Dogs and WalkedDogs, so once a dog from the Dog list has been walked, I want to remove that dog from the Dog list and at the same time then add it to the WalkedDogs list. By doing so via ui, so when main runs, the user is shown a list of Dogs, and chooses a dog with index...

  • How I can remove a specific item from array stored in localstorage and update the page...

    How I can remove a specific item from array stored in localstorage and update the page immediately!

  • the programming language is in java Problem 2 You are given an array A with n...

    the programming language is in java Problem 2 You are given an array A with n distinct elements. Implement an (n log n)-time algorithm that creates an array B where all elements are in range from 0 to n - 1 and where the order of elements is the same as in A. That is, 0 has the same index in B as the smallest element in A, 1 has the same index in B as the second smallest element...

  • Purpose: The JAVA application will allow the student to; I. demonstrate the declaration of an array;...

    Purpose: The JAVA application will allow the student to; I. demonstrate the declaration of an array; II. demonstrate the initialization of the array; III demonstrate the printing of the elements of the array. Create a new file called "Array5A” . 1. Prepare a document box (tell me that task(s) the application is to accomplish, how it will accomplish the tasks, the author of the application, the date of completion, and any additional notes.) 2. Write a Java statement to create...

  • (C++) Write a function, remove, that takes three parameters: an array of integers, the number of...

    (C++) Write a function, remove, that takes three parameters: an array of integers, the number of elements in the array, and an integer (say, removeItem). The function should find and delete the first occurrence of removeItem in the array. (Note that after deleting the element, the number of elements in the array is reduced by 1.) Assume that the array is unsorted. Also, write a program to test the function. Your program should prompt the user to enter 10 digits...

  • can someone please help me with this. I need to use java. Recursion Write and run...

    can someone please help me with this. I need to use java. Recursion Write and run a program that reads in a set of numbers and stores them in a sequential array. It then calls a recursive function to sort the elements of the array in ascending order. When the sorting is done, the main function prints out the elements of the newly sorted array Hint: How do you sort an array recursively? Place the smallest element of the array...

  • JAVA QUESTION I have an array String array [] = {"101", "010", 111"} I have an...

    JAVA QUESTION I have an array String array [] = {"101", "010", 111"} I have an input of "000" that I want to compare to each value in the array and count how many times they Differ. So String input = "000" CODE A FUNCTION THAT ACCEPTS THE ARRAY AND THE INPUT VARIABLE AS PARAMETERS. IN THIS FUNCTION, CODE A FOR LOOP THAT COMPARES THE String input to each element of the array ,COUNTS how many time they differ for...

  • Write a java program to remove duplicate elements from an array. Do not use Java library...

    Write a java program to remove duplicate elements from an array. Do not use Java library utilities or 3rd party tools to solve this problem. You need to use the “for loop” and manage the index as you traverse thru the array [i ] while looking for duplicates. For example: Example 1 --------------------- Original Array: 105 123 -921 -1 123 8 8 8 -921 Array with unique values: 105 123 -921 -1 8 Example 2 --------------------------- Original Array: 10 22...

  • Use JAVA language. public class DynamicArray2 { private String[] data; // the backing array private int...

    Use JAVA language. public class DynamicArray2 { private String[] data; // the backing array private int virtualArrayLength; // the number of elements in the dynamic array // Throws an IndexOutOfBoundsException if i is not a valid index // for adding to the dynamic array, otherwise inserts s at index i. // Elements can be added from index 0 to this.size(). public void add(int i, String s) { // If there is no room for s in data, create a new...

  • Our 1st new array operation/method is remove. Implement as follows: public static boolean remove( int[] arr,...

    Our 1st new array operation/method is remove. Implement as follows: public static boolean remove( int[] arr, int count, int key ) { 1: find the index of the first occurance of key. By first occurance we mean lowest index that contains this value. hint: copy the indexOf() method from Lab#3 into the bottom of this project file and call it from inside this remove method. The you will have the index of the value to remove from the array 2:...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT