Prompt was to have user enter sentence and output should be the number of words, this code's correct but idk How "prevChar = currentChar"work?

Joined
Mar 8, 2024
Messages
1
Reaction score
0
public static void main(String[] args)
{
String sentence;
int wordCount = 0;
char prevChar = ' ';
char currentChar;
int index;

System.out.println("Enter a sentence: ");
sentence = EasyIn.getString();


for (index = 0; index < sentence.length(); index++)
{
currentChar = sentence.charAt(index);

if (Character.isLetter(currentChar) && !Character.isLetter(prevChar))
{
wordCount++;
}

prevChar = currentChar;
}

System.out.println("The number of words is " + wordCount + ".");


}
 
Joined
Jul 4, 2023
Messages
466
Reaction score
57
BTW you're code
Java:
public class Main
{
    public static void main(String[] args)
    {
        String sentence;
        int wordCount = 0;
        char prevChar = ' ';
        char currentChar;
        int index;
        
        //System.out.println("Enter a sentence: ");
        //sentence = EasyIn.getString();

        // for demonstration  purpose
        sentence = "Lorem ipsum dolor sit amet,  consectetur "; // output: 6
        sentence = sentence.trim();

        for (index = 0; index < sentence.length(); index++)
        {
            currentChar = sentence.charAt(index);
            
            if (Character.isLetter(currentChar) && !Character.isLetter(prevChar))
            {
                wordCount++;
            }
                
            prevChar = currentChar;
        }
        
        System.out.println("The number of words is " + wordCount + ".");
    }
}

different ways

[ working code on-line ]
Java:
public class Main {
    public static void main(String[] args) {
        String sentence = "Lorem ipsum dolor sit amet, consectetur ";

        // Remove any trailing punctuation at the end of the sentence
        sentence = sentence.replaceAll("[^a-zA-Z0-9]+$", "");

        // Split the sentence into an array of words using the split method
        String[] words = sentence.split("\\s+");

        // Count the number of words
        int wordCount = words.length;

        System.out.println("The number of words is " + wordCount + ".");
    }
}

[ working code on-line ]
Java:
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        String sentence = "Lorem ipsum dolor sit amet, consectetur ";

        // Create a Scanner object to scan words from the sentence
        Scanner scanner = new Scanner(sentence);

        // Count the number of words
        int wordCount = 0;
        while (scanner.hasNext()) {
            scanner.next(); // Move to the next word
            wordCount++;
        }

        System.out.println("The number of words is " + wordCount + ".");
        
        // Close the scanner to free resources
        scanner.close();
    }
}

[ working code on-line ]
Java:
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    public static void main(String[] args) {
        String sentence = "Lorem ipsum dolor sit amet, consectetur ";

        // Define a regular expression to match words
        Pattern wordPattern = Pattern.compile("\\b\\w+\\b");

        // Create a matcher for the input sentence
        Matcher matcher = wordPattern.matcher(sentence);

        // Count the number of words
        int wordCount = 0;
        while (matcher.find()) {
            wordCount++;
        }

        System.out.println("The number of words is " + wordCount + ".");
    }
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,930
Messages
2,570,072
Members
46,522
Latest member
Mad-Ram

Latest Threads

Top