Jump to content

{Java} Counting how many words are in a sentence?

creativename
Go to solution Solved by Mefoga,

public class sentence{

public static void main(String args[]){

System.out.print("input a sentence: ");

String line;

EasyReader input = new EasyReader():

line = input.readLine():

int count = 1;

for(int i = 0;i < line.length();i++)
{

if(line.charAt(i) == ' ')

count++;
}

if(count > 2)

System.out.println("There are more than 2 words.");

else

System.out.println("There are 2 or fewer words.");

}

}
 

Of course this isn't safe to all stupid end users, with cases that won't work being where the string is just a space, the string is just a word with a space before and/or after the actual word, or multiple spaces. I was just assuming correct grammar with mine, making it very simple.

My goal for the code is for the user to input a sentence, and then if the sentence has 2 or more words, it prints "there are more than 2 words"

 

My code so far is

 

public class sentence{

public static void main(String args[]){

System.out.print("input a sentence: ");

String line;

EasyReader input = new EasyReader():

line = input.readLine():

}

}

 

 

I got the inputting thing down, but how do I count how many words are in a sentence?

 

Also easyreader is an extra java file that I use for inputs. I think it's similar to scanner.

CPU: Intel Core i7 2600k | Mootherboard: ASUS P8z68v-Pro | GPU: EVGA GTX780Ti 3GB | RAM: Kingston HyperX Genesis 8GB (4GBx2) 1600mhz | PSU: Corsair AX760 | STORAGE: Samsung 840 Pro 512GB | COOLER: Noctua NH-C14 | CASE: Fractal Design Define R4 Pearl Black | Operating SystemWindows 7 Professional 64-bit |

Link to comment
Share on other sites

Link to post
Share on other sites

My goal for the code is for the user to input a sentence, and then if the sentence has 2 or more words, it prints "there are more than 2 words"

 

My code so far is

 

public class sentence{

public static void main(String args[]){

System.out.print("input a sentence: ");

String line;

EasyReader input = new EasyReader():

line = input.readLine():

}

}

 

 

I got the inputting thing down, but how do I count how many words are in a sentence?

 

Also easyreader is an extra java file that I use for inputs. I think it's similar to scanner.

If you really wanted to you could go through the string using like a for loop, if you know how, and just check each character for a space. Just add 1 after the loop finishes or initialize the counter as one instead of zero and that should give you your answer. Test to see if number is greater than 2(?) and if it is, output that it is using an if else then in the else say it isn't

Edit: Want me to make the code for you?

Link to comment
Share on other sites

Link to post
Share on other sites

public class sentence{

public static void main(String args[]){

System.out.print("input a sentence: ");

String line;

EasyReader input = new EasyReader():

line = input.readLine():

int count = 1;

for(int i = 0;i < line.length();i++)
{

if(line.charAt(i) == ' ')

count++;
}

if(count > 2)

System.out.println("There are more than 2 words.");

else

System.out.println("There are 2 or fewer words.");

}

}
 

Of course this isn't safe to all stupid end users, with cases that won't work being where the string is just a space, the string is just a word with a space before and/or after the actual word, or multiple spaces. I was just assuming correct grammar with mine, making it very simple.

Link to comment
Share on other sites

Link to post
Share on other sites

If you really wanted to you could go through the string using like a for loop, if you know how, and just check each character for a space. Just add 1 after the loop finishes or initialize the counter as one instead of zero and that should give you your answer. Test to see if number is greater than 2(?) and if it is, output that it is using an if else then in the else say it isn't

Edit: Want me to make the code for you?

 

 

public class sentence{

public static void main(String args[]){

System.out.print("input a sentence: ");

String line;

EasyReader input = new EasyReader():

line = input.readLine():

int count = 1;

for(int i = 0;i < line.length();i++)

{

if(line.charAt(i) == ' ')

count++;

}

if(count > 2)

System.out.println("There are more than 2 words.");

else

System.out.println("There are 2 or fewer words.");

}

}

Oh I was trying to figure out what you said, but this makes it easier. Thank you.

CPU: Intel Core i7 2600k | Mootherboard: ASUS P8z68v-Pro | GPU: EVGA GTX780Ti 3GB | RAM: Kingston HyperX Genesis 8GB (4GBx2) 1600mhz | PSU: Corsair AX760 | STORAGE: Samsung 840 Pro 512GB | COOLER: Noctua NH-C14 | CASE: Fractal Design Define R4 Pearl Black | Operating SystemWindows 7 Professional 64-bit |

Link to comment
Share on other sites

Link to post
Share on other sites

You could also use the String split method. This splits a string into an array of parts on the specified character (in this case, spaces).

String[] words = line.split(" ");if (words.length > 2)    System.out.println("There are more than 2 words.");else    System.out.println("There are 2 or fewer words.");

You can also check beforehand that spaces exist with the String contains method.

if (line.contains(" ")) {    // Split string}

Just like Mefoga's answer, this isn't perfect either. It also assumes proper sentences. But at the very least, it illustrates some handy string functions to know about.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×