Question

Hi there, I am just having a hard time trying to do this code. Question is:...

Hi there,
I am just having a hard time trying to do this code.
Question is: from the given file display all the departments that don't have space. Make the output in alphabetical order.
Code is supposed to be in JAVA

********workplace.txt********
firstname   lastname   department
Peggie MacGeffen  Legal
Katina Ronci  Training
Anna-maria Allmen Legal
Deana  Fontes Product Management
Winny  Bentson    Services
Mace   Bellard    Training
Ulrica Gaythwaite Product Management
Dillie McGovern   Engineering
Ana    Bakeup Marketing
Pepi   Hancill    Human Resources
Jacky  Maasz  Accounting
Oona   Gullen Sales
Pippy  Millis Support
Toby   Gagg   Product Management
Man    Perrycost  Services
Rodolphe   Wootton    Accounting
Marvin Sidney Legal
Beau   Kedslie    Support
Archaimbaud    Wayon  Training
Jakob  Meynell    Services
Tiena  Murrum Engineering
Arther Morrant    Legal
Lana   Deer   Human Resources
Wynne  Nevill Research and Development
Benedicto  Hughf  Product Management
Urbanus    Nardoni    Engineering
Cybill Mulbry Support
Honey  Marlen Human Resources
Claude Brunn  Business Development
Symon  Wardingly  Research and Development
Glendon    Jakucewicz Support
Lauren Hollows    Support
Jamima Wilshere   Support
Debor  Everett    Sales
Sutherland Paulich    Business Development
Imojean    Gerhartz   Sales
Edita  Laurens    Services
Jason  Caesmans   Human Resources
Leeann MacNeillie Research and Development
Gwenni Foyster    Support
Nata   Fargie Product Management
Tierney    Dummigan   Services
Codie  Hinz   Human Resources
Jaymie Bellon Legal
Carla  Cruden Engineering
Addy   Bodsworth  Sales
Gina   Kleinholz  Business Development
Mathian    Dabel  Product Management
Sadella    Kemmis Legal
Zak    Adame  Support
Demetre    Laffoley-Lane  Marketing
Claiborne  Almeida    Sales
Alain  Ingerson   Services
Philippine Grinyer    Accounting
Margaret   Shurrocks  Accounting
Gus    Gately Support
Janek  Kornyakov  Business Development
Barbaraanne    Gallienne  Support
Kanya  Norway Product Management
Madelin    Hugnet Marketing
Margareta  Kenner Product Management
Remington  McPeck Human Resources
Ronni  Defries    Services
Tuesday    Baudoux    Engineering
Madelaine  Bartusek   Engineering
Juliane    Ganniclifft    Business Development
Myrna  Pidduck    Human Resources
Tull   Lovel  Legal
Jacynth    Sharnock   Training
Astra  Puller Legal
Artair Ruck   Services
Janot  Hardinge   Product Management
Mackenzie  Berrecloth Accounting
Angelle    Thorald    Marketing
Jorrie Ruppert    Sales
Kori   Hubbucke   Business Development
Tildy  Swanton    Services
Janifer    Birdsall   Research and Development
Fanya  Bresnahan  Services
Brenda Aitken Legal
Farrel Habron Human Resources
Farley Pacey  Marketing
Sarina Pagen  Sales
Emmery Snoad  Product Management
Blinnie    Warriner   Research and Development
Lemar  Willard    Research and Development
Carena Littlekit  Research and Development
Kathryn    Taks   Support
Galvin Roncelli   Support
Ingeberg   Storrie    Accounting
Madonna    Fallens    Research and Development
Shannen    Matiewe    Product Management
Tobe   Stanion    Marketing
May    Hamber Legal
Nickola    Wernham    Product Management
Cherianne  Luesley    Sales
Lucas  Baumer Support
Bobina Boult  Research and Development
Kermit Gebuhr Human Resources
Nonah  Lafoy  Engineering
Andrej Beagrie    Services
Florella   Hollows    Human Resources
Beulah Burley Research and Development
Ralf   Chamberlaine   Sales
Terencio   Mullard    Legal
Rubie  Horrod Research and Development
Stu    Prester    Business Development
Salomone   Coldman    Marketing
Cecilia    Scotsbrook Marketing
Korey  Aire   Training
Darsey Lettsom    Business Development
Luis   Kleinholz  Marketing
Ninette    Foyston    Support

***There was more to this .txt file but all of it doesn't fit**

********My Code********

import java.io.*;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeSet;

public class Departments
{

  public static void main(String[] args) throws IOException {

    File file = new File("workplace.txt");
    Scanner fileReader = new Scanner(file);
    fileReader.nextLine();
    Set<String> data = new TreeSet<>();

    String firstName;
    String lastName;
    String departments;
    String line = " ";

    System.out.println(data);
  }
}

*********Required Output*******

Accounting\n
Engineering\n
Legal\n
Marketing\n
Sales\n
Services\n
Support\n
Training\n
0 0
Add a comment Improve this question Transcribed image text
Answer #1

JAVA CODE

Text Code

import java.io.*;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeSet;

public class Departments
{

    public static void main(String[] args) throws IOException {

        File file = new File("workplace.txt");
        Scanner fileReader = new Scanner(file);
        String headerLine = fileReader.nextLine();

        //Note here TreeSet is defined which stores the values in natural sorting order
        //For Strings natural sorting order is alphabetical
        Set<String> data = new TreeSet<>();

        String firstName;
        String lastName;
        String departments;
        String line = " ";

        //reading the line one by one
        while(fileReader.hasNextLine()) {

            line = fileReader.nextLine();

            //Split line on space
            String[] values = line.split("\\s+");
            //If there are more than 3 values in the splitted array then it is sure that
            //department name has atleast one space
            if (values.length == 3) {

                //3rd value is of the department
                //Since array starts from 0 3rd value is at index 2
                String department = values[2];

                //inserting the value in the set
                data.add(department);
            }
        }

        //iterating in the set
        //and since values are stored in sorted manner in tree set
        //output will be sorted as well
        for (String ss : data) {
            System.out.println(ss);
        }

        //Below line is to print all the departments in one line
        //Uncomment it if required
        //System.out.println(data);
    }
}

INPUT

AS given in the question

OUTPUT

Please comment for any further assistance

Add a comment
Know the answer?
Add Answer to:
Hi there, I am just having a hard time trying to do this code. Question is:...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • What is the Value-Chain analysis for eHarmony? eHarmony (the online dating website) VALUE CHAIN ANALYSIS (example...

    What is the Value-Chain analysis for eHarmony? eHarmony (the online dating website) VALUE CHAIN ANALYSIS (example printing company) Firm infrastructure: General management, Planning Management, Legal, Finance, Accounting Human Resource Management: Recruitment, Retention, Training, R&D Support Processes Technology Development: Continuous improvement in printing & finishing assets Procurement of Resources: Purchasing paper, printing consumables and other finishing consumables Competitive Advantage Inbound Logistics Outbound Logistics Marketing and Sales Service Operations Primary Business Processes • Reception • Storage • Inventory control • Transportation planning...

  • Modification to be completed by group member 4: In the processLineOfData method, write the code to...

    Modification to be completed by group member 4: In the processLineOfData method, write the code to handle case "M" of the switch statement such that: A Manager object is created using the firstName, lastName, salary, and bonus local variables. Notice that salary and bonus need to be converted from String to double. You may use parseDouble method of the Double class as follows:               Double.parseDouble(salary) Call the parsePaychecks method in this class passing the Manager object created in the previous step...

  • The below pic has the drop down options to choose from. I am in need of...

    The below pic has the drop down options to choose from. I am in need of understanding the Directing section. Have some of them filled out but do not know if or what is correct so please ignore the answers that I have selected in the jpeg. Thank you. i Requirements 1. For each of the six departments, choose all appropriate decisions/actions for each of the three stages of management (planning, directing, and controlling). 2. For each of the decisions/actions...

  • In the processLineOfData, write the code to handle case "H" of the switch statement such that:...

    In the processLineOfData, write the code to handle case "H" of the switch statement such that: An HourlyEmployee object is created using the firstName, lastName, rate, and hours local variables. Notice that rate and hours need to be converted from String to double. You may use parseDouble method of the Double class as follows:               Double.parseDouble(rate) Call the parsePaychecks method in this class passing the HourlyEmployee object created in the previous step and the checks variable. Call the findDepartment method...

  • Hi, I would like to have answer in the below case. Step 1. In groups of...

    Hi, I would like to have answer in the below case. Step 1. In groups of three to five students, assume that you are a consulting team to a family business. The family has ased an inheritance to acquire a medium-sized pharmaceu- dical company. Last year, sales were down 10 percent from the previous year. Indeed, business has declined over the past three years, even though the pharmaceutical industry has been growing. The family that acquired the business has asked...

  • 1. Highlanders Co., based in Chicago, USA, is a manufacturer of sports equipment. Highlanders Co. relocates...

    1. Highlanders Co., based in Chicago, USA, is a manufacturer of sports equipment. Highlanders Co. relocates its data processing service to a company based in Mexico, while retaining control over the process. This process is known as _____. offshoring franchising outsourcing insourcing dumping 2. Identify a key aspect to consider in developing a code of ethics. Ensure that employee group meetings are not carried out on a complete draft version. Make certain that the headings of the code sections are...

  • Please help. I need a very simple code. For a CS 1 class. Write a program...

    Please help. I need a very simple code. For a CS 1 class. Write a program in Java and run it in BlueJ according to the following specifications: The program reads a text file with student records (first name, last name and grade). Then it prompts the user to enter a command, executes the command and loops. The commands are the following: "printall" - prints all student records (first name, last name, grade). "firstname name" - prints all students with...

  • I am having a hard time solving this problem from Chapter 5 in the Book of...

    I am having a hard time solving this problem from Chapter 5 in the Book of Fundamental Accounting Principles edition 15 volume 1. thank you Required 1. Record and post the appropriate closing entries. 2. Prepare a post-closing trial balance. Chapter 5 Cumulative Comprehension Problem: Echo Systems Echo Systems-perpetual or periodic Note: Solutions are available for both perpetual and periodic. (The first three segments of this comprehensive problem were presented in Chapters 2, 3, and 4. If those segments have...

  • Using Java, I am trying to read in a file with the format outlined below. "AARON,...

    Using Java, I am trying to read in a file with the format outlined below. "AARON, ELVIA J"   WATER RATE TAKER   WATER MGMNT   "$87,228.00" "AARON, JEFFERY M"   POLICE OFFICER   POLICE   "$75,372.00" "AARON, KARINA"   POLICE OFFICER   POLICE   "$75,372.00" "AARON, KIMBERLEI R"   CHIEF CONTRACT EXPEDITER   GENERAL SERVICES   "$80,916.00" "ABAD JR, VICENTE M"   CIVIL ENGINEER IV   WATER MGMNT   "$99,648.00" "ABARCA, ANABEL"   ASST TO THE ALDERMAN   CITY COUNCIL   "$70,764.00" "ABARCA, EMMANUEL"   GENERAL LABORER - DSS   STREETS & SAN   "$40,560.00" "ABBATACOLA, ROBERT J"   ELECTRICAL MECHANIC  ...

  • In the processLineOfData method, write the code to handle case "H" of the switch statement such...

    In the processLineOfData method, write the code to handle case "H" of the switch statement such that: • An HourlyEmployee object is created using the firstName, lastName, rate, and hours local variables. Notice that rate and hours need to be converted from String to double. You may use parseDouble method of the Double class as follows: Double.parseDouble(rate) Call the parsePaychecks method in this class passing the Hourly Employee object created in the previous step and the checks variable. Call the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT