What is the easiest way to find the number of characters in a sentence before nth word ?
For eg. :
String sentence = "Mary Jane and Peter Parker are friends."
int trigger = 5; //"Parker"
Output would be
Character count = 20
Any help will be appreciated.
Thanks.
Easiest way would just be to loop around the characters in the String and count the number of white-spaces.
The following increments a length variable for every character. When a white-space is encountered, we decrement the number of remaining white-spaces to read, and when that number reaches 1, it means we hit the wanted word, so we break out of the loop.
public static void main(String[] args) {
String sentence = "Mary Jane and Peter Parker are friends.";
int trigger = 5; //"Parker"
int length = 0;
for (char c : sentence.toCharArray()) {
if (trigger == 1) break;
if (c == ' ') trigger--;
length++;
}
System.out.println(length); // prints 20
}
public int getNumberOfCharacters(int nthWord){
String sentence = "Mary Jane and Peter Parker are friends.";
String[] wordArray = sentence.split(" ");
int count = 0;
for(int i=0; i<=nthWord-2 ; i++){
count = count + wordArray[i].length();
}
return count + (nthWord-1);
}`
try this it should work
Using regex can be done like this:
public static void main(String[] args) {
String sentence = "Mary Jane and Peter Parker are friends.";
int trigger = 5;
Pattern pattern = Pattern.compile(String.format("(?:\\S+\\s+){%d}(\\S+)", trigger - 1));
Matcher matcher = pattern.matcher(sentence);
matcher.find();
System.out.println(matcher.group().lastIndexOf(" ") + 1);
}
I am going through all the trouble of finding the exact work instead of simply indexOf("Parker") because of possible duplicates.
The regex will match N words without capturing and capture the N+1 word. In your case it will match all previous words up to the one you want and capture the next one.
Related
I've searched about everywhere but I just can't find anything very concrete. I've been working on this code for awhile now but it keeps stumping me.
public static void main(String[] args) {
System.out.println(palindrome("word"));
}
public static boolean palindrome(String myPString) {
Scanner in = new Scanner(System.in);
System.out.println("Enter a word:");
String word = in.nextLine();
String reverse = "";
int startIndex = 0;
int str = word.length() -1;
while(str >= 0) {
reverse = reverse + word.charAt(i);
}
}
There's a lot of ways to accomplish this using a while loop.
Thinking about simplicity, you can imagine how could you do this if you had a set of plastic separated character in a table in front of you.
Probably you'll think about get the second character and move it to the begin, then get the third and move to begin, and so on until reach the last one, right?
0123 1023 2103 3210
WORD -> OWRD -> ROWD -> DROW
So, you'll just need two code:
init a variable i with 1 (the first moved character)
while the value of i is smaller than total string size do
replace the string with
char at i plus
substring from 0 to i plus
substring from i+1 to end
increment i
print the string
The process should be:
o + w + rd
r + ow + d
d + row +
drow
Hope it helps
Here is an piece of code I write a while back that uses almost the same process. Hope it helps!
String original;
String reverse = "";
System.out.print("Enter a string: ");
original = input.nextLine();
for(int x = original.length(); x > 0; x--)
{
reverse += original.charAt(x - 1);
}
System.out.println("The reversed string is " +reverse);
My program currently only counts the number of capital letters in the whole string, not the ones after a period mark.
Desired output:
Enter essay:
I like Cats. Hey.
Sentences starting with capital letter: 2
Current output:
Enter essay:
I like Cats. Hey.
Sentences starting with capital letter: 3
Here's my code so far:
static int sentencesChecker(String shortEssay) {
int count = 0;
for (int i = 0; i < shortEssay.length(); i++) {
if (isUpperCase(shortEssay.charAt(i)) ) {
count++;
}
} System.out.println("Sentences starting with capital letter: " + count);
return count;
}
public static void main(String[] args) {
Scanner input = new Scanner (System.in);
System.out.println("Enter essay: ");
String essay = input.nextLine();
sentencesChecker(essay);
}
Some more easy way than counting over the char array of the String would probably be the usage of String#split:
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter essay: ");
String essay = input.nextLine();
String[] Uppcasesentences = essay.split("\\.\\s*[A-Z]");
if(Uppcasesentences[0].matches("^\\s*[A-Z].*")) {
System.out.println("You have got " + essay.split("\\.\\s*[A-Z]").length + " sentences starting uppercase");
}
else {
System.out.println("You have got " + (essay.split("\\.\\s*[A-Z]").length-1) + " sentences starting uppercase");
}
}
O/P
Enter essay:
Sentence 1. sentence 2. Sentence 3. Sentence 4. sentence 5. Sentence 6
You have got 4 sentences starting uppercase
What is happening here is, the regex splits the String on each occasion of a dot followed by 0-n whitespaces followed by an uppercase letter. The length of the array you did just created should equal the amount of sentences starting uppercase now.
Edit: the split did ignore the first sentence, and would produce 2 for the input sentence 2. Sentence 2. Checking if the first array element starts with uppercase now. If not subtract 1.
You can use a regular expression:
A regular expression, regex or regexp (sometimes called a rational expression)
is, in theoretical computer science and formal language theory, a sequence of characters that define a search pattern. Usually this pattern is then used by string searching algorithms for "find" or "find and replace" operations on strings.
The regular expression to use in this context is:
^[A-Z]|\. *[A-Z]
That regular expression means (underlined the portion described in the right):
^[A-Z]|\. *[A-Z] Any uppercase letter from A to Z at the starting of line
------
^[A-Z]|\. *[A-Z] or
-
^[A-Z]|\. *[A-Z] The character . followed by any number of
--------- spaces, followed by an uppercase letter in the range A to Z
This can be used as follow in java
public int countSentencesStartingWithUppercase(String line) {
String regexp = "^[A-Z]|\\. *[A-Z]"; // Note the additional \ this is
// done because \ is a special
// character in strings
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(line);
int count = 0;
while (matcher.find()) {
count++;
}
return count;
}
Here a link to the tutorial on regular expressions in java.
Here is the code.
String str ="I like Cats. Hey.";
//Scanner s = new Scanner(System.in);
//System.out.println("Would you like to play again?");
String[] strs = str.split("[.][ ]");
int count =0;
for(String string : strs){
if(Character.isUpperCase( str.charAt(0) )){
count++;
}
}
System.out.println("the count :"+count);
So I've been making a small piece of code in Java that takes input from the user counts the uppercase, lowercase and other parts (such as spaces, numbers, even brackets) and then returns how much there are of each to the user.
The problem I have is that say I put in "Hello There" it stops counting spots after the "o" in Hello. So after the first word.
Code
import java.util.Scanner;
public class Example {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int upper = 0;
int lower = 0;
int other = -1;
int total = 0;
String input;
System.out.println("Enter the phrase: ");
input = scan.next();
for (int i = 0; i < input.length(); i++) {
if (Character.isUpperCase(input.charAt(i))) upper++;
if (Character.isLowerCase(input.charAt(i))) lower++;
else other++;
total = upper + lower + other;
}
System.out.println("The total number of letters is " + total);
System.out.println("The number of upper case letters is " + upper);
System.out.println("The number of lower case letters is " + lower);
System.out.println("The number of other letters is " + other);
}
}
Scanner#next:
Finds and returns the next complete token from this scanner. A
complete token is preceded and followed by input that matches the
delimiter pattern.
The problem is that next doesn't see the word "There" since "Hello World" is not a complete token.
Change next to nextLine.
Advice: Use the debugger and you'll find the problem quickly, and when you have doubts refer to the docs, they're there for you.
Problem is that next() only returns the line before a space but nextLine() will read the whole line.
So Change
scan.next();
to
scan.nextLine();
You need to change next() to nextLine()- it will read all the line
As others have said. You should change from scn.next to scn.nextLine(). But why? This is because scn.next() only read until it encounters a space, and it stops reading. So whatever input after a space will not be read.
scn.nextLine() reads until a newline (i.e. enter) is encountered.
You can try with regular expressions:
public static void main(String[] args) {
String input = "Hello There";
int lowerCase = countMatches(Pattern.compile("[a-z]+"), input);
int upperCase = countMatches(Pattern.compile("[A-Z]+"), input);
int other = input.length() - lowerCase - upperCase;
System.out.printf("lowerCase:%s, upperCase:%s, other:%s%n", lowerCase, upperCase, other);
}
private static int countMatches(Pattern pattern, String input) {
Matcher matcher = pattern.matcher(input);
int count = 0;
while (matcher.find()) {
count++;
}
return count;
}
I'm trying to take a string:
String s = "This is a String!";
And return all 2-word pairs within that string. Namely:
{"this is", "is a", "a String"}
But right now, all I can get it to do is return:
{"this is", "a String"}
How can I define my while loop such that I can account for this lack of overlapping words? My code is as follows: (Really, I'd be happy with it just returning an int representing how many string subsets it found...)
int count = 0;
while(matcher.find()) {
count += 1;
}
Thanks all.
I like the two answers already posted, counting words and subtracting one, but if you just need a regex to find overlapping matches:
Pattern pattern = Pattern.compile('\\S+ \\S+');
Matcher matcher = pattern.matcher(inputString);
int matchCount = 0;
boolean found = matcher.find();
while (found) {
matchCount += 1;
// search starting after the last match began
found = matcher.find(matcher.start() + 1);
}
In reality, you'll need to be a little more clever than simply adding 1, since trying this on "the force" will match "he force" and then "e force". Of course, this is overkill for counting words, but this may prove useful if the regex is more complicated than that.
Run a for loop from i = 0 to the number of words - 2, then the words i and i+1 will make up a single 2-word string.
String[] splitString = string.split(" ");
for(int i = 0; i < splitString.length - 1; i++) {
System.out.println(splitString[i] + " " + splitString[i+1]);
}
The number of 2-word strings within a sentence is simply the number of words minus one.
int numOfWords = string.split(" ").length - 1;
Total pair count = Total number of words - 1
And you already know how to count total number of words.
I tried with group of pattern.
String s = "this is a String";
Pattern pat = Pattern.compile("([^ ]+)( )([^ ]+)");
Matcher mat = pat.matcher(s);
boolean check = mat.find();
while(check){
System.out.println(mat.group());
check = matPOS.find(mat.start(3));
}
from the pattern ([^ ]+)( )([^ ]+)
...........................|_______________|
..................................group(0)
..........................|([^ ]+)| <--group(1)
......................................|( )| <--group(2)
............................................|([^ ]+)| <--group(3)
I'm trying to write a program where a user would enter a phrase, and the program would count the blank spaces and tell the user how many are there. Using a for loop but i'm stuck, could someone help me out?
import java.util.Scanner;
public class Count
{
public static void main (String[] args)
{
String phrase; // a string of characters
int countBlank; // the number of blanks (spaces) in the phrase
int length; // the length of the phrase
char ch; // an individual character in the string
Scanner scan = new Scanner(System.in);
// Print a program header
System.out.println ();
System.out.println ("Character Counter");
System.out.println ();
// Read in a string and find its length
System.out.print ("Enter a sentence or phrase: ");
phrase = scan.nextLine();
length = phrase.length();
// Initialize counts
countBlank = 0;
// a for loop to go through the string character by character
for(ch=phrase.charAt()
// and count the blank spaces
// Print the results
System.out.println ();
System.out.println ("Number of blank spaces: " + countBlank);
System.out.println ();
}
}
The for loop for counting spaces would be written as follows:
for(int i=0; i<phrase.length(); i++) {
if(Character.isWhitespace(phrase.charAt(i))) {
countBlank++;
}
}
It reads as follows: “i is an index, ranging from the index of the first character to the index of the last one. For each character (gotten with phrase.charAt(i)), if it is whitespace (we use the Character.isWhitespace utility function here), then increment the countBlank variable.”
Just wondering, couldn't you just split the string entered by blank spaces and take the length of the array subtracted by 1?
In C# it would be as trivial as
string x = "Hello Bob Man";
int spaces = x.Split(' ').Length - 1;
Pretty sure java has a split? Works even if you have two contiguous spaces.
You have probably problem with that for each loop
char[] chars = phrase.toCharArray(); Change string into array of chars.
for(char c : phrase.toCharArray()) { //For each char in array
if(Character.isWhitespace(c) { //Check is white space.
countBlank++; //Increment counter by one.
}
}
or
for(int i =0; i <phrase.lenght(); i++) {
if(Character.isWhitespace(phrase.charAt(i)) { //Check is the character on position i in phrase is a white space.
countBlank++; //Increment counter by one.
}
}
You have to complete for cycle and count spaces
//replace this lines
for(ch=phrase.charAt()
// and count the blank spaces
//to this lines
for (int i = 0; i < phrase.length(); i++)
{
if(phrase.charAt(i) == ' ') countBlank++;
}
Loop through the characters in the string.
Check if the character is a space (char value = 32 or ch == ' ')
If space, add to countBlank, otherwise continue
Display the results.
You might look at the String and Character classes in the Java documentation for assistance.
I'm not very familiar with java, but if you can access each character in the string.
You could write something like this.
int nChars = phrase.length();
for (int i = 0; i < nChars; i++) {
if (phrase.charAt(i) == ' ') {
countBlank++;
}
}
This is at the following Java Tutorials
import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class SplitDemo2 {
private static final String REGEX = "\\d";
private static final String INPUT = "one9two4three7four1five";
public static void main(String[] args) {
Pattern p = Pattern.compile(REGEX);
String[] items = p.split(INPUT);
for(String s : items) {
System.out.println(s);
}
}
}
OUTPUT:
one
two
three
four
five
The regex for whitespace is \s
Hope that helps.