Question

I am unsure on how to do this (java language) The program you must write needs...

I am unsure on how to do this (java language)

The program you must write needs to perform the following.

1. Read from stdin(System.in), up to 100 whitespace separated words, and store them in a data structure of your choice.

2. Calculate the average word length.

3. Print each word which is greater than the average length, in the reverse order thatthey were read in.

4. Print "Average: "followed the average word length to two decimal places.

5. If no words are entered then there should be no output at all.

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

# If you have a query/issue with respect to the answer, please drop a comment. I will surely try to address your query ASAP and resolve the issue

# # Please consider providing a thumbs up to this question if it helps you. by doing that, you will help other students who are facing a similar issue.

#----------------------INPUT/OUTPUT---------------------------

#---------------------------------------------------------------------

import java.util.Scanner;

class Main {

    public static void main(String[] args) {

        //scanner object

        Scanner scanner = new Scanner(System.in);

        //reads input and splits into array

        String[] arr = scanner.nextLine().split(" ");

        int total = 0;

        //loops over the array

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

            //adds the lenght

            total = total + arr[i].length();

        }

        //finds the average

        double avg = (double) total / arr.length;

        //checks if lengh>0 that is user entered words

        if (arr.length > 0) {

            //loop over the array in reverse

            for (int i = arr.length - 1; i >= 0; i--) {

                //checks length

                if (arr[i].length() > avg) {

                    System.out.println(arr[i]);

                }

            }

            //prints with 2 decimal places

            System.out.printf("Average: %.2f", avg);

        }

    }

}

Add a comment
Know the answer?
Add Answer to:
I am unsure on how to do this (java language) The program you must write needs...
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
  • write a program in C Write a program to implement the following requirement: The program will...

    write a program in C Write a program to implement the following requirement: The program will read from standard input any text up to 10, 900, characters and store each unique word (any string that does not contain any whitespace) into a node of a linked list, following the following Node struct: struct NODE { char *word; struct NODE * prev; Note that each node must store a unique word, i.e., no word is stored in more than one node....

  • Today you are to write a Java program that will prompt for and read 2 words...

    Today you are to write a Java program that will prompt for and read 2 words of equal length entered by the user, and create a new word which contains the last letter of the 1st word, the last letter of the 2nd word, followed by the second to last character of the 1st word, followed by the second to last character of the 2nd word and so on. Be sure to use the same format and wording as in...

  • JAVA LANGUAGE For this lab you are required to fulfill all requirements exactly as described in...

    JAVA LANGUAGE For this lab you are required to fulfill all requirements exactly as described in this provided document, no less, no more. Question: Today you are to write a Java program that will prompt for and read 2 words of equal length entered by the user, and create a new word which contains the last letter of the 1st word, the last letter of the 2nd word, followed by the second to last character of the 1st word, followed...

  • Write an efficient java program to implement BST. Your java program should read n words and...

    Write an efficient java program to implement BST. Your java program should read n words and its corresponding French and store it in a BST. Then read every English word and print its corresponding French by searching in BST. Assume your java program is in engtofren.java file To compile: javac engtofren.java To execute: java engtofren< any data file name Your main method should be as follow: public static void main(String args[]) { engtofren bst = new engtofren (); // try{...

  • Could you guys write an efficient java program to implement BST. Your java program should read...

    Could you guys write an efficient java program to implement BST. Your java program should read words and its corresponding French and store it in a BST. Then read every English word and print its corresponding French by searching in BST. Assume your java program is in xxxxx5.java file (5th java project), where xxxxx is the first 5 characters of your last name. To compile: javac xxxxx5.java To execute: java xxxxx5 < any data file name Your main method should...

  • I am having problems with my java homework in class. Write a program that will interactively...

    I am having problems with my java homework in class. Write a program that will interactively read the name, age, weight in pounds, and height in inches for five people and output the BMI for each one. At the end of the program, it should output the average age, average height and average weight of the five participants. IT HAS TO INCLUDE: Declare and use a program constant of data type int named CONVERSION_FACTOR to represent the value 703. Use...

  • . . In this programming assignment, you need to write a CH+ program that serves as...

    . . In this programming assignment, you need to write a CH+ program that serves as a very basic word processor. The program should read lines from a file, perform some transformations, and generate formatted output on the screen. For this assignment, use the following definitions: A "word" is a sequence of non-whitespace characters. An "empty line" is a line with no characters in it at all. A "blank line" is a line containing only one or more whitespace characters....

  • Write a Java program that reads in 5 words from the user and stores these words...

    Write a Java program that reads in 5 words from the user and stores these words in a String array. Then your program needs to print 2 lines of output with the following information: All words are in reverse order (based both on content and order). Every word at an even index of the array (i.e., indices 0, 2, and 4). For example, if user inputs "ABC", "DEF", "TY", "JK", and "WE". Then your program should output the following two...

  • 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...

  • PLEASE WRITE SIMPLE JAVA PROGRAM AND ALSO EXPLAIN THE LOGIC. Given a string, print the first...

    PLEASE WRITE SIMPLE JAVA PROGRAM AND ALSO EXPLAIN THE LOGIC. Given a string, print the first word which has its reverse word further ahead in the string. Input Format: A string of space separated words, ending with a 's'. Conditions ; Each string ends with a $ character. All characters in the string are either spaces. $ or lower case English alphabets, Output Format : In a single line pent either . The first word from the start of the...

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