Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program in C++ which: Asks the user to enter his/her name AND validates

ID: 3818059 • Letter: W

Question

Write a program in C++ which:

Asks the user to enter his/her name AND validates it

Once the name is validated, then the program prompts the user to enter anything (s)he wants until user types -1.

The program must create a text file with the user’s name AND store EVERYTHING the user types in it.

The program must then read the contents of the file and count the number of even digits and the number of odd digits

The program should display the number of even digits if there are any; otherwise it should indicate that there are no even digits.

The program should display the number of odd digits if there are any; otherwise it should indicate that there are no odd digits.

The program MUST have the following functions:

void validateUserName(which parameters? pass by reference or by value?);//validate user name

void validateUserInput(which parameters? pass by reference or by value?);//validate user input

void checkEvenDigit(which parameters? pass by reference or by value?);//check for the presence of even digits

void checkOddDigit(which parameters? pass by reference or by value?);//check for the presence of odd digits

void createFile(which parameters? pass by reference or by value?);//create userFile

void writeDataToFile(which parameters? pass by reference or by value?);//write to the file

void readDataFromFile(which parameters? pass by reference or by value?);//read from the file

void displayResults(which parameters? pass by reference or by value?);//display results

The main() function should consist mostly of local variable declarations and a series of function calls. One of the objectives of the quiz is to get you to “modularize” your programs using functions—hence main() must necessarily be very short!

Explanation / Answer

import java.util.Scanner;
import java.util.regex.Pattern;
import java.io.*;

public class test
{
   static Scanner scanner = new Scanner(System.in);
   static int even = 0;
   static int odd = 0;
   public static void main(String[] args) throws IOException {
  
   String userName = scanner.nextLine();
   createFile(userName);
   String fileName = userName+".txt";
   writeDataToFile(fileName);
   readDataFromFile(fileName);
   displayResults();
   }
  
   static void validateUserName(String name){
       final Pattern pattern = Pattern.compile("[A-Za-z ]*");
   if (!pattern.matcher(name).matches()) {
   System.out.println("Invalid User Name");
   System.exit(1);
   }
   }
  
   static boolean validateUserInput(String input){
       try{
Integer.parseInt(input);
return true;
}catch(NumberFormatException e){
return false;
}
   }
  
   static void writeDataToFile(String fileName) throws IOException{
       BufferedWriter out = null;
       String input = null;
       try
       {
       FileWriter fstream = new FileWriter(fileName, true); //true tells to append data.
       out = new BufferedWriter(fstream);
       input = scanner.nextLine();
       while(!input.equals("-1")){
           if(validateUserInput(input)){
               out.newLine();
               out.write(input);
           }else{
               out.close();
           }
           input = scanner.nextLine();
       }
      
       }
       catch (IOException e)
       {
       System.err.println("Error: " + e.getMessage());
       System.exit(1);
       }
       finally
       {
       if(out != null) {
       out.close();
       }
       }

   }
  
   static void readDataFromFile(String fileName) throws IOException{
       BufferedReader read = null;
       try
       {
       FileReader fstream = new FileReader(fileName); //true tells to append data.
       read = new BufferedReader(fstream);
       Scanner scan = new Scanner(read);
       test a = new test();
       while (scan.hasNext())
{
String str = scan.next();
char[] myChar = str.toCharArray();
for(int i = 0; i < myChar.length; i++){
   int b = myChar[i]- '0';
a.checkEvenDigit(b);
a.checkOddDigit(b); }
}

scan.close();
       }
       catch (IOException e)
       {
       System.err.println("Error: " + e.getMessage());
       System.exit(1);
       }
       finally
       {
       if(read != null) {
       read.close();
       }
       }
      
   }
  
  
   void checkOddDigit(int digit) {
       if(digit%2 == 1){
           odd++;
       }      
   }

   void checkEvenDigit(int digit) {
       if(digit%2 == 0){
           even++;
       }
   }

   static void createFile(String fileName){
       try{
           File file = new File(fileName+".txt");
           if(file.exists()){
               file.delete();
           }
           file.createNewFile();
       }
       catch(Exception e){
           System.out.println("Error in creating file");
           System.exit(1);
       }
   }
  
   static void displayResults(){
       System.out.println("No. of even digits: "+even);
       System.out.println("No. of odd digits: "+odd);
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote