Question

Hello, can you please show me how to do this program with COMPLETE INPUT VALIDATION so...

Hello,

can you please show me how to do this program with COMPLETE INPUT VALIDATION so the computer tells the user to enter ints only if the user enters in floating point numbers or other characters?

Write a method called evenNumbers that accepts a Scanner reading input from a file containing a series of integers, and report various statistics about the integers to the console. Report the total number of numbers, the sum of the numbers, the count of even numbers and the percent of even numbers. For example, if the input file contains the following text:

5 7 2 8 9 10 12 98 7 14 20 22

:

Then the method should produce the following console output:

12 numbers, sum = 214, 8 evens(66.67%)

PLEASE TEST THE PROGRAM TO SEE IF IT WORKS

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

If you have any doubts, please give me comment...

Otherthan any integers will be skipped...

import java.io.*;

import java.util.*;

class EvenNumbers {

public static void main(String[] args) {

try {

Scanner in = new Scanner(new File("in.txt"));

evenNumbers(in);

} catch (FileNotFoundException e) {

System.out.println("File not found(in.txt)");

}

}

public static void evenNumbers(Scanner in) {

String temp;

int n = 0, num, sum = 0, evens = 0;

while (in.hasNext()) {

if (in.hasNextInt()) {

num = in.nextInt();

sum += num;

if (num % 2 == 0)

evens++;

n++;

} else {

temp = in.next();

}

}

double even_perc = ((double) evens / n) * 100;

System.out.printf("%d numbers, sum = %d, %d(%.2f)%%) ", n, sum, evens, even_perc);

}

}

Add a comment
Know the answer?
Add Answer to:
Hello, can you please show me how to do this program with COMPLETE INPUT VALIDATION so...
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
  • Hello, Could you please input validate this code so that the code prints an error message...

    Hello, Could you please input validate this code so that the code prints an error message if the user enters in floating point numbers or characters or ANYTHING but VALID ints? And then could you please post a picture of the output testing it to make sure it works? * Write a method called evenNumbers that accepts a Scanner * reading input from a file with a series of integers, and * report various statistics about the integers to the...

  • Hello, Could you please explain how I would complete this program with input validation to ensure...

    Hello, Could you please explain how I would complete this program with input validation to ensure that an error message will not appear if the user enters something other than an integer that is not 1-100 and later if they enter anything other than yes and no? Here is the program: Write a program that plays the Hi-Lo guessing game with numbers. The program should pick a random number between 1 and 100 (inclusive), then repeatedly promt the user to...

  • Finish the given ProcessFile.java program that prompts the user for a filename and reprompts if file...

    Finish the given ProcessFile.java program that prompts the user for a filename and reprompts if file doesn’t exist. You will process through the file skipping any text or real (double) numbers. You will print the max, min, sum, count, and average of the integers in the file. You will want to create test files that contain integers, doubles, and Strings. HINT: Use hasNextInt() method and while loop. You may also want to use Integer.MAX_VALUE and Integer.MIN_VALUE for the initialization of...

  • Homework 3: Input Validation 1   Objectives control structures console-based user input using Scanner class writing complete...

    Homework 3: Input Validation 1   Objectives control structures console-based user input using Scanner class writing complete programs using two classes: client and supplier 2   User Interface Specification This is a console-based I/O program. Display should go to System.out (print or println) and the program will get user input using the Scanner class. The flow of execution should be as follows: When the program starts, display a one-line introduction to the user Display a menu with 5 options 1. validate zip...

  • Please use Python Exercise 1 Create a program named sentinel_loop using break. This program should implement...

    Please use Python Exercise 1 Create a program named sentinel_loop using break. This program should implement functionality that is the same as the Part 1 tutorial example, except for the following differences: • • This program should count even and odd integers. This program should report counts of even and odd integers. Remember that even integers can be identified using the test condition: value % 2 == 0 Remember to test your code for appropriate behavior when the user signals...

  • * Please write the answer without the explanation. * Please star with Public class. Write a...

    * Please write the answer without the explanation. * Please star with Public class. Write a program that asks the user to enter two positive integers (input validation), then the program should pass the numbers to the method to display all even numbers between those two integers inclusive in the following format. If enter 11 and 18, the output should be: (12, 14, 16, 18)

  • Hi please help me with this zybooks question asap. Thanks 4.17 Chapter 4 Program: Sum of...

    Hi please help me with this zybooks question asap. Thanks 4.17 Chapter 4 Program: Sum of Numbers/C++ program Write a C ++ program that asks the user for a positive integer value by prompting "Enter a positive integer number: ", read in that number, then use a loop to get the sum of all the integers from 1 up to the number entered. For example, if the user enters 50, the loop will find the sum of 1, 2,3, 4…50....

  • In the first task, you will write a Java program that accepts a string of the...

    In the first task, you will write a Java program that accepts a string of the format specified next and calculate the answer based on the user input. The input string should be of the format dddxxdddxx*, where d represents a digit and x represents any character and asterisk at the end represents that the string can have any number of characters at the end. 1. Prompt the user to enter a string with the specific format (dddxxdddxx*) 2. Read...

  • in Java and also follow rubric please 4. Write a complete program to do the following:...

    in Java and also follow rubric please 4. Write a complete program to do the following: Using an input and output files, write a program that will read 20 numbers from an input file called InFile. Sum all even numbers and multiply all odd numbers. Print the numbers read from the input file to an output file called OutFile. Also print the sum of the even numbers and the product of the odd numbers to the output file. Rubric: •...

  • In this lab, you complete a partially written Java program that includes two methods that require...

    In this lab, you complete a partially written Java program that includes two methods that require a single parameter. The program continuously prompts the user for an integer until the user enters 0. The program then passes the value to a method that computes the sum of all the whole numbers from 1 up to and including the entered number. Next, the program passes the value to another method that computes the product of all the whole numbers up to...

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