Java Lab: Word Frequencies

preview_player
Показать описание
----------------------------------------------------------------------------------------
Social media:
----------------------------------------------------------------------------------------

Code in comments
Рекомендации по теме
Комментарии
Автор

Thank you so much for the explanations in the comments. They have really helped me understand better

mayamjjohnson
Автор

import java.util.Scanner;

public class WordFrequencies {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);

int numElements = scnr.nextInt(); // The input begins with an integer indicating the number of words that follow
String strArray[] = new String[numElements]; // Then gets stored into the number of elements for strArray

for (int i = 0; i < numElements; i++) { // for the amount of elements there are, keep iterating that many times
// until the user types in a string that number of times.
strArray[i] = scnr.next();
}

// Now that the for loop is over, we go down to here (the user has already typed in the words).
int countArray[] = new int[numElements]; // the number of elements gets stored into countArray[]
int count; // We specify count as an integer because you will see why in the below code.

for (int i = 0; i < numElements; i++) { // Keeps iterating based on the number of elements.
count = 0; // initializing count to 0 because that is where we want to start from.
// It's a nested for loop so it starts at index i = 0 and then j = 0.
// This is crazy to think about but just look at the code, the bottom example, and use your head and imagine.
// All this nested for loop is doing is going word for word and checking which ones equal each other, when they do,
// count goes up +1.
for (int j = 0; j < countArray.length; j++) { // countArray = numElements
// We are trying to store the number of times a word has been repeated
if { // To check each word at index I with words at index J
count++; // Increments count by one if the if statement evaluates to true.
}

countArray[i] = count; // When for loop is done this code gets read.
// count;
// store the count (number of repeated words) in countArray at the position of the word in the index
// (i where it is being checked)
}
}
/*
Example: for the above nested for loop.

first step: when i == 0



strArray[0] == strArray[0]
hey == hey => count++ => count = 1

strArray[0] == strArray[1]
hey == hi => no

strArray[0] == strArray[2]
hey == Mark => no

strArray[0] == strArray[3]
hey == hi => no

strArray[0] == strArray[4]
hey == mark => no

countArray[i] = 1



second step: when i == 1



strArray[1] == strArray[0]

hi == hey => no

strArray[1] == strArray[1]

hi == hi => count++ => count = 1

strArray[1] == strArray[2]

hi == Mark = > no

strArray[1] == strArray[3]

hi == hi => count++ => count = 2

strArray[1] == strArray[4]

hi == mark => no

countArray[i] = 2



third step: when i == 2



strArray[2] == strArray[ 0 ]

Mark == hey => no

strArray[2] == strArray[1]

Mark == hi => no

strArray[2] == strArray[2]

Mark == Mark => count++ => count = 1

strArray[2] == strArray[3]

Mark == hi => no

strArray[2] == strArray[4]

Mark == mark => no

countArray[i] = 1



four step: when i == 3



strArray[3] == strArray[0]

hi == hey => no

strArray[3] == strArray[1]

hi == hi => count++ => count = 1

strArray[ 3] == strArray[ 2 ]

hi == Mark = > no

strArray[3] == strArray[3]

hi == hi => count++ => count = 2

strArray[3] == strArray[4]

hi == mark => no

countArray[i] = 2



five step: when i == 4



strArray[4] == strArray[0]

mark == hey => no

strArray[4] == strArray[1]

mark == hi => count++ => count = 1

strArray[4] == strArray[2]

mark == Mark = > no

strArray[4] == strArray[3]

mark == hi => no

strArray[4] == strArray[4]

mark == mark => count++ => count = 1

countArray[i] = 1
*/

for (int i = 0; i < strArray.length; i++) { // for loop to print the words and their repetition
+ " " + countArray[i]);
}
scnr.close(); // finally close the scanner object scnr
}
}

bassboostey
visit shbcf.ru