Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Filter String array

  1. #1
    Junior Member
    Join Date
    Sep 2022
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Filter String array

    Implement console program which will meet the following requirements:

    Program starts and asks user to enter random words separated by space

    Program asks user to enter minimum length of string to filter words which were entered

    Program creates array object from entered words

    Program calls specific method which takes String[] as a parameter and returns array of strings which contains words that have length more or equal to value specified by user

    Method should look like this:
    public static String[] filterWordsByLength(int minLength, String[] words) {

    <write your code here>
    }


    c. Program prints filtered array to the console output.



    guys above is the requirment that asked to me to write the code.. below i attached the image code that show me an error which is not satisfy the requirment.

    //Error//

    https://ibb.co/5r5hCnF

    //Error//

    below i attached the code..



    //CODE//


    import java.util.*; public class FilterStringArray {

    public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.print("Please, enter any words separated by space: ");
    String userInput = sc.nextLine();
    System.out.print("Please, enter minumum word length to filter words: ");
    int minLength = sc.nextInt();

    String[] words = userInput.split("\\s+");
    String[] filteredWords = filterWordsByLength(minLength, words);
    System.out.println(Arrays.toString(filteredWords)) ;
    }



    public static String[] filterWordsByLength(int minLength, String[] words) {
    String[] filter = new String[words.length];

    for (int i=0; i < words.length ;i++ )
    {
    filter[i] = words[i].substring(0,minLength);
    }

    return filter;
    }


    //CODE//
    Last edited by ravi rox; September 13th, 2022 at 06:48 AM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Filter String array

    Please copy the full text of any error messages and paste it here.

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Sep 2022
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Filter String array

    done, i attched the image link which is shows the error in a clear point of view

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Filter String array

    Please copy the full text of any error messages and paste it here. No images. Text can not be copied from an image to include in a response.

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.

    The results should look like this:
      public class SomeClass {
          int varX;
      ...
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 3
    Last Post: October 17th, 2020, 07:28 PM
  2. [SOLVED] Problem with string split method to string array
    By Helplessdrowningpuppy in forum What's Wrong With My Code?
    Replies: 2
    Last Post: September 21st, 2014, 04:16 PM
  3. Obtain string from certain index in a string array
    By MartialLaw in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 16th, 2013, 08:55 PM
  4. Testing if an inputted String exists in an String Array
    By djl1990 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 29th, 2012, 03:46 PM
  5. [SOLVED] Sorting an object array using string variables from a string array
    By Shadud in forum What's Wrong With My Code?
    Replies: 0
    Last Post: November 26th, 2012, 05:50 PM