count word in string no matter the delimiter java
Java
public static void main(String[] args)
{
//Scanner object instantiation
Scanner dude = new Scanner(System.in);
//variable declaration
String string1 = "";
int count = 0;
boolean isWord = false;
//user prompt and input
System.out.println("Enter in your string");
string1 = dude.nextLine();
int endOfLine = string1.length()-1;
char ch [] = string1.toCharArray();
for (int i = 0; i < string1.length(); i++)
{
if(Character.isLetter(ch[i]) && i != endOfLine)
{//if character is letter and not end of line
isWord = true; //it is part of a word
}
if (!Character.isLetter(ch[i]) && isWord)
{ //if character is not a letter, and previous
//character is a letter i.e. non-letter is
//preceded by character
count++; //add to word count
isWord = false; //get ready to detect new word
}
if (Character.isLetter(ch[i]) && i == endOfLine)
{ //if character is letter
//and at end of line
count++; //add to word count
isWord = false;
}
}
System.out.println("There are " +count+ " words");
}
Also in Java:
- Title
- spring framework iterate
- Category
- Java
- Title
- java cannot find file path
- Category
- Java
- Title
- java remove duplicates
- Category
- Java
- Title
- java last element in array
- Category
- Java
- Title
- generic method lambda java
- Category
- Java
- Title
- a recursive function that calculates the greatest common divisor from user's input in java
- Category
- Java
- Title
- exponents java
- Category
- Java
- Title
- processing library cassette
- Category
- Java
- Title
- room insert and return id
- Category
- Java
- Title
- output statement java
- Category
- Java
- Title
- java remove first character from string
- Category
- Java
- Title
- Spring Security JWT Authentication + PostgreSQL – RestAPIs SpringBoot + Spring MVC + Spring JPA
- Category
- Java
- Title
- how to make a char uppercase in java
- Category
- Java
- Title
- rewrite file java
- Category
- Java
- Title
- convert string to mayus java
- Category
- Java
- Title
- java letter alphabet index
- Category
- Java
- Title
- get day name from date in java
- Category
- Java
- Title
- java stream sort
- Category
- Java
- Title
- array null pointer java
- Category
- Java
- Title
- computeifabsent hashmap java
- Category
- Java
- Title
- java jaxb unmarshall xml to map
- Category
- Java
- Title
- how to do for each in java
- Category
- Java
- Title
- how to add java_home in mac
- Category
- Java
- Title
- processing play sound
- Category
- Java
- Title
- java packages example
- Category
- Java
- Title
- java optional to collection
- Category
- Java
- Title
- convert date to calendar java
- Category
- Java
- Title
- how do you make a method that returns an array java
- Category
- Java
- Title
- sieve in java
- Category
- Java
- Title
- java clone method
- Category
- Java
- Title
- java get input
- Category
- Java
- Title
- how to use spring in java
- Category
- Java
- Title
- remove action bar android studio
- Category
- Java
- Title
- how to remove all whitespace from string java
- Category
- Java
- Title
- how to open a webpage with java
- Category
- Java
- Title
- if statement java
- Category
- Java
- Title
- read integer input java
- Category
- Java
- Title
- annotation spring notnull
- Category
- Java
- Title
- get host from request object java
- Category
- Java
- Title
- socket in java
- Category
- Java
- Title
- java scanner input int
- Category
- Java
- Title
- java jagged array days and months
- Category
- Java
- Title
- compare list from db and list from request
- Category
- Java
- Title
- how to get all the names of the files in a folder in java?
- Category
- Java
- Title
- java 8 find min value in array
- Category
- Java
- Title
- properties java 8 maven in pom xml
- Category
- Java
- Title
- android foreground push notification
- Category
- Java
- Title
- download spring
- Category
- Java
- Title
- java connect mariadb
- Category
- Java
- Title
- file to image javafx
- Category
- Java
- Title
- java d'intervalle de resultat
- Category
- Java
- Title
- parseints(str) java
- Category
- Java
- Title
- gradle require java version
- Category
- Java
- Title
- android java retrofit offline cache
- Category
- Java
- Title
- hibernate onetone with mapsid
- Category
- Java
- Title
- select photo from camera android
- Category
- Java
- Title
- how to fill a list with a single value java
- Category
- Java
- Title
- shorthand if java without else
- Category
- Java
- Title
- split method in java
- Category
- Java
- Title
- java nested loop
- Category
- Java
- Title
- binary to octal conversion java program
- Category
- Java
- Title
- java print treemap
- Category
- Java
- Title
- Unrecognized option: --version Error: Could not create the Java Virtual Machine. Error: A fatal exception has occurred. Program will exit.
- Category
- Java
- Title
- java script dynamic varibale creation
- Category
- Java
- Title
- editer un label java
- Category
- Java
- Title
- how do you concatenate an int with a string in java
- Category
- Java
- Title
- java set value of arraylist
- Category
- Java
- Title
- kotlin jsonobject get nested
- Category
- Java
- Title
- java get keys from hashmap
- Category
- Java
- Title
- center textview programmatically android
- Category
- Java
- Title
- create color object java
- Category
- Java
- Title
- fibonacci sequence in java recursion
- Category
- Java
- Title
- getindexrequest elasticsearch
- Category
- Java
- Title
- how to remove spaces from an array in java
- Category
- Java
- Title
- java script to detect and launch all browsers
- Category
- Java
- Title
- java merge sort
- Category
- Java
- Title
- java scanner next()
- Category
- Java
- Title
- java create map
- Category
- Java
- Title
- check if optional is empty java
- Category
- Java
- Title
- arraylist remove method java
- Category
- Java
- Title
- convert string to localdatetime
- Category
- Java
- Title
- java read lines from file
- Category
- Java
- Title
- java previous permutation
- Category
- Java
- Title
- difference between java and javax
- Category
- Java
- Title
- how to draw a line in java
- Category
- Java
- Title
- diagonal difference hackerrank solution in java 8 using list
- Category
- Java
- Title
- one line if statement java
- Category
- Java
- Title
- java round double to 2 decimal places
- Category
- Java
- Title
- create object of static class in java
- Category
- Java
- Title
- how to add a number to an array in java
- Category
- Java
- Title
- linux pyspark select java version
- Category
- Java
- Title
- how to count an replace substring string in java
- Category
- Java
- Title
- java while loop break
- Category
- Java
- Title
- java recursion
- Category
- Java
- Title
- print map java
- Category
- Java
- Title
- difference between print and println in java
- Category
- Java
- Title
- java thread
- Category
- Java
- Title
- how to get a user input in java
- Category
- Java
- Title
- java read from connection even if 404
- Category
- Java
- Title
- list in java
- Category
- Java