Question: Write a program which: Asks the user to enter a positive integer greater than 0 Validates that the entry is a positive integer Outputs the
Write a program which:
Asks the user to enter a positive integer greater than 0
Validates that the entry is a positive integer
Outputs the digits in reverse order with a space separating the digits
Outputs the even digits not in reverse order with a space separating the digits (consider zero to be even)
Outputs the odd digits not in reverse order with a space separating the digits
The program outputs There are no odd digits, when there are no odd digits in the user input.
The program outputs There are no even digits, when there are no even digits in the user input.
Allows user is to repeat/continue the program as many times as he/she wants
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
-----------------
the original number is 102030
the number reversed 0 3 0 2 0 1
the even digits are 0 2 0 0
the odd digits are 1 3
-----------------
SPECIFIC REQUIREMENTS
The program must have the following three void methods:
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
and a fourth method that returns a String; the method is called validate, see below.
validate //validate user input
Here is my code. It won't complete when I enter numbers. Not sure what is wrong. Please help.
import java.util.Scanner; import java.io.*; class ReverseNumber{ public static void validate(String n,FileWriter out) throws Exception{ int numberLength = n.length(); int counter = 0; //Creare a Scanner onject to read input Scanner keyboard = new Scanner(System.in); if(Integer.parseInt(n) > 0) { String result="the original number is "+n+" "; System.out.print(result); out.write(result); reverse(n,out); even(n,out); odd(n,out); } while(numberLength == 0) { System.out.println("Invalid entry, try again "); n = keyboard.nextLine(); numberLength = n.length(); } while(counter < numberLength) { if(!Character.isDigit(n.charAt(counter))); { System.out.println("Invalid entry, try again "); n = keyboard.nextLine(); numberLength = n.length(); counter = 0; } } if(Integer.parseInt(n) > 0) { String result="the original number is "+n+" "; System.out.print(result); out.write(result); reverse(n,out); even(n,out); odd(n,out); } }
public static void reverse(String n,FileWriter out) throws Exception{ String input = String.valueOf(n); String result = ""; for (int i = input.length() - 1; i >= 0; i--) { result = result + input.charAt(i)+' '; } result="the number reversed "+result+" "; System.out.print(result); out.write(result); } public static void even(String n,FileWriter out) throws Exception{ String input = String.valueOf(n); String result = ""; for (int i = 0; i
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
