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

Write a Java program, using appropriate methods and parameter passing, that obta

ID: 3584903 • Letter: W

Question

Write a Java program, using appropriate methods and parameter passing, that obtains from the user the following items: a person's age, the person's gender (male or female), the person's email address, and the person's annual salary. The program should continue obtaining these details for as many people as the user wishes. As the data is obtained from the user validate the age to ensure that the value lies in the range 20 to 30 inclusive; you may assume that the user will enter whole numbers only. validate the salary to ensure that it lies in the range 40000 to 150000 inclusive; you may assume that the user will enter whole numbers only. write the data to a text file named salaries.txt in the following format: agegenderemail address:salary Note: each line of data represents one person and each data value is separated by a semicolon () Hence the data in the file that you create would look similar to: 22 male;brad@gmail.com;65000 20 female;lucy@icloud.com;72000 21 female;angela.Samson@yahoo.com.au;51000 23-female;m_king@me.com;125000 When the program terminates it should display to the screen how many of the people were male and how many were female that were written to the file.

Explanation / Answer

package snippet;

import java.io.BufferedWriter;

import java.io.FileWriter;

import java.io.IOException;

import java.util.Scanner;

public class UserData {

private static final String FILENAME = "salaries.txt";

public static void main(String[] args) {

BufferedWriter bw = null;

FileWriter fw = null;

String c;

try {

fw = new FileWriter(FILENAME);

bw = new BufferedWriter(fw);

do {

Scanner sc = new Scanner(System.in);

System.out.println();

System.out.print("Enter age: ");

int age = sc.nextInt();

if(age < 20 || age >30) {

System.out.println("You have entered the wrong age. Enter age between 20 and 30. Run the program again");

break;

}

System.out.println();

System.out.print("Enter salary: ");

int salary = sc.nextInt();

if(salary < 40000 || age > 150000) {

System.out.println("You have entered the wrong salary. Enter salary between 40000 and 150000. Run the program again");

break;

}

String gender;

System.out.println();

System.out.print("Enter gender (Male/Female): ");

gender = sc.next();

String emailId;

System.out.println();

System.out.print("Enter emailID: ");

emailId = sc.next();

bw.write(age+";"+gender+";"+emailId+";"+salary);

System.out.println("Do you want to continue (Y/N)");

c = sc.next();

}while(c.equals("Y") || c.equals("y"));

System.out.println("Done");

} catch (IOException e) {

e.printStackTrace();

} finally {

try {

if (bw != null)

bw.close();

if (fw != null)

fw.close();

} catch (IOException ex) {

ex.printStackTrace();

}

}

}

}

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