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

Write JAVA program please help How do I validate input to only allow positive nu

ID: 3603260 • Letter: W

Question

Write JAVA program please help

How do I validate input to only allow positive numbers? The Validate method I wrote crashed the program. My professor wants us to add that feature.

The homework question is:

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

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.

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

public class Reverse {
public static void main(String[] args) throws IOException {
  
//Creates path of outDataFile.txt
BufferedWriter out = new BufferedWriter(new FileWriter("outDataFile.txt"));

Scanner input=new Scanner(System.in);
int choice;
//String validate;
String repeat;
  
//Loop repeats until a valid input is found
do
{
//Asks for number and reads value
System.out.print(" Enter an integer and press ");
choice = input.nextInt();

//String space = "";
//validate = input.next("");
//Prints error if invalid number
if(Validate(choice))
{
System.out.println(" The original number is " + choice);
out.write(" The original number is " + choice);
out.newLine();

//Calls Reverse method to reverse number
Reverse(choice, out);

//Calls Even method to find if number is even
Even(choice, out);

//Calls Odd method to find if number is odd
Odd(choice, out);
}
else
System.out.println("Invalid Input.");
//Asks if user wants to continue
System.out.print(" y/Y to continue, any else to exit ");

repeat = input.next();
}while(repeat.toLowerCase().equals("y")); //repeat the execution as long as user enters y

out.close(); //Closes writer
}
  
//Validate method to check if even or odd number is only positive
//Writes output to outDataFile.txt
public static boolean Validate(int userAnswer){
//Returns true is greater than 0
if(userAnswer > 0)
{
return true;
}
return false;
}
  
//Reverse method reverses the number entered
//Writes output to outDataFile.txt
public static void Reverse(int num, BufferedWriter out) throws IOException {
String reversed = "";
while(num > 0)
{
int digit = num % 10;
reversed = reversed + digit + "";
num = num / 10;
}
  
out.write("The number reversed " + reversed);
out.newLine();
System.out.println("The number reversed " + reversed);
}
  
//Even method finds which numbers are even
//Writes output to outDataFile.txt
public static void Even(int num, BufferedWriter out) throws IOException {
String even = "";
while(num > 0)
{
int digit = num % 10;
if(digit%2 == 0)
even = digit + " " + even;
num = num / 10;
}
  
System.out.print("The even digits are " + even);
  
out.write("The even digits are " + even);
out.newLine();
}
  
//Odd method find which numbers are odd
//Writes output to outDataFile.txt
public static void Odd(int num, BufferedWriter out) throws IOException {
String odd = "";
  
while(num > 0)
{
int digit = num % 10;
  
if(digit%2 == 1)
odd = digit + " " + odd;
num = num / 10;
}
  
System.out.print(" The odd digits are " + odd + " ");
  
out.write(" The odd digits are " + odd + " ");
out.newLine();
}
}

Explanation / Answer

import java.util.Scanner; public class AccountMain { public static void selectAccount(){ System.out.println("Which account would you like to access?"); System.out.println(); System.out.println("1 = Business Account "); System.out.println("2 = Savings Account"); System.out.println("3 = Checkings Account"); System.out.println("4 = Return to Main Menu"); menuAccount(); } public static void menuAccount(){ BankMain main = new BankMain(); BankMainSub sub = new BankMainSub(); BankMainPart3 main5 = new BankMainPart3(); Scanner account = new Scanner(System.in); int actNum = account.nextInt(); if (actNum == 1){ System.out.println("*Business Account*"); sub.businessAccount(); } else if (actNum == 2){ System.out.println("*Savings Account*"); main.drawMainMenu(); } else if (actNum == 3){ System.out.println("*Checkings Account*"); main5.checkingsAccount(); } else if (actNum == 4){ BankMain.menu(); } }

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