Write a program which: 1. Asks the user to enter his/her name AND validates it 2
ID: 3811584 • Letter: W
Question
Write a program which:
1. Asks the user to enter his/her name AND validates it
2. Once the name is validated, then the program prompts the user to enter anything (s)he wants until user types -1.
3. The program must create a text file with the user’s name AND store EVERYTHING the user types in it.
4. The program must then read the contents of the file and count the number of even digits and the number of odd digits
5. The program should display the number of even digits if there are any; otherwise it should indicate that there are no even digits.
6. The program should display the number of odd digits if there are any; otherwise it should indicate that there are no odd digits.
7. 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
8. 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!
GENERAL RESTRICTIONS FOR ALL ASSIGNMENTS
No global variables
No infinite loops, examples include:
for(;;;)
while(1)
while(true)
do{//code}while(1);
No break statements to exit loops
Explanation / Answer
package readwriteevenoddfile;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
/*@author Himanshu */
public class ReadWriteEvenOddFile {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws IOException {
// TODO code application logic here
System.out.println("Enter your name");
Scanner readName = new Scanner(System.in);
String name= readName.next();
int nameValidation=validateUserName(name);
if(nameValidation != 1)
{
System.out.println("Exiting...");
}
else
{
File file =writeDataToFile(name);
readDataFromFile(file);
}
}
static int validateUserName(String name){
if(name.equals("Flash"))
{
System.out.println("Welcome Flash, You are the Fastest Man Alive");
return 1;
}
else
{
System.out.println("Go Home "+name+"Send Flash Here");
return 0;
}
}
static int checkEvenDigit(char c)
{
int evenCount =0;
if(Character.isDigit(c))
{
if((int)c % 2 == 0)
{
evenCount++;
}
}
return evenCount;
}
static int checkOddDigit(char c)
{
int oddCount =0;
if(Character.isDigit(c))
{
if((int)c % 2 != 0)
{
oddCount++;
}
}
return oddCount;
}
static File createFile(String name)
{
try {
File file = new File("d:\"+name);
if (file.createNewFile()){
System.out.println("File is created!");
return file;
}else{
System.out.println("File already exists.");
return null;
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
static File writeDataToFile(String name) throws IOException
{
System.out.println("Enter your inputs Enter -1 to stop");
File file =createFile(name);
String path = file.getAbsolutePath();
String inputCont = System.console().readLine();
Charset charset = Charset.forName("US-ASCII");
try (BufferedWriter writer = Files.newBufferedWriter(file.toPath(), charset)) {
writer.write(inputCont, 0, inputCont.length());
} catch (IOException x) {
System.err.format("IOException: %s%n", x);
}
return file;
}
static void readDataFromFile(File file)
{
int evenCount=0,oddCount=0;
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(file));
int i;
// repeat until EOF
while ((i = (char)reader.read()) != -1) {
char c = (char)i;
evenCount=checkEvenDigit(c);
oddCount = checkOddDigit(c);
// do with c whatever you want
}
displayResults(evenCount,oddCount);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (reader != null) {
reader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
static void displayResults(int evenCount, int oddCount)
{
if(evenCount <1)
{
System.out.println("No even Digit");
}
else
{
System.out.println("Number of even Digit = "+evenCount);
}
if(oddCount <1)
{
System.out.println("No odd Digit");
}
else
{
System.out.println("Number of odd Digit = "+oddCount);
}
}
}
/** You need to enter username as Flash to continue */
/*Input is takenin a string you can convert it into data stream and write in file if you want*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.