Word count java - java

So my code is supposed to return the amount of words (words being lengths of letters) and it works except for when i enter anything 0 or less can some pplease help spot the error??
Edited Code:
public class WordCount {
public static int countWords(String original, int minLength){
String[] s1 = original.split("\\s+");
for(int i = 0; i < s1.length; i++){
s1[i] = s1[i].replaceAll("^\\W]", "");
}
int count = 0;
for(int i = 0; i < s1.length; i++){
String str = s1[i];
int len = 0;
for(int x = 0; x < str.length(); x++){
char c = str.charAt(x);
if(Character.isLetter(c) == true){
len ++;
}
}
if(len >= minLength){
count ++;
}
}
return count;
}
public static void main(String[] args){
System.out.println("enter string: ");
String s = IO.readString();
System.out.println("min length: ");
int m = IO.readInt();
System.out.println(countWords(s, m));
}
}

I would apply a solution that uses a regular expression to process the text.
I prepared a sketch of code, which can be summarized as follows:
String[] words = myString.replaceAll("[^a-zA-Z ]", " ").split("\\s+");
What this code does is to:
replace whatever is not a letter (since you said just letters) with a space
split the results on the spaces
The resulting array words contains all the words (i.e., sequences of letters) which were contained in the original string.
A full example can be found here. In this example I just print the words as a list. In case you want just the count of words, you just have to return the count of elements in the array.

Try this :
String s = original.replaceAll("[\\W]", " ").replaceAll("[\\s]+", " ");
because you have to replace spaces more than 1 here as well.

Related

Java program to find the letter that appears in the most words?

I have a sentence, and I want to find the char that appears in the most words, and how many words it appears in.
For example: "I like visiting my friend Will, who lives in Orlando, Florida."
Which should output I 8.
This is my code:
char maxChar2 = '\0';
int maxCount2 = 1;
for (int j=0; j<strs2.length; j++) {
int charCount = 1;
char localChar = '\0';
for (int k=0; k<strs2[j].length(); k++) {
if (strs2[j].charAt(k) != ' ' && strs2[j].charAt(k) != maxChar2) {
for (int l=k+1; l<strs2[j].length(); l++) {
if (strs2[j].charAt(k)==strs2[j].charAt(l)) {
localChar = strs2[j].charAt(k);
charCount++;
}
}
}
}
if (charCount > maxCount2) {
maxCount2 = charCount;
maxChar2 = localChar;
}
}
, where strs2 is a String array.
My program is giving me O 79. Also, uppercase and lowercase do not matter and avoid all punctuation.
As a tip, try using more meaningful variable names and proper indentation. This will help a lot especially when your program is not doing what you thought it should do. Also starting smaller and writing some tests for it will help a bunch. Instead of a full sentence, get it working for 2 words, then 3 words, then a more elaborate sentence.
Rewriting your code to be a bit more readable:
// Where sentence is: "I like".split(" ");
private static void getMostFrequentLetter(String[] sentence) {
char mostFrequentLetter = '\0';
int mostFrequentLetterCount = 1;
for (String word : sentence) {
int charCount = 1;
char localChar = '\0';
for (int wordIndex = 0; wordIndex < word.length(); wordIndex++) {
char currentLetter = word.charAt(wordIndex);
if (currentLetter != ' ' && currentLetter != mostFrequentLetter) {
for (int l = wordIndex + 1; l < word.length(); l++) {
char nextLetter = word.charAt(l);
if (currentLetter == nextLetter) {
localChar = currentLetter;
charCount++;
}
}
}
}
if (charCount > mostFrequentLetterCount) {
mostFrequentLetterCount = charCount;
mostFrequentLetter = localChar;
}
}
}
Now all I did was rename your variables and change your for loop to a for-each loop. By doing this you can see more clearly your algorithm and what you're trying to do. Basically you're going through each word and comparing the current letter with the next letter to check for duplicates. If I run this with "I like" i should get i 2 but instead I get null char 1. You aren't properly comparing and saving common letters. This isn't giving you the answer, but I hope this makes it more clear what your code is doing so you can fix it.
Here is a somewhat more elegant solution
public static void FindMostPopularCharacter(String input)
{
String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
input = input.toUpperCase();
HashMap<Character, Integer> charData = new HashMap<>();
char occursTheMost = 'A'; //start with default most popular char
int maxCount = 0;
//create the map to store counts of all the chars seen
for(int i = 0; i < alphabet.length(); i++)
charData.put(alphabet.charAt(i), 0);
//first find the character to look for
for(int i = 0; i < input.length(); i++)
{
char c = input.charAt(i);
//if contained in our map increment its count
if(charData.containsKey(c))
charData.put(c, charData.get(c) + 1);
//check for a max count and set the values accordingly
if(charData.containsKey(c) && charData.get(c) > maxCount)
{
occursTheMost = c;
maxCount = charData.get(c);
}
}
//final step
//now split it up into words and search which contain our most popular character
String[] words = input.split(" ");
int wordCount = 0;
CharSequence charSequence;
for(Character character : charData.keySet())
{
int tempCount = 0;
charSequence = "" + character;
for(int i = 0; i < words.length; i++)
{
if(words[i].contains(charSequence))
tempCount++;
}
if(tempCount > wordCount)
{
occursTheMost = character;
wordCount = tempCount;
}
}
System.out.println(occursTheMost + " " + wordCount);
}
Output of
String input = "I like visiting my friend Will, who lives in Orlando, Florida.";
FindMostPopularCharacter(input);
is
I 8
Note: If there are ties this will only output the character that first reaches the maximum number of occurrences.
FindMostPopularCharacter("aabb aabb aabb bbaa");
Outputs
B 4
because B reaches the max first before A due to the last word in the input.
FindMostPopularCharacter("aab aab b")
B 3

How to sort integer array without losing location

I'm making a program which receives a string as input and returns the "sum" of the values for each letter of each word.
For example, my input of "Take advantage, do your best, don't stress.", would return:
do(19) take(37) dont(43) best(46) advantage(75) your(79) stress(100)
"do" would have a value of 19 because the letter "d" has a value of 4 (it is the fourth letter of the alphabet), and "o" has a value of 15, so the total is 19.
Now to store these values I have two arrays, one string array for each word, and one int array for the point value that they have. However, I only have this so far:
take(37) advantage(75) do(19) your(79) best(46) dont(53) stress(100)
As you can see, it is not sorted in ascending order as I am trying to do. I display these values like this:
System.out.print(words[j] + "(" + points[j] + ")" + " ");
where words is the String array and points is the int array. How can I sort them?
My current code:
public static void main (String[] args)
{
String input = "Take advantage, do your best, don't stress.";
String output = "";
//Get rid of all punctuation
for(int i = 0; i < input.length(); i++){
if( ( (int)input.charAt(i) >= 65 && (int)input.charAt(i) <= 90) || (int)input.charAt(i) == 32 || ( (int)input.charAt(i) >= 97 && (int)input.charAt(i) <= 122)){
//Handles Uppercase
if(input.charAt(i) >= 65 && input.charAt(i) <= 90){
int temp = (int)input.charAt(i) + 32;
char c = (char)temp;
output += c;
}
//Handles all other characters
else{
output += input.charAt(i);
}
}
}
//Done punctuation
String[] words = output.split(" ");
int[] points = new int[words.length];
//Points assignment
for(int j = 0; j < words.length; j++){
for(int k = 0; k < words[j].length(); k++){
points[j] += (int)words[j].charAt(k) - 96;
}
System.out.print(words[j] + "(" + points[j] + ")" + " ");
}
}
How about storing your results in a Map<String,Integer> instead of two lists:
Map myMap = new HashMap<String,Integer>;
From there you can sort the Map by its values: Sort a Map<Key, Value> by values (Java)
Next you can iterate through the sorted map:
for(String s : myMap.keySet()){
System.out.println(s+"("+myMap.get(s)+")");
}
If that is an option, your code can be made much simpler with Java 8.
First of all, removing punctuation can be done with a simple regular expression: you only want to keep letters, so we can just remove everything that is neither a letter nor a space. This is done by calling replaceAll("[^a-zA-Z\\s]", ""). After that, we can get a hold of all the words by splitting around "\\s+" (i.e. all whitespace characters).
Then, let's create a helper method returning the value of a String. As you have in your question, this would just be:
private static int value(String str) {
return str.chars().map(c -> c - 'a' + 1).sum();
}
Finally, we need to sort the words array with a comparator comparing the value of each word. The comparator is created with the help of Comparator.comparingInt(keyExtractor) where the key extraction would be a function returning the value of a word. In this case, it could be expressed a lambda expression: word -> value(word).
To have the final output, we need to transform the words array into a String where each word is concatenated with its value in parentheses. This is done by creating a Stream<String> of the words (Arrays.stream(array)), sorting it according the comparator above (sorted(comparator)), mapping each word to the result of concatenating its value to it and finally collecting that into a String delimited by a space (Collectors.joining(delimiter)).
Whole code would be:
public static void main(String[] args) {
String str = "Take advantage, do your best, don't stress.";
String[] words = str.toLowerCase().replaceAll("[^a-zA-Z\\s]", "").split("\\s+");
String output =
Arrays.stream(words)
.sorted(Comparator.comparingInt(w -> value(w)))
.map(w -> w + "(" + value(w) + ")")
.collect(Collectors.joining(" "));
System.out.println(output);
}
private static int value(String str) {
return str.chars().map(c -> c - 'a' + 1).sum();
}
Use any of sorting algorithm and do sorting for both arrays. For example:
public static void bubbleSort(int[] numArray, String[] words) {
int n = numArray.length;
int temp = 0;
String tt;
for (int i = 0; i < n; i++) {
for (int j = 1; j < (n - i); j++) {
if (numArray[j - 1] > numArray[j]) {
temp = numArray[j - 1];
tt=words[j-1];
numArray[j - 1] = numArray[j];
words[j-1]=words[j];
numArray[j] = temp;
words[j]=tt;
}
Then change last part of your main function to look like this:
String[] words = output.split(" ");
int[] points = new int[words.length];
//Points assignment
for(int j = 0; j < words.length; j++){
for(int k = 0; k < words[j].length(); k++){
points[j] += (int)words[j].charAt(k) - 96;
}
}
bubbleSort(points,words);
for(int j = 0; j < words.length; j++){
System.out.print(words[j] + "(" + points[j] + ")" + " ");
}
If you are not allowed to use Java 8 (else use #Tunaki's approach), create a Comparable object that keeps two values, a String (word) and an int (sum). Then, just add each word to a list and sort it using Collections.sort(yourList).
public class Word implements Comparable<Word>{
private String word;
private int sum;
public Word(String word) {
this.word = word;
setSum();
}
private void setSum() {
//your sum function, I just copy and paste it from your post
for(int k = 0; k < word.length(); k++)
sum += (int)word.charAt(k) - 96;
}
public String getWord() {
return word;
}
public int getSum() {
return sum;
}
#Override
public int compareTo(Word o) {
return this.sum > o.sum ? 1 : -1;
}
}

Find the word with the greatest numbers of repeated letters

For this challenge I need to find the word with the greatest numbers of repeated letters. For example, if I enter Hello world! the output should be Hello as it contains 2 characters of l, or No words and it should be -1.
I broke down the problem into:
1) Broke a sentence into the array of words
2) Went through each word in a loop
3) Went through each charcater in a loop
I'm stuck in how I should return if a word contains more letters than any other.
public static void main(String[] args) {
Scanner kbd = new Scanner(System.in);
System.out.println("Enter any sentence or word combination: ");
String myString = kbd.nextLine();
String result = "";
int count = 0;
String[] words = myString.split("\\s+");
for(int i = 0; i < words.length; i++) {
for(int j = 0; j < words[i].length(); j++) {
for(int k = 1; k < words[i].length(); k++) {
char temp = words[i].charAt(k);
if(temp == words[i].charAt(k-1)) {
count++;
}
}
}
}
}
You almost did it and I suppose you're looking into something like this:
static int mostFreqCharCount(final String word) {
final int chars[] = new int[256];
int max = 0;
for (final char c : word.toCharArray()) {
chars[c]++;
if (chars[c] > chars[max]) // find most repetitive symbol in word
max = c;
}
return chars[max];
}
public static void main(final String[] args) {
System.out.println("Enter any sentence or word combination: ");
final Scanner kbd = new Scanner(System.in);
final String myString = kbd.nextLine();
kbd.close();
int maxC = 0;
String result = "";
final String[] words = myString.split("\\s+");
for (final String word : words) {
final int c = mostFreqCharCount(word);
if (c > maxC) {
maxC = c;
result = word;
}
}
if (maxC > 1) // any word has at least 1 symbol, so we should return only 2+
System.out.println(result);
}
the main idea - calculate number of most frequent symbol for each word and store only maximal one in variables maxC and result
You'll want to create an array of length = words.length, and store the highest value for each word in its relative index:
int counts[] = new int[words.length];
for(int i = 0; i < words.length; i++) {
for(int j = 0; j < words[i].length(); j++){
count = 0
for(int k = k+1; k < words[i].length(); k++){
if(words[i].charAt(j) == words[i].charAt(k)){
count++;
}
if(counts[i] < count)
counts[i] = count;
}
}
Then just scan through the array for the highest value, n, and return words[n]
If you just need the ONE word with the greatest count, you only need three variables, one for currentBestWord, one for currentLargestCount, and one to keep the count of characters in a word.
int currentLargestCount=0;
String currentBestWord="";
HashMap<String,Integer> characterCount=new HashMap<String,Integer>();
String[] words=myString.split("\\s+");
for(int i=0;i<words.length;i++){
String w=words[i];
characterCount=new HashMap<String,Integer>();
for(int j=0;j<w.length();j++){
String character=w.charAt(j).toString();
if(characterCount.containsKey(character)){
characterCount.put(character,characterCount.get(character)+1);
}else{
characterCount.put(character,1);
}
}
// Now get the largest count of this word
Iterator ir=characterCount.ValueSet().iterator();
int thiscount=0;
while(ir.hasNext()){
int thischaractercount=ir.next();
if(thiscount<thischaractercount) thiscount=thischaractercount;
}
if(thiscount>currentLargestCount){
currentLargestCount=thiscount;
currentBestWord=w;
}
}

Cannot compare Chars in java, code running but results don't come

/* Program to search a character in a sentence and tell the occurrence of
that character in that sentence and also prints all the indexes. */
package com.shobhit.string1;
import java.util.Scanner;
public class StringSearch1
{
public static void main(String args[])
{
String sentence;
char c; // takes first character of input
char duplicate; // finds duplicates of c in code
int length; // find sentence length
int index; // index of character in sentence
int numberOfOccurrence = 0;
Scanner in = new Scanner(System.in);
System.out.println("Enter a sentence:");
sentence = in.nextLine();
System.out.println("Enter a character you want to find in the sentence:");
c = in.next().charAt(0);
length = sentence.length();
System.out.println("The indexes of the character are:");
for (int i = 0; i >= length - 1; i++)
{
duplicate = sentence.charAt(i);
if (c == duplicate)
{
numberOfOccurrence = numberOfOccurrence + 1;
index = sentence.indexOf(c);
System.out.println("Index is:" + index);
}
}
System.out.println("The number of times " + c + " has occured is:"
+ numberOfOccurrence);
}
}
Unexpected output:
Enter a sentence:
I don't want to type another long sentence
Enter a character you want to find in the sentence:
t
The indexes of the character are:
The number of times t has occured is:0
You want this look like this
for(int i = 0 ; i < length; i++)
instead of
for(int i = 0 ; i >= length -1; i++)
because it is never going to be iterate for length > 1 cases
Your condition for the "for" loop is wrong. It should be i < length or i < length - 1
for(int i = 0 ; i>=length -1; i++)
Can you try
for(int i = 0 ; i<=length -1; i++)

Java: Creating a Delimiter Method that has Input/Output

I'm working on a study problem from class and essentially it reads a string, and a character. The character is the delimiter. It will then search the string for the delimiter and create an array in equal length to the number of times the delimiter is found. It then assigns each character or string to its own spot in the array and returns it.
Maybe I am over thinking things, but the just of it is to not rely on the various string methods and to sort of create your own. How can I get this method to only assign the string/char found in the one that is read to one position in the array and not all as well as stop it from adding unnecessary output? Help/Suggestions greatly appreciated
public static String[] explode(String s, char d){
String []c;
int count = 1;
//checks to see how many times the delimter appears in the string and creates an array of corresponding size
for(int i = 0; i < s.length(); i++){
if(d == s.charAt(i))
count ++;
}
c = new String [count];
//used for checking to make sure the correct number of elements are found
System.out.println(c.length);
//goes through the the input string "s" and checks to see if the delimiter is found
//when it is found it makes c[j] equal to what is found
//once it has cycled through the length of "s" and filled each element for c, it returns the array
for(int i = 0; i < s.length(); i++){
for(int j = 0; j < c.length; j++){
if(d == s.charAt(i))
c[j] += s.substring(i-1);
}
}
//provides output for the array [c] just to verify what was found
for(int y = 0; y < c.length; y++)
System.out.println(c[y]);
return c;
}
public static void main(String [] args){
String test = "a,b,c,d";
char key = ',';
explode(test,key);
}
^The following will output:
4
nulla,b,c,db,c,dc,d
nulla,b,c,db,c,dc,d
nulla,b,c,db,c,dc,d
nulla,b,c,db,c,dc,d
I'm aiming for:
4
a
b
c
d
Thank you
Perhaps you could try something like this:
public static void main(String[] args){
explode("a,b,c,d", ',');
}
public static void explode(final String string, final char delimiter){
int length = 1;
for(final char c : string.toCharArray())
if(delimiter == c)
length++;
if(length == 1)
return;
final String[] array = new String[length];
int index, prev = 0, i = 0;
while((index = string.indexOf(delimiter, prev)) > -1){
array[i++] = string.substring(prev, index);
prev = index+1;
}
array[i] = string.substring(prev);
System.out.println(length);
for(final String s : array)
System.out.println(s);
}
Output of the program above is:
4
a
b
c
d
Or if you want to utilize a List<String> instead (and Java 8), you could remove a couple of lines by doing something like this:
public static void explode(final String string, final char delimiter){
final List<String> list = new LinkedList<>();
int index, prev = 0;
while((index = string.indexOf(delimiter, prev)) > -1){
list.add(string.substring(prev, index));
prev = index+1;
}
list.add(string.substring(prev));
System.out.println(list.size());
list.forEach(System.out::println);
}

Categories