Home > Mobile >  Having trouble with figuring out how to create a nested loop with arrays
Having trouble with figuring out how to create a nested loop with arrays

Time:03-04

I'm currently having trouble defining the nested loop and arrays for the current problem:

Write a program that reads an integer, a list of words, and a character. The integer signifies how many words are in the list. The output of the program is every word in the list that contains the character at least once. For coding simplicity, follow each output word by a comma, even the last one. Add a new line to the end of the last output. Assume at least one word in the list will contain the given character. Assume that the list of words will always contain fewer than 20 words.

This is what I have so far

import java.util.Scanner;

public class labProgram {

 public static void main(String[] args) {
  Scanner scnr = new Scanner(System.in);
  String[] userList = new String[20];
  int numElements = scnr.nextInt();
  char userChar = scnr.next().charAt(0);

  int i;

  for(i = 0; i < userList.length();   i) {

     userList[i] = scnr.next();
  }
}

}

What steps should I take to define and loop this problem?

CodePudding user response:

You need to loop through each word by performing adding an inner for loop that iterates through each character in word[i]. Inside of this inner loop, you can check to see if the current character matches the target character that you want.

//loops through the list of words
for(int i = 0; i < userList.length; i  ){
    boolean letterExists = false;
// inner for loop will iterate through each character in wordlist[i]
  for(int j = 0; j < userList[i].length; j  ) {
      char currentLetter = userList.charAt(j);
      if(currentLetter ==  userChar)
         letterExists = true;
  }

// print if the user exists
     if(letterExists)
        System.out.println(userList[i]);
}

CodePudding user response:

You can exchange the order of some instruction to make the array as long as you wish.

Here an example:

import java.util.Scanner;

public class LabProgram { 

  public static void main(String[] args) {
    Scanner scnr = new Scanner(System.in);
    int numElements = scnr.nextInt();   // ask the user the elements of the array
    String[] userList = new String[numElements];    // create the array
    char userChar = scnr.next().charAt(0);  // ask the user for the char to search

    // adding the strings to the array
    for(int i = 0; i < userList.length;   i) {
        userList[i] = scnr.next();  
    }

    // searching the char in the strings and printing the matched strings
    System.out.println("Matched Strings: ");
    for (int i=0; i < userList.length; i  ) 
        for (int j=0; j < userList[i].length(); j  )
            if (Character.compare(userList[i].charAt(j), userChar) == 0) {
                System.out.print(userList[i]   ",");
                continue;
            }    
  }
}

Consider also to not use the instruction continue, you can use a new array to store the matched strings instead.

  • Related