JAVA Programming No Arrays please. The Program: You will randomly generate an ID
ID: 671033 • Letter: J
Question
JAVA Programming
No Arrays please.
The Program: You will randomly generate an ID number for the employee and then check to see if it is in use. ID's in use will be stored in a file.
Create a class called IDNumber, the code written must first generate an ID number in the format XXX-L where each X is a digit within the range 0-9 and the L is a letter within the range A-M.
Use a loop to generate digit by digit, then the ID number will then need to be put together in a String variable.
Remember do not use an array.
Explanation / Answer
package mani;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Random;
public class testing {
public static void main(String[] args) throws FileNotFoundException{
String[] inuse=new String[10];
inuse[0]="123-A";
inuse[1]="345-B";
Random r=new Random();
String IDNumber="";
for(int i=0;i<3;i++){
IDNumber=IDNumber+Integer.toString(r.nextInt(10));
}
PrintWriter writer = new PrintWriter("idnumber.txt");
writer.println("The first line");
writer.println("The second line");
writer.close();
IDNumber=IDNumber+"-";
char t=(char)(65+r.nextInt(13));
IDNumber=IDNumber+t;
for(String k:inuse){
if(k==IDNumber){
System.out.println("IDnumber in use!!!"+IDNumber);
writer.write(IDNumber);
}else{
System.out.println("IDnumber not in use!!!"+IDNumber);
}
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.