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

Problem Description: Write a program in JAVA that reads a text file, ask the use

ID: 3630246 • Letter: P

Question

Problem Description:
Write a program in JAVA that reads a text file, ask the user for name and ID, if the name and ID is a match than the program will ask for a code name to assign to the user and writes the name, ID and code name in a different file. The input file name should be indata.txt the output file name should be your last plus first initial with TXT extension. (Example: behif.txt).

Content of input file indata.txt:
Darth Maul 1234
Allana Solo 2233
R2-D2 4455
Luke Skywalker 6677
Leia Organa 8899
Darth Vader 1100

So if the user R2-D2 enters the code name “bigdog” the output file will look like the following:

Darth Maul 1234 none
Allana Solo 2233 none
R2-D2 4455 bigdog
Luke Skywalker 6677 none
Leia Organa 8899 none
Darth Vader 1100 none

Explanation / Answer

public static void main(String[] args) throws IOException
{
// arrays to store names, ids, and code names
ArrayList<String> names = new ArrayList<String>();
ArrayList<Integer> ids = new ArrayList<Integer>();

// read file
Scanner filein = new Scanner(new File("indata.txt"));
while(filein.hasNextLine())
{
String line = filein.nextLine().trim();
// last word is id
ids.add(Integer.parseInt(line.substring(line.lastIndexOf(' ')+1)));
// rest is name
names.add(line.substring(0, line.lastIndexOf(' ')));
}
filein.close();

// ask for username and ID
Scanner kb = new Scanner(System.in);
System.out.print("Name? ");
String name = kb.nextLine().trim();
System.out.print("ID? ");
int id = kb.nextInt();

// go through names & IDs, writing to output file as we iterate
PrintWriter fileout = new PrintWriter(new File("behif.txt"));
for(int i = 0; i < names.size(); i++)
{
fileout.print(names.get(i)+" "+ids.get(i)+" ");
// check if names and ids match
if(names.get(i).equals(name) && ids.get(i) == id)
{
// ask for code name
System.out.print("Code name? ");
fileout.print(kb.nextLine().trim());
}
// else print "none"
else
{
fileout.print("none");
}
fileout.println();
}
// save file
fileout.close();
}

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