Question

in java, Ask the user for a string. Display the number of whitespace characters in that...

in java,

Ask the user for a string. Display the number of whitespace characters in that string.

Standard Input:

this is a test

Required Output:

Enter a String\n
Number of whitespace characters: 3\n
0 0
Add a comment Improve this question Transcribed image text
Answer #1

ANSWER: HEre i am giving you the code and output please like it

CODE:

import java.util.*;


public class Main {
  
     
public static void main(String [] args)
{
   Scanner sc= new Scanner (System.in);
  
   String str;
   System.out.println("Enter a String :");
   str=sc.nextLine();
   int len=str.length() - str.replaceAll(" ", "").length();
     
   System.out.println("Number of whitespace characters :"+len);
     
}
}

OUTPUT:

Add a comment
Know the answer?
Add Answer to:
in java, Ask the user for a string. Display the number of whitespace characters in that...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • in java please Create an array of characters. Ask the user to enter two characters. Tell...

    in java please Create an array of characters. Ask the user to enter two characters. Tell the user which character occurs more frequently For example, suppose the array contains (a, b, c, d, 8, 2, e, e, f, g and the user enterse and then the program will say that occurs more frequently thanL Bonus: Display a list of all characters in the array with their frequencies. Each value should appear only once For example for the array(a, b, c,...

  • Write a Java Program that reads the following: Ask the user for a balance. From the...

    Write a Java Program that reads the following: Ask the user for a balance. From the accounts.dat file, display all the accounts that have a balance of at least what the user entered. accounts.dat is a binary file. Each entry contains a credit card number (long), a balance (double), and a cash back flag (boolean). Question 1 10 points Ask the user for a balance. From the accounts.dat file.display all the accounts that have a balance of at least what...

  • In Java, using only recursion. Ask the user for a list of integers. Display the greatest...

    In Java, using only recursion. Ask the user for a list of integers. Display the greatest number in the sequence. Allow any non-integer to end the input. DO NOT USE LOOPS. We are currently learning recursion and can only use recursion. Please provide notes so to better understand. Please describe how inputting a string will end the input section and then go on to execute what number is greater within the entire user input. -------------------------------------------------------------------------------- Standard Input                 5 20...

  • In Java, write a program that prompts the user to input a sequence of characters and...

    In Java, write a program that prompts the user to input a sequence of characters and outputs the number of vowels. You will need to write a method to return a value called isAVowel which will return true if a given character is a vowel and returns false if the character is not a vowel. A second method, called isAConstant, should return true if a given character is a constant and return false if the character is not a constant....

  • Write a Java program which takes a string as a user input. Create 2 functions. The...

    Write a Java program which takes a string as a user input. Create 2 functions. The first function expects a string as argument and returns void. It converts all upper case characters to lower case and converts all lower case characters to upper case. It then prints out the converted string to a console. The second function also expects a string as argument and returns void. It will find first charactor that is repeated exactly 2 times in the string....

  • This is another attempt to ask this question: I need a Java program that asks the...

    This is another attempt to ask this question: I need a Java program that asks the user for a String. the string must be atleast 7 character and only words and/or numbers, the program should read this input and output “Valid”. No special characters or whitespaces are allowed, if so output “Invalid” *** Please note: The program must be able to know if the string is less than 7 characters and output INVALID. Also the program must be able to...

  • JAVA: Write a program that inputs a string that represents a binary number. The string can...

    JAVA: Write a program that inputs a string that represents a binary number. The string can contain only 0s and 1s and no other characters, not even spaces. Validate that the entered number meets these requirements. If it does not, display an error message. If it is a valid binary number, determine the number of 1s that it contains. If it has exactly two 1s, display "Accepted". Otherwise, display "Rejected". All input and output should be from the console.

  • (Java) HELP! Create a program that will ask the user to enter their first name and...

    (Java) HELP! Create a program that will ask the user to enter their first name and their favorite number. Ask the user if they would like the information repeated back to them. If they type "Y" display the answers back to them. If they type anything else, display the word "Goodbye". An example of what your output window should look like is below. The system-generated output is in red. The user-provided input is in green. (Your actual program will only...

  • JAVA Ask the user for integers. If the user did not enter an integer, ask again....

    JAVA Ask the user for integers. If the user did not enter an integer, ask again. (Type Safe Input) Keep a running total of the intergers entered (sum). Once the user enters 0, stop looping. Print the sum of all the numbers. Do not use try-catch. EXAMPLE OUTPUT: Enter an integer, 0 to stop> [fasdfsa] Invalid input. Enter an integer, 0 to stop> [231.342] Invalid input. Enter an integer, 0 to stop> [1] Enter an integer, 0 to stop> [2]...

  • Java plz You, as the author, discovered a particular word (a sequence of non-whitespace characters) in...

    Java plz You, as the author, discovered a particular word (a sequence of non-whitespace characters) in your paper was misspelled, and you would like to correct the error. Implement the following method declaration to replace all occurrences of wrongWord with newWord in the array words: public static void replace(String[] words, String wrongWord, String newWord)

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT