This is just for intro class. 1. A user must be able to enter first name and las
ID: 670220 • Letter: T
Question
This is just for intro class.
1. A user must be able to enter first name and last name, in that order, and you assign five random lottery numbers to that person and let the user enter names until they want to quit. You must tell them what they have to type to end the program. You must write all valid entries with “lastName, firstName: “ and assigned lottery numbers to a file. If the user enters no name or only puts a first name tell the user it is not a valid entry, do not write to the file, and prompt again. When the user quits write the winning lottery numbers to the file.
Requirements:
* Prompt user for first name and last name in that order
* Things like sentinel values and the number of lottery numbers assigned should be able to be modified easily with one line of code
* Verify user entered both first and last name prior to assigning lottery numbers and writing to file
* Notify the user of invalid entries and continue prompting
* Test your program with entries like “”, “Andy”, “Andy “
* Lottery numbers are integers randomly assigned between 1-100
* Write “lastname, firstname: x x x x x” to file for valid entries
* Once the user is done write the winning lottery numbers to the file
* Your source file should be named Lab4LastNameFI.java
* Output file should be called LotteryEntrants.txt
Sample Input:
E:BoxCS007Labs>java Lab4
Please enter first name and last name for entry into lottery(type quit to end):Kathryn Blythe
Please enter first name and last name for entry into lottery(type quit to end):Angelica Grant
Please enter first name and last name for entry into lottery(type quit to end):Rich Cuddy
Please enter first name and last name for entry into lottery(type quit to end):Ryan Curran
Please enter first name and last name for entry into lottery(type quit to end):Paige Haring
Please enter first name and last name for entry into lottery(type quit to end):Andy
Andy Is not a valid entry try again
Please enter first name and last name for entry into lottery(type quit to end):quit
E:BoxCS007Labs>java Lab4
Sample Output File:
Blythe, Kathryn: 91, 20, 3, 32, 94
Grant, Angelica: 34, 71, 75, 80, 28
Cuddy, Rich: 78, 66, 14, 88, 52
Curran, Ryan: 8, 30, 54, 56, 98
Haring, Paige: 16, 79, 9, 76, 6
Winning Lottery Numbers are: 60 5 23 43 10
Explanation / Answer
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Random;
import java.util.Scanner;
public class Lab4LastNameFI {
public static int NO_OF_ENTRIES = 5;
static Random randomGenerator = new Random();
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String output = "";
int count = 0;
while(true) {
System.out.println("Please enter first name and last name for entry into lottery(type quit to end):");
String str = scan.nextLine();
count++;
//System.out.println("-->"+str);
if(str.trim().equalsIgnoreCase("quit"))
break;
//other wise read the input
if(str != null && !str.trim().equals("")) {
count = str.trim().split(" ").length;
// System.out.println("count = "+ count+" ->"+str+"#");
if(count < 2) {
if(count < 1)
System.out.println("Enter valid first name");
else if(count < 2)
System.out.println("Enter valid last name");
} else {
//now generate random numbers and assign to the output
output += str+":";
for(int i = 0; i < NO_OF_ENTRIES; i++) {
int rand= randomGenerator.nextInt(100) ;
output += " "+rand;
}
output += " ";
count = 0;
}
}
}
if(output != null && !output.equals("")) {
//Now generate winning lottery numbers
output += "Winning Lottery Numbers are:";
for(int i = 0; i < NO_OF_ENTRIES; i++) {
int rand= randomGenerator.nextInt(100) ;
output += " "+rand;
}
//Now write to file
writeToFile("LotteryEntrants.txt", output);
}
}
public static void writeToFile(String fileName, String output) {
try {
File file = new File(fileName);
// if file doesnt exists, then create it
if (!file.exists()) {
file.createNewFile();
}
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
bw.write(output);
bw.close();
System.out.println("Done");
} catch (IOException e) {
e.printStackTrace();
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.