1

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?

5
  • I'm stuck stuck with what? what is the problem? Commented Apr 22, 2018 at 5:15
  • 4
    You need to do another loop, an if check for the length and another System.out.println basically you need to do more of what you have done already. NOTE: You don't need an array for repetition Commented Apr 22, 2018 at 5:16
  • 1
    Welcome to SO. Tidy code, nice. I just have one suggestion: we have left the age in which we use weird abbreviations to name our variables. Your code should immediately be readable by other developers. So what does mbk mean? Maybe you can replace it with an expressive name? Commented Apr 22, 2018 at 5:22
  • Arrays.stream(words).filter(s -> s.length() > 4).forEach(System.out::println) :o) Commented Apr 22, 2018 at 5:27
  • Thank you so much! And thanks again for the heads up about the mbk stuff, it is the initials of my name so I wasn't able to notice the difficulty for you guys. I was able to finish the code where I was stucked. Thanks again for your help! Commented Apr 22, 2018 at 5:39

5 Answers 5

1

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)
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for your wise help, I was able to finish the code. I will consider your suggestions in my further projects.
Happy to help :)
The scanner.close() here is wrong, since you didn't open the scanner. System.in was opened for you (by the operating system), and it isn't your responsibility to close it. If you had opened the scanner on a file, for example, then that would be different.
0

Yo, I gotchu:

this goes through the entire list, and prints anything that has a greater length than 4.

for (int i = 0; i < yourArray.length; i++) {
    if (yourArray[i].length() > 4) {
         System.out.println(yourArray[i]);
    }
}

4 Comments

Thank you so much! Your code solved the exact problem that I needed to solve.
@Mesut Berk Karataş did my comment answer the question, or the one above me?
I saw your comment first and it has worked, I tried the comment above as well.
Ah, ok. Happy to help.
0

Use the length() method

for(String s: words){
    if(s.length()>=4){
        System.out.println(s);
    }
 }

2 Comments

Thank you I was able to finish it at last!
Glad it helped :)
0

words[j].length() will give you the length of the string in index j of your array. I am leaving you to compare it to 4, And to introduce a variable j (you may call it i again or any other name you find fitting).

Comments

0
import java.util.Scanner;

public class StackOf4 {

    public static void main(String[] args) {

        Scanner scn=new Scanner(System.in);

        String[] names=new String[5];


        for(int i=0;i<5;i++) {


            System.out.println("Enter a string:");
            names[i]=scn.nextLine();

        }

        System.out.println("The strings with more than 4 characters are: ");

        for(int i=0;i<5;i++) {

            if(names[i].length()>4)
            System.out.println(names[i]);

        }


    }


}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.