Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

There is an error in my code. The following output is suppose to be as such below: the program should produce the following output: name:

There is an error in my code. The following output is suppose to be as such below:

the program should produce the following output:

name: Steven Edwards formal: Steven Edwards official: Edwards, Steven initials: S.E. name: Joshua Carter formal: Joshua Carter official: Carter, Joshua initials: J.C. Duplicate name "Joshua Carter" discovered name: Anthony Jackson formal: Anthony Jackson official: Jackson, Anthony initials: A.J. name: Barbara Carter formal: Barbara Carter official: Carter, Barbara initials: B.C. name: Edward Evans formal: Edward Evans official: Evans, Edward initials: E.E. name: Christopher White formal: Christopher White official: White, Christopher initials: C.W. Duplicate name "Christopher White" discovered name: Michelle Taylor formal: Michelle Taylor official: Taylor, Michelle initials: M.T. name: Melissa Baker formal: Melissa Baker official: Baker, Melissa initials: M.B. name: Anthony Young formal: Anthony Young official: Young, Anthony initials: A.Y. name: Michael Hall formal: Michael Hall official: Hall, Michael initials: M.H. --- 12 names processed.

please fix up the code so I can achieve the same output

My Code:

import java.io.*; import java.util.*; class Name { public Name(String last, String first) { this.last = last; this.first = first; } public Name(String first) { this("", first); } public boolean equals(Name same) { return first.equals(same.first) && last .equals(same.last); } public String toString() { return first + " " + last; }

public String getFormal() { return first + " " + last; } public String getOfficial() { return last + ", " + first; } public static Name read(Scanner scan) { if (!scan.hasNext()) return null; String last = scan.next(); String first = scan.next(); return new Name(last, first); } public String getInitials() { return Character.toUpperCase(first.charAt(0)) + "." + Character.toUpperCase(last.charAt(0))+"."; } private String first, last; public static void main(String [] args) throws Exception { Scanner scan = new Scanner(new File("names.text")); int count = 0; Name readName = read(scan); Name readLastName =null; while(readName != null) {

System.out.println("name: " + readName); System.out.println("formal: " + readName.getFormal()); System.out.println("official: " + readName.getOfficial()); System.out.println("initials: " + readName.getInitials()); System.out.println(); count++; readName = read(scan); if(readLastName!=null && readName!=null && readLastName.equals(readName)){ System.out.println("Duplicate name \'"+readName+"\' discovered"); } readLastName= readName;

} System.out.println("---"); System.out.println(count + " names processed."); } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Relational Database And SQL

Authors: Lucy Scott

3rd Edition

1087899699, 978-1087899695

More Books

Students also viewed these Databases questions