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 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%)
*
*/
import java.util.*;
import java.io.*;
public class Chap6Ex02{
public static void main (String[] args) throws FileNotFoundException{
Scanner input = new Scanner(new File("integers.txt"));
evenNumbers(input);
}
public static void evenNumbers( Scanner inp){
int count = 0;
int sum = 0;
int evens = 0;
while(inp.hasNextInt()){
int num = inp.nextInt();
count++;
sum += num;
if (num % 2 == 0){
evens++;
}
}
double percent = evens/count * 100;
System.out.println(count + " numbers, sum = " + sum);
System.out.println(evens + " evens (" + percent + "%)");
}
}Program:
import java.util.*;
import java.io.*;
public class Chap6Ex02{
public static void main (String[] args) throws FileNotFoundException{
Scanner input = new Scanner(new File("integers.txt"));
evenNumbers(input);
}
public static void evenNumbers(Scanner inp){
int count = 0;
int sum = 0;
int evens = 0;
boolean isValid = true;
//run a loop until end of file is reached
while(inp.hasNext())
{
//check is the value encountered is an integer or not, if integer proceed else print error message
if(inp.hasNextInt())
{
//read the integer
int num = inp.nextInt();
//increment the count numbers read
count++;
//add the integer to sum
sum += num;
//check if integer is even, if even increment count of evens
if (num % 2 == 0){
evens++;
}
}
//if an invalid value is encountered in the file
else
{
//display error message and terminate the loop
System.out.println("Error: Invalid values in input file.");
isValid = false;
break;
}
}
//display results only if all the values are valid
if(isValid)
{
double percent = (double)(evens)/(double)(count) * 100;
System.out.println(count + " numbers, sum = " + sum);
System.out.printf("%d evens (%.2f%%)", evens, percent);
//System.out.println(evens + " evens (" + percent + "%)");
}
}
}
Output:
Output when valid input is used


Output when invalid input is entered




Program screenshot:

Hello, Could you please input validate this code so that the code prints an error message...
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...
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...
1. Import file ReadingData.zip into NetBeans. Also, please
download the input.txt file and store it into an appropriate folder
in your drive.
a) Go through the codes then run the file and write the output
with screenshot (please make sure that you change the location of
the file) (20 points)
b) Write additional Java code that will show the average of all
numbers in input.txt file (10 points)
import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; import java.io.*; 1- * @author mdkabir...
Problem: Use the code I have provided to start writing a program that accepts a large file as input (given to you) and takes all of these numbers and enters them into an array. Once all of the numbers are in your array your job is to sort them. You must use either the insertion or selection sort to accomplish this. Input: Each line of input will be one item to be added to your array. Output: Your output will...
JAVA: Rewrite the following code to where the inputs are from a file. The file name will be from the input from the user scanner. CODE: import java.util.*; public class Q1 { public static int sumOD (int k) { int sumOD = 0; int in = k; while (in != 0) { int digitON = in % 10; sumOD += digitON; in /= 10; } return sumOD; } public static void main(String[] args) { int n, i, k; System.out.println("Enter...
I am given an input file, P1input.txt and I have to write code to find the min and max, as well as prime and perfect numbers from the input file. P1input.txt contains a hundred integers. Why doesn't my code compile properly to show me all the numbers? It just stops and displays usage: C:\> java Project1 P1input.txt 1 30 import java.io.*; // BufferedReader import java.util.*; // Scanner to read from a text file public class Project1 { public static...
JAVA HELP FOR COMP: Can someone please modify the code to create LowestGPA.java that prints out the name of the student with the lowestgpa. In the attached zip file, you will find two files HighestGPA.java and students.dat. Students.dat file contains names of students and their gpa. Check if the code runs correctly in BlueJ or any other IDE by running the code. Modify the data to include few more students and their gpa. import java.util.*; // for the Scanner class...
Please explain if the following code is actually correct. If the following code correct, please explain why the code works and is also correct. Don’t use * Java’s Integer .toBinaryString(int) in this program./* According to the textbook and the instructor, I am not supposed to use arrays such as int binary[] = new int[25]; I guess this is the reason why this problem is starting to look kind of hard. Chapter 5 Exercise 37: Java Programming * * (Decimal to...
Homework Assignment on Mimir: Read in a file "numbers.txt" into a Java program. Sum up all the even numbers over 50. Print the result to the console using System.out.println(); The file will only have numbers. Code: import java.io.FileNotFoundException; import java.io.FileReader; import java.util.Scanner; /** * * @author David */ public class ReadingFiles { /** * @param args the command line arguments */ public static void main(String[] args) throws FileNotFoundException { // TODO code application logic here...
Computer Science - Java
Explain the code step by step (in detailed order). Also explain
what the program required. Thanks
7 import java.io.File; 8 import java.util.Scanner; 9 import java.util.Arrays; 10 import java.io.FileNotFoundException; 12 public class insertionSort 13 14 public static void insertionSort (double array]) 15 int n -array.length; for (int j-1; j < n; j++) 17 18 19 20 21 double key - array[j]; int i-_1 while ( (i > -1) && ( array [i] > key array [i+1] -...