answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Anon25 [30]
2 years ago
3

Write a program that reads in worked_example_1/babynames.txt and produces two files, boynames.txt and girlnames.txt, separating

the data for the boys and girls.

Computers and Technology
1 answer:
Lerok [7]2 years ago
4 0

Answer:

I am writing a JAVA program.

import java.util.Scanner;  //class to take input from user

import java.io.File;  //class for working with files

import java.io.FileNotFoundException;  // indicates that file could not be opened

import java.io.PrintWriter;  //used to print or formatted data

public class BabyNames{  //class name

public static void main(String[] args) throws FileNotFoundException{  //main function body throws FileNotFoundException to indicate if the file cannot be opened

File input_baby = new File("babynames.txt");  //creates object iFile and passes babynames.txt file name to it

File boy_output = new File("boynames.txt");  //creates object of File and passes boynames.txt name to it This file is to store boys names from babynames.txt

File girl_output = new File("girlnames.txt");  //creates object of File & passes  girlnames.txt name to it. To store girls names from babynames

String[] boy_names = new String[100];  //create an array to hold boys names

String[] girl_names = new String[100];  //creates an array to hold girls names

PrintWriter boyNames = new PrintWriter(boy_output);  //create object of PrintWriter for writing the separated boys names to boynames.txt file

PrintWriter girlNames = new PrintWriter(girl_output);  //create object of PrintWriter for writing the separated  girls names to girlnames.txt file

Scanner input = new Scanner(input_baby);  //creates Scanner class object to scan babyname.txt

int i = 0;  

while (input.hasNextLine()){  //checks if the babynames files has next line until the last line of the file

String str = input.nextLine();  //scans the current line

String[] strings = str.split(" ");  //split the line into array of strings/words based on the space " " delimiter

boy_names[i] = strings[1];  //array to store boys names and strings[1] locates the boys names in input file

girl_names[i++] = strings[3];  }  //array to store girls names strings[3] locates the girls names

for(i = 0; i < 100; i++){  //write the separated boys names to boynames.txt file and separated girls names in girlnames.txt file

           boyNames.write(boy_names[i]);

           boyNames.println();

           girlNames.write(girl_names[i]);

           girlNames.println();}  

input.close();  

boyNames.close();  

girlNames.close(); } }

Explanation:

The program has an input file babynames.txt and two text files to store separated boys and girls names. The while loop reads every line of the babynames.txt at each iteration and and splits each line into array of words strings based on the space delimiter. A line from babynames.txt file: 1 Michael 65,275 Jessica 46,470

boy_names[i] = strings[1] This line in loop basically locates boys names from each line of the babynames file. For the line given above. At first iteration of while loop when i=0 then boy_names[i] = strings[1] statement sets 1st index of strings array to the 0-th index of boy_names. Notice that 1st index of strings array is the second word of the above line i.e. Michael. So Michael is stored as 1st element of array boys_name.

girl_names[i++] = strings[3]; locates girls names in the same way as boy_names[i] = strings[1]

strings[3] means 3rd index of strings array Notice that 3rd index of strings array is Jessica.

There are total of 100 boys and 100 girls names in babynames.txt file so for(i = 0; i < 100; i++) this for loop prints these 100 separated boys and girls names in their respective files.

However this program only separates the boys and girls names and not their data such as in this line of input file Michael has data 65,275 and Jessica has 46,470. If you want to add this data into boynames.txt and girlnames.txt files then alter the above code. The altered code is attached.

create an array to store data of boys.

String[] boy_data = new String[100];

Create an array to store data of girls.

String[] girl_data = new String[100];

Now considering the same example 1 Michael 65,275 Jessica 46,470

boy_data[i] = strings[0] + " " + strings[1] + " " + strings[2];  the boy_data array stores data of boys i.e. the elements of 0th 1st and 2nd index. Notice that element at 0th index is the first element element i.e. 1, element at 1st index is Michael and element at 2nd index is 65275. So this is the data of Michael.

girl_data[i++] = strings[0] + " " + strings[3] + " " + strings[4];  }  the girl_data array stores data of girls i.e. the elements of 0-th 3rd and 4th index. Notice that element at 0-th index is the first element element i.e. 1, element at 3rd index is Jessica and element at 4th index is 46,470. So this is the data of Jessica.

Now the outer loop iterates is used to print the separated data of boys and girls. 1st inner loop has an if statement  if(boy_data[j].split(" ")[1].compareTo(boy_names[i]) == 0) which splits the boy_data and compareTo() method compares the first element of boy_data (Michael) to first element of the boy_names array (Michael). If both are same then it write the data i.e.  to boynames.txt 1 Michael 65,275. Same works for the girls too. The program along with its output is attached.

You might be interested in
The height of a small rocket y can be calculated as a function of time after blastoff with the following piecewise function: y 5
SOVA2 [1]

Answer:

High level Language

understand

Explanation:

rocket is 0...4433456u888

5 0
1 year ago
Write a cout statement that prints the value of a conditional expression. The conditional expression should determine whether th
MissTica

Answer:

The explanation to this question is given below in the explanation section.

Explanation:

#include <iostream>

#include <string>

using namespace std;

int main()

{

   int score;

   cout << "Enter Score: \n";

   cin>>score;

   if (score == 100)

//whether the value of the score variable is equal to 100

   {

       cout << "Perfect ";

//If so, it should print “Perfect”

   }

   else

   {

       cout << "Nice Try ";  

   }

 

}

     

     

7 0
2 years ago
Janis needs to select all the text in a single bullet point. Which action describes the easiest method to perform this task?
olganol [36]

Answer:

Triple-click the text in the bullet item is the easiest  way to select all text

Explanation:

5 0
2 years ago
A new company starts up but does not have a lot of revenue for the first year. Installing anti-virus software for all the compan
Feliz [49]

WAN domain which stands for Wide Area Network and consists of the Internet and semi-private lines. The <span>RISKS are: Service provider can have a major network outage, Server can receive a DOS or DDOS attack</span> and A FTP server can allow anonymously uploaded illegal software.





7 0
2 years ago
In this exercise you will debug the code which has been provided in the starter file. The code is intended to take two strings,
vampirchik [111]

Answer:

Here is the corrected program:

import java.util.Scanner; //to accept input from user

public class 02_14_Activity_one { //class name

    public static void main(String[] args) { //start of main method

    Scanner scan = new Scanner(System.in); //creates Scanner class object

  System.out.println("Enter first string"); //prompts user to enter first string

          String s1 = scan.nextLine(); //reads input string from user

  System.out.println("Enter second string"); //prompts user to enter second string

          String s2 = scan.nextLine(); //reads second input string from user

    System.out.println("Enter number of letters from each word"); //enter n

          int n = scan.nextInt(); //reads value of integer n from user

          System.out.println(s1.substring(0,n) + s2.substring(s2.length() - n ));

         } } //uses substring method to print a string made up of the first n letters of s1 and the last n letters of s2.

Explanation:

The errors were:

1.

Scanner scan = Scanner(System.in);

Here new keyword is missing to create object scan of Scanner class.

Corrected statement:

 Scanner scan = new Scanner(System.in);

2.

String s1 = nextLine();  

Here object scan is missing to call nextLine() method of class Scanner

Corrected statement:

String s1 = scan.nextLine();

3.

String s2 = Scanner.nextLine();

Here class is used instead of its object scan to access the method nextLine

Corrected statement:

String s2 = scan.nextLine();

4.

String n = scan.nextLine();

Here n is of type String but n is a whole number so it should be of type int. Also the method nextInt will be used to scan and accept an integer value

Corrected statement:

int n = scan.nextInt();

5.

System.out.println(s1.substring(1,n-1) + s2.substring(s1.length()-n));

This statement is also not correct

Corrected statement:

System.out.println(s1.substring(0,n) + s2.substring(s2.length() - n ));

This works as follows:

s1.substring(0,n) uses substring method to return a new string that is a substring of this s1. The substring begins at the 0th index of s1 and extends to the character at index n.

s2.substring(s2.length() - n ) uses substring method to return a new string that is a substring of this s2. The substring then uses length() method to get the length of s2 and subtracts integer n from it and thus returns the last n characters of s2.

The screenshot of program along with its output is attached.

7 0
2 years ago
Other questions:
  • One reason for using social media is to develop social and professional contacts. True or false?
    9·2 answers
  • Modern operating systems decouple a process address space from the machine’s physical memory. List two advantages of this design
    15·1 answer
  • What data discovery process, whereby objects are categorized into predetermined groups, is used in text mining?
    9·1 answer
  • Write the definition of a function printAttitude, which has an int parameter and returns nothing. The function prints a message
    14·1 answer
  • Consider the formula: G=D+(A+C^2)*E/(D+B)^3 Show the order that a processor would follow to calculate G. To do so, break down th
    10·1 answer
  • We have an internal webserver, used only for testing purposes, at IP address 5.6.7.8 on our internal corporate network. The pack
    15·1 answer
  • Assume that you are testing the Orders database introduced in Watt (2014) - Appendix C. Discuss the problems and possible conseq
    12·1 answer
  • Lucy is completing a project as part of a science class using materials she found online. Which of the following is MOST LIKELY
    5·1 answer
  • Accenture has partnered with a global construction company that specializes in building skyscrapers. The company wants better ma
    12·1 answer
  • Write a program that uses the Purchase class in 5.13. Set the prices to the following: Oranges: 10 for $2.99 Eggs: 12 for $1.69
    11·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!