Hello, Please help write program in JAVA PROGRAM SHOULD DO THE FOLLOWING: 1) Ask
ID: 3812926 • Letter: H
Question
Hello,
Please help write program in JAVA
PROGRAM SHOULD DO THE FOLLOWING:
1) Asks the user to enter a positive integer greater than 0
2) Validates that the entry is a positive integer
3) Outputs the digits in reverse order with a space separating the digits
4) Outputs the even digits not in reverse order with a space separating the digits (consider zero to be even)
5) Outputs the odd digits not in reverse order with a space separating the digits
6) Allows user is to repeat/continue the program as many times as he/she wants
7) Keeps a record in a txt file named outDataFile.txt with the history of all numbers entered and the associated results, in the following format:
the original number is 1023
the number reversed 3 2 0 1
the even digits are 0 2
the odd digits are 1 3
SPECIFIC REQUIREMENTS:
The program must have the following four void methods:
validate //validate user input
reverse // output reverse digits to screen and txt file
even //output even digits to screen and txt file
odd //output odd digits to screen and txt file
SUGGESTION from Professor:
To make the validate method return a string as follow:
public static String validate (String userAnswer);
Then do it.
However, the odd, even and reverse methods must be void.
BELOW ARE THE SCREENSHOTS OF WORKING PROGRAM:
an integer and pressExplanation / Answer
import java.util.*;
import java.io.FileWriter;
import java.io.IOException;
public class Digits {
static FileWriter writer;
//writer.write("Hello World");
public static String validate(String userAnswer){
for(int i=0;i<userAnswer.length();i++){
if(!Character.isDigit(userAnswer.charAt(i))) return "";
}
System.out.print("the original number is "+userAnswer+" ");
try{
writer.write("the original number is "+userAnswer+" ");
}
catch(Exception e) {
System.out.println("Unable to write in file. ");
}
return userAnswer;
}
public static void reverse(String str){
String rev ="";
for(int i=0;i<str.length();i++){
rev = " "+str.charAt(i) + rev;
}
System.out.print("the number reversed"+rev+" ");
try{
writer.write("the number reversed"+rev+" "); }
catch(Exception e) {
System.out.println("Unable to write in file. ");
}
}
public static void evendigits(String str){
String evendigits ="";
for(int i=0;i<str.length();i++){
if(str.charAt(i)%2==0) evendigits = evendigits+ " " + str.charAt(i) ;
}
System.out.print("the even digits are"+evendigits+" ");
try{
writer.write("the even digits are"+evendigits+" "); }
catch(Exception e) {
System.out.println("Unable to write in file. ");
}
}
public static void odddigits(String str){
String odddigits ="";
for(int i=0;i<str.length();i++){
if(str.charAt(i)%2==1) odddigits = odddigits+ " " + str.charAt(i) ;
}
System.out.print("the odd digits are"+odddigits+" ");
try{
writer.write("the odd digits are"+odddigits+" "); }
catch(Exception e) {
System.out.println("Unable to write in file. ");
}
}
public static void main(String[] args) {
try{
writer = new FileWriter("outDataFile.txt", true);
}
catch(Exception e){
System.out.println("Unable to open file for writing. ");
}
Scanner sc = new Scanner(System.in);
String response;
do{
System.out.println("Enter an integer and press <Enter>");
String str= sc.nextLine();
while(str!=validate(str)){
System.out.println("Invalid entry, try again");
str= sc.nextLine();
}
reverse(str);
evendigits(str);
odddigits(str);
System.out.print("y/Y to continue, any else to exit ");
response=sc.nextLine();
}while(response=="y" || response=="Y");
try{
writer.close();
}
catch(Exception e){
System.out.println("Unable to open file for writing. ");
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.