I'm trying to get a word count of a string entered by a user but I keep getting "0" back as a result. I can't figure out what to do.
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner reader=new Scanner(System.in);
ArrayList<String> A=new ArrayList<String>();
System.out.print("Enter a sentence: ");
String a=reader.next();
int charCount=a.length();
int space;
int period;
int wordCount=0;
//word count\
for (int i=0; i<1; i++){
space=a.indexOf(" ");
if (a.charAt(0)!=' '){
if (space!=-1){
A.add(i, a.substring(0, space-1));
a=a.substring(a.indexOf(" ")+1, charCount-1);
}
if (space==-1&&a.charAt(charCount-1)=='.'){
period=a.indexOf(".");
A.add(i, a.substring(0, period));
}
charCount=a.length();
}
wordCount=A.size();
}
System.out.print("Word Count: "+A.size());
}
Why don't you try like this-
public static void main (String[] args) {
Scanner reader=new Scanner(System.in);
System.out.println("Enter a sentence: ");
String str1 = reader.nextLine();
String[] wordArray = str1.trim().split("\\s+");
int wordCount = wordArray.length;
System.out.println("Word count is = " + wordCount);
}
Related
I want scan sentence and count how many word is. And than put the sentence to Array. And print it.
It works until System.out.println("단어개수 : " + count);
but it doesn't work after that.
import java.util.Scanner;
public class Midterm_HW00 {
public static void main(String[] args) {
System.out.println("Insert sentence: ");
Scanner scanner = new Scanner(System.in);
int count =1;
String sentence = scanner.nextLine(); //문자열 읽기
for(int i = 0; i < sentence.length(); i++) {
if(sentence.charAt(i)==' ') { //단어 개수를 띄어쓰기 개수로 계산
count++;
}
}
System.out.println("단어 개수: " + count);
String[] wordArray = new String[30]; //배열 선언
int word = wordArray.length;
for(int j=0; j<word; j++){
wordArray[j] = scanner.next();
System.out.println("" + wordArray[j]);
scanner.close();
}
}
}
you can get all this by using simple method:
String[] array = yourString.split(" ");
int amountOfWords = array.length;
Scanner scanner = new Scanner(System.in);
String sentence = scanner.nextLine();
String[] words = sentence.split("[ ]+");
System.out.println("단어 개수: " + words.length);
for(String word : words){
System.out.println(word);
}
you can use string split by a space to generate an array like this String[] wordArray = sentence.split("\\s+");
code :
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("Insert sentence: ");
Scanner scanner = new Scanner(System.in);
int count =1;
String sentence = scanner.nextLine(); //문자열 읽기
for(int i = 0; i < sentence.length(); i++) {
if(sentence.charAt(i)==' ') { //단어 개수를 띄어쓰기 개수로 계산
count++;
}
}
System.out.println("단어 개수: " + count);
String[] wordArray = sentence.split("\\s+"); //배열 선언
for(int j=0; j<wordArray.length; j++){
System.out.println("" + wordArray[j]);
}
}
import java.util.*;
public class Main {
public static void main(String[] args) {
System.out.println("Insert sentence: ");
Scanner scanner = new Scanner(System.in);
int count =1;
String sentence = scanner.nextLine();
for(int i = 0; i < sentence.length(); i++) {
if(sentence.charAt(i)==' ') {
count++;
}
}
System.out.println("단어 개수: " + count);
String[] wordArray = new String[] {sentence};
for(int j=0; j<count; j++){
System.out.println("" + wordArray[j]);
scanner.close();
}
}
}
import java.util.*;
public class Solution {
public static void main(String [] args) {
Scanner sc = new Scanner(System.in);
int count = 1;
String sentence = sc.nextLine();
// finding the number of word
for (int i=0; i<sentence.length(); i++) {
if (sentence.charAt(i) == ' ')
count += 1;
}
System.out.println("The count is : " + count);
// store the sentence to a string array using split(split by the space)
String [] sentenceToArray = sentence.split(" ");
// print all elements inside the array
for (int i=0; i<sentenceToArray.length; i++)
System.out.print(sentenceToArray[i]);
}
}
How to read and use the String array using scanner class or other in java
i could read the String into array like that below
arr[0]="apple";
arr[1]="mango";
arr[2]="banana";
.
.
.
and soon
but in by using scanner class how it possible pls tell me
and any help full "for each" in java String arrays...
To read into string array and then read the populated array you could use:
public static void main(String [] args){
// to populate string array`enter code here`
Scanner sc = new Scanner(System.in);
int length = 10, pos = -1;
String arr[] = new String[length];
while(++pos < length){
System.out.print("Enter string : ");
arr[pos] = sc.nextLine();
}
// to read the already populated string array1
for(String s: arr){
System.out.println(s);
}
}
Program to read specific number of elements from the console and write them on the console.
public class Test {
public static void main(String args[]) {
System.out.println("Enter string array size");
Scanner in = new Scanner(System.in);
int i = in.nextInt();
String[] arr = new String[i];
for (int j = 0; j < i; j++) {
System.out.println("Enter next element");
arr[j] = in.next();
}
for (String s : arr) {
System.out.println("Next element[" + s+"]");
}
}
}
import java.util.Scanner;
public class StringRWrite {
public static void main(String[] args) {
Scanner sc1=new Scanner(System.in);
System.out.println("enter the length of String array");
int n=sc1.nextInt();
String[] larray=new String[n];
for(int i=0;i<n;i++){
System.out.println("enter the "+(i+1)+" String :");
larray[i]=sc1.next();
}
System.out.println("Strings Entered by user:");
for(String s:larray){
System.out.println(s);
}
}
}
import java.util.Scanner;
public class StringReadAndWrite {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("enter the length of String array");
int n=sc.nextInt();
String[] sarray=new String[n];
for(int i=0;i<n;i++){
System.out.println("enter the "+(i+1)+" String :");
sarray[i]=sc.next();
}
System.out.println("Strings Entered by user:");
for(String s:sarray){
System.out.println(s);
}
}
}
import java.util.Scanner;
public class BasketballPlayers {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("enter the number of basketball players");
int numberOfPlayers = input.nextInt();
String[] playersArray =new String[numberOfPlayers];
for(int i=0;i< numberOfPlayers;i++){
System.out.println("enter the "+(i+1)+" String :");
playersArray[i]= input.next();
}
System.out.println("BasketBall Player names:");
for(String s:playersArray){
System.out.println(s);
}
}
}
My current code is as follows, I need to print the user's input using the outputWithoutWhitespace() method but I am having a hard time understanding where to apply it/how to actually use it. I believe I should be using \t. How can I take away the whitespace when printing with the outputWithoutWhitespace() method?
import java.util.Scanner;
public class TextAnalyzer {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
String userInput = " ";
int getNumOfCharacters = 0;
int count = 0;
System.out.println("Enter a sentence or phrase: ");
userInput = scnr.nextLine();
System.out.println("You entered: " + userInput);
count = getNumOfCharacters(userInput);
System.out.print("Number of characters: "+ count);
}
public static int getNumOfCharacters(String userInput) {
int userCount = userInput.length();
return userCount;
}
}
package whitesp;
import java.util.Scanner;
public class TextAnalyzer {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
String userInput = " ";
int getNumOfCharacters = 0;
int count = 0;
System.out.println("Enter a sentence or phrase: ");
userInput = scnr.nextLine();
System.out.println("You entered: " + userInput);
count = getNumOfCharacters(userInput);
System.out.print("Number of characters: "+ count);
}
public static int getNumOfCharacters(String userInput) {
String omitSpace=userInput.replace(" ","");
int userCount = omitSpace.length();
return userCount;
}
}
I am trying to get the following code to delete the last character one at a time until the last...
Output:
Will
Wil
Wi
W
* #author Will
*/
import java.util.Scanner;
public class DecreaseLetters {
public static void main(String args[]){
Scanner s=new Scanner(System.in);
System.out.println("Anota una Frase: ");
String input=s.nextLine();
return str.substring(0,str.length()-1);
System.out.println(input);
}
}
Maybe this?
import java.util.Scanner;
public class DecreaseLetters {
public static void main(String args[]){
Scanner s=new Scanner(System.in);
System.out.println("Anota una Frase: ");
String str=s.nextLine();
while(str.length()>1) {
str = str.substring(0,str.length()-1);
System.out.println(str.substring(0,str.length()-1));
}
}
}
Reverse:
Scanner s=new Scanner(System.in);
System.out.println("Anota una Frase: ");
String str=s.nextLine();
int i = 0;
while(i<=str.length()) {
String str2 = str.substring(0,i);
System.out.println(str2);
i++;
}
Java String is immutable. You could use a StringBuilder like,
Scanner s = new Scanner(System.in);
System.out.println("Anota una Frase: ");
String input = s.nextLine();
StringBuilder sb = new StringBuilder(input);
while (sb.length() > 0) {
System.out.print(sb);
System.out.print(" ");
sb.setLength(sb.length() - 1);
}
System.out.println();
or, you could produce substrings in a loop like
for (int i = 0; i < input.length(); i++) {
System.out.print(input.substring(0, input.length() - i));
System.out.print(" ");
}
System.out.println();
One way is to use a for loop and the substring() function of the String class:
public static void main (String[] args) throws java.lang.Exception
{
System.out.println("Anota una Frase: ");
String text = new Scanner(System.in).nextLine();
for (int i = 0; i < text.length(); i++)
System.out.println(text.substring(0, text.length()-i));
}
How do I have this program ask the user for input (a sentence) and print out the longest word of that sentence.
package projectOne;
import java.util.Scanner;
public class LongestWord {
//Scanner keyboard = new Scanner(System.in);
//System.out.println("In 1 sentence tell me what is on your mind today.");
//String actualstring = keyboard.nextLine();
static String actualstring = keyboard.nextLine();
static String[] splitstring = actualstring.split(" ");
public static void main(String [] args){
Scanner keyboard = new Scanner(System.in);
System.out.println("In 1 sentence tell me what is on your mind today.");
//String actualstring = keyboard.nextLine();
//String[] splitstring = actualstring.split(" ");
LongWord();
}
public static void LongWord() {
String longword = "";
for (int i=0; i<=splitstring.length-1; i++){
if (longword.length()<splitstring[i].length())
longword = splitstring[i];
}
System.out.println(longword);
int replyLength = longword.length();
System.out.println(replyLength);
if (replyLength == 3)
System.out.println("Hmmm tell me more about "+longword+" please");
else if (replyLength == 4)
System.out.println("Why do you feel "+longword+" is important?");
else if (replyLength == 5)
System.out.println("How does "+longword+" affect you?");
else if (replyLength > 5)
System.out.println("We seem to be making great progress with "+longword);
else
System.out.println("Is there something else you would like to discuss?");
}
}
I don't think you quite understand how methods work. In your main you should prompt the user for the line they'd like to enter and then you should read the entire line intro a String using the Scanner.nextLine() method and then you should pass said String into your longWord method for processing.
public static void main(String [] args) {
Scanner keyboard = new Scanner(System.in);
System.out.println("In 1 sentence tell me what is on your mind today.");
String sentence = keyboard.nextLine();
longWord(sentence);
}
public static void longWord(String sentence) {
//Process and print the longest word using the passed in String param
//Splitting, looping, comparisons, output
}
Try:
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
String maxword = null;
str = str + ' ';
int l = str.length();
String word = "";
int maxlength = 0;
for (int i = 0; i < l; i++) {
word = word + str.charAt(i);
if (str.charAt(i + 1) == ' ') {
if (word.length() > maxlength) {
maxword = new String(word);
maxlength = word.length();
}
word = "";
i++;
}
}
System.out.println("Longest Word: " + maxword);
}