Question

I am trying to create a program called Count.java that contains a single method called compute...

I am trying to create a program called Count.java that contains a single method called compute with a string parameter called value.

The method should count the number of newlines, words (any sequence of characters separated with whitespace), and characters in a given string and return an array of the three integer values.

Use a Scanner to count the number of words in a string. The Scanner.next() method returns the next word, ignoring whitespace.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer:

/******************************************************************************

Welcome to GDB Online.
GDB online is an online compiler and debugger tool for C, C++, Python, Java, PHP, Ruby, Perl,
C#, VB, Swift, Pascal, Fortran, Haskell, Objective-C, Assembly, HTML, CSS, JS, SQLite, Prolog.
Code, Compile, Run and Debug online from anywhere in world.

*******************************************************************************/
import java.util.*;
public class Count
{
public static int[] compute(String value){
int charCount=0;
int lineCount=1;
int wordsCount=0;
for (int i=0;i<value.length();i++)
{
//if we found slash n we count the lines
if(value.charAt(i)=='\n')
lineCount++;
}
Scanner sc=new Scanner(value);
//scanning the tokens from given string
while(sc.hasNext())
{
wordsCount++;
sc.next();
}
//calculating the char count without spaces and \n values
charCount=value.length()-lineCount-wordsCount;
int arr[]={charCount,lineCount,wordsCount};
return arr;
  
  
}
   public static void main(String[] args) {
  
   int arr[]=compute("hello world \n welcome to my world");
   System.out.println("charcount: "+arr[0]);
       System.out.println("linecount: "+arr[1]);
           System.out.println("wordscount: "+arr[2]);
   }
}

Add a comment
Know the answer?
Add Answer to:
I am trying to create a program called Count.java that contains a single method called compute...
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
  • PrintArray vi Create a class called PrintArray. This is the class that contains the main method....

    PrintArray vi Create a class called PrintArray. This is the class that contains the main method. Your program must print each of the elements of the array of ints called aa on a separate line (see examples). The method getArray (included in the starter code) reads integers from input and returns them in an array of ints. Use the following starter code: //for this program Arrays.toString(array) is forbidden import java.util.Scanner; public class PrintArray { static Scanner in = new Scanner(System.in);...

  • In this lab you will write a spell check program. The program has two input files:...

    In this lab you will write a spell check program. The program has two input files: one is the dictionary (a list of valid words) and the other is the document to be spellchecked. The program will read in the words for the dictionary, then will read the document and check whether each word is found in the dictionary. If not, the user will be prompted to leave the word as is or type in a replacement word and add...

  • I need java code for the following problem. Lab 7: Methods 1. Write a Java program called Numbers that calls the following methods and displays the returned value: Write a method called cubelt that a...

    I need java code for the following problem. Lab 7: Methods 1. Write a Java program called Numbers that calls the following methods and displays the returned value: Write a method called cubelt that accepts one integer parameter and returns the value raised to the third power as an integer. o Write a method called randominRange that accepts two integer parameters representing a range. The method returns a random integer in the specified range inclusive. 2. o Write a method...

  • write a program which include a class containing an array of words (strings).The program will search...

    write a program which include a class containing an array of words (strings).The program will search the array for a specific word. if it finds the word:it will return a true value.if the array does not contain the word. it will return a false value. enhancements: make the array off words dynamic, so that the use is prompter to enter the list of words. make the word searcher for dynamic, so that a different word can be searched for each...

  • Create a class called DuplicateRemover. Create an instance method called remove that takes a single parameter...

    Create a class called DuplicateRemover. Create an instance method called remove that takes a single parameter called dataFile (representing the path to a text file) and uses a Set of Strings to eliminate duplicate words from dataFile. The unique words should be stored in an instance variable called uniqueWords. Create an instance method called write that takes a single parameter called outputFile (representing the path to a text file) and writes the words contained in uniqueWords to the file pointed...

  • in java eclipse, Create a program “FibonacciFifteen.java” and a method called “double[] getFibonacci()” that creates an...

    in java eclipse, Create a program “FibonacciFifteen.java” and a method called “double[] getFibonacci()” that creates an array with a length of 15 that contains the first 15 numbers in the Fibonacci sequence and returns it. Set the first element to 0 and the second element to 1, then use a for loop to fill out the rest of the array by adding the prior two elements.

  • java /* Q2 (10 pts): Write a method called method that accepts an integer parameter *...

    java /* Q2 (10 pts): Write a method called method that accepts an integer parameter * * * * and returns a sum of the first n terms of the sequence. * In other words, the method should generate the following sequence: 1 + 1/2 + 1/3 + 1/4 + ... 1/n * For example, method2(2) will return 1.5 since 1+1/2 = 1.5 * method2 (15) will return 3.3182289932289937 * You may assume that the parameter n is nonnegative. */...

  • What to submit: your answers to exercises 1, 3, and 4 and separate the codes to...

    What to submit: your answers to exercises 1, 3, and 4 and separate the codes to each question. Using your solution to question 3 of Lab Practice 2, modify it to create a class that contains a static method; the method takes a string as a parameter and returns a boolean value indicating whether the parameter string has repeated characters in it or not. That is, return true if there is at least one character which appears more than once...

  • Program: Authoring assistant

    7.17 LAB*: Program: Authoring assistant(1) Prompt the user to enter a string of their choosing. Store the text in a string. Output the string. (1 pt)Ex:Enter a sample text: We'll continue our quest in space.  There will be more shuttle flights and more shuttle crews and,  yes,  more volunteers, more civilians,  more teachers in space.  Nothing ends here;  our hopes and our journeys continue! You entered: We'll continue our quest in space.  There will be more shuttle flights and more shuttle crews and,  yes,  more volunteers, more civilians,  more teachers in space.  Nothing ends here;  our hopes and our journeys continue!(2) Implement a printMenu() method, which outputs a menu of user options for analyzing/editing the string, and returns the user's entered menu option. Each option is represented by a single character.If an invalid character is entered, continue to prompt for a valid choice. Hint: Implement Quit before implementing other options. Call printMenu() in the main() method....

  • I need help writing a C programming code. I am trying to create a program that...

    I need help writing a C programming code. I am trying to create a program that returns ANY given number's decimal value as an Integer (Whole number). Examples: User input: 0.35 Output: 35 User input: 1.465 Output: 465 User input: 10.6054 Output: 6054

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