How to check character values on a String array?

Mesut Berk Karataş

I was asked to write a Java program which gets 5 strings from user and collects it in an array. Furthermore, I need to check every single string in the array and only print the ones who has more than four characters.

public static void main(String[] args) {
    Scanner mbk = new Scanner(System.in);
    int repetition[] = { 1, 2, 3, 4, 5 };
    String words[] = new String[repetition.length];
    for (int i = 0; i < words.length; i++) {
        System.out.println("Please enter element " + repetition[i] + ":");
        words[i] = mbk.nextLine();
    }
}

This is the code that I have written and I'm stuck. Can anyone help?

Adil

First of all, It's a very bad practice to use variable names like mbk, the variable name should be easily understood by any other developer reading your code. It's name should reflect it's purpose. Here's a small revision of your code.

Also, Leaving a I/O stream open can cause you a big time of trouble, so i have also added the scanner.close statement.

public static void main(String[] args) throws Exception {
    Scanner userInput = new Scanner(System.in);
    int maxArrayLength[] = { 1, 2, 3, 4, 5 };
    String words[] = new String[maxArrayLength.length];
    for (int i = 0; i < words.length; i++) {
        System.out.println("Please enter element " + maxArrayLength[i] + ":");
            words[i] = userInput.nextLine();
        }

    for(int lengthCheckCounter = 0; lengthCheckCounter < words.length; lengthCheckCounter++)
        if(words[lengthCheckCounter].length() > 4)
            System.out.println(words[lengthCheckCounter] + "-" + words[lengthCheckCounter].length());
        userInput.close();
}

Another way of closing the I/O stream/resources could be to use try-with-resources block featured since JDK 1.7. Then we can write your code as follows:

try(Scanner userInput = new Scanner(System.in)) {
    // All other code
}

If you are on JDK 1.8, you can use a single line to do everything as suggested in Lonely Neuron's comment:

// Initialize your words array here
// Take user input
// Print words greater than 4 using JDK 1.8 Filter below
Arrays.stream(words).filter(s -> s.length() > 4).forEach(System.out::println)

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

how to convert string to array character

分類Dev

How to check whether the string has a starting character is special characters : javascript

分類Dev

Regexp - How to check whether a string starts OR ends with given character?

分類Dev

How to check array if it contains a string with same text?

分類Dev

Check if a string contains a certain character

分類Dev

Check for Unique character in a string (java)

分類Dev

How to get dynamic string values between specific character in PHP

分類Dev

How to check if a DB array matches any values of a given array

分類Dev

How to check if values in an array is exists in an array in jquery/javascript

分類Dev

How to put values of objects from an array into a string

分類Dev

In Swift, How to check if the first element of an Array is equal to a list of values?

分類Dev

Check a string in array

分類Dev

Average of occurrences of a character in a string array

分類Dev

Check character from input count occurance in String

分類Dev

How to check if byte array contents form a double number and not a String Java?

分類Dev

Check string contains multiple values

分類Dev

Check if the values are '|' separated in SQL string

分類Dev

Write a character array with null values into a file stream

分類Dev

Check if array has same values as other array

分類Dev

How do I ORDER BY a JSON Array with its String Values?

分類Dev

How to join an array of values by returning each value as a separate string?

分類Dev

How to check if variable is a string?

分類Dev

How to test if a string character is a digit?

分類Dev

How to search for a string with "+" character in sed?

分類Dev

How to add character amounts in an array?

分類Dev

How to check for a value in an array

分類Dev

How to check if character exists in DataFrame cell

分類Dev

How to check if a character is inside the scope of a comment in C

分類Dev

How to check duplicate results of a random character generator

Related 関連記事

  1. 1

    how to convert string to array character

  2. 2

    How to check whether the string has a starting character is special characters : javascript

  3. 3

    Regexp - How to check whether a string starts OR ends with given character?

  4. 4

    How to check array if it contains a string with same text?

  5. 5

    Check if a string contains a certain character

  6. 6

    Check for Unique character in a string (java)

  7. 7

    How to get dynamic string values between specific character in PHP

  8. 8

    How to check if a DB array matches any values of a given array

  9. 9

    How to check if values in an array is exists in an array in jquery/javascript

  10. 10

    How to put values of objects from an array into a string

  11. 11

    In Swift, How to check if the first element of an Array is equal to a list of values?

  12. 12

    Check a string in array

  13. 13

    Average of occurrences of a character in a string array

  14. 14

    Check character from input count occurance in String

  15. 15

    How to check if byte array contents form a double number and not a String Java?

  16. 16

    Check string contains multiple values

  17. 17

    Check if the values are '|' separated in SQL string

  18. 18

    Write a character array with null values into a file stream

  19. 19

    Check if array has same values as other array

  20. 20

    How do I ORDER BY a JSON Array with its String Values?

  21. 21

    How to join an array of values by returning each value as a separate string?

  22. 22

    How to check if variable is a string?

  23. 23

    How to test if a string character is a digit?

  24. 24

    How to search for a string with "+" character in sed?

  25. 25

    How to add character amounts in an array?

  26. 26

    How to check for a value in an array

  27. 27

    How to check if character exists in DataFrame cell

  28. 28

    How to check if a character is inside the scope of a comment in C

  29. 29

    How to check duplicate results of a random character generator

ホットタグ

アーカイブ