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

2. Complete the Java program below so that it reads the text file dogNames.txt a

ID: 3815058 • Letter: 2

Question

2. Complete the Java program below so that it reads the text file dogNames.txt and writes its contents to another text file, "doggoneData.txt".

The file dogNames.txt contains just the single word "Tippy", so the output in "doggoneData.txt" should simply look like this:

Tippy

Your task is to complete the code below by using the variable name along with the appropriate Scanner and PrintWriter methods.

  String name;
  Scanner inputFile = new Scanner(new FileReader("dogNames.txt"));   
  PrintWriter outputFile = new PrintWriter("doggoneData.txt");

//code here

inputFile.close();
  outputFile.close();

3. Complete the Java program below so that it reads the text file dogYears.txt and writes its contents to another text file "doggoneData.txt". Here the input file gives ages in dog years, and the output file reports these figures in human years (so Tippy is 2 in the input file, and 14 - 7 times older - in the output file.)

The output in the file "doggoneData.txt", which gives dog ages in human years, should be formatted like this:

Tippy 14
Rex 49
Desdemona 35

Your task is to complete the code below by using the appropriate Scanner and PrintWriter methods.

  String name1, name2, name3;
  int age1, age2, age3;
  Scanner inputFile = new Scanner(new FileReader("dogYears.txt"));   
  PrintWriter outputFile = new PrintWriter("doggoneData.txt");   

//code here

inputFile.close();
  outputFile.close();

4. Suppose an external file is made up entirely of integers. In the model we've been using in this unit, the file is actually read in, line by line, as a sequence of Strings. Use String method split inside processLine() to produce individual tokens that look like numbers, but are actually Strings that are composed of digits. To convert each token from String format to integer format, you need to use the static parseInt() method from the Integer class. Thus

int birthYear = Integer.parseInt("1983");

correctly stores the integer 1983 into the birthYear integer variable.

For this assignment you should create a complete processLine method in the EchoSum class, so that it transforms each number in each line to integer format, and then sums the entries on the line and prints their sum on a separate line. For example, if the external text file looks like this:



your program should display this:


import java.io.*;

public class EchoSum extends Echo
{

  public EchoSum (String datafile) throws IOException
  {
    super(datafile);
  }

  // Prints the sum of each line.
  public void processLine(String line){

//code here

} //end method
} //end class

5. For this problem we extend the Echo class to a class called EchoNoSpace. Here the external text file is written to the console as before, but now all spaces are removed first before lines are displayed. Thus a line like

The best things in life are free

becomes

Thebestthingsinlifearefree

The code for doing this uses split from the String class. The links below give the classes that participate in the solution, and the answer block below provides the implementation for the processLine() method in that derived class.

The EchoNoSpace class has an addtional integer attribute, wordCount, which is initialized to 0 in the class constructor. After the file has been processed, this variable should hold the exact number of words (tokens) in the file. The EchoTester code at the link below reports this value as follows:

System.out.println("wordCount: " + e.getWordCount());

For this assignment, edit the processLine() code in the answer box so that the EchoTester's main() method reports the correct value for the number of words in the file. Important: your code should count non-empty words only. Remember that split may place empty strings - strings of length 0 - in the array words, and thus your code must be able to ignore such entries.

import java.io.*;

public class EchoNoSpace extends Echo
{

  // the number of the words counted
  private int wordCount;

  public EchoNoSpace (String datafile) throws IOException
  {
    super( datafile);
    wordCount=0;
  }

  // Prints the given line without any spaces.
  // Overrides the processLine method in Echo class
  public void processLine(String line){

String result = "";

String[] words = line.split(" ");

for(String w : words)

{

result = result + w;

}

System.out.println(result);

}

  // returns the number of words in the file
  public int getWordCount(){
    return wordCount;
  } //end method
} //end class

Explanation / Answer

HI, I have answered Q2 and Q3.

Please repost others questions in separate post.

Please let me knwo in case of any issue in Q2 and Q3.

2)

String name;
Scanner inputFile = new Scanner(new FileReader("dogNames.txt"));   
PrintWriter outputFile = new PrintWriter("doggoneData.txt");

name = inputFile.next(); // reading name
outputFile.println(name);
//code here
inputFile.close();
outputFile.close();


3)

String name1, name2, name3;
int age1, age2, age3;
Scanner inputFile = new Scanner(new FileReader("dogYears.txt"));   
PrintWriter outputFile = new PrintWriter("doggoneData.txt");   
//code here
name1 = inputFile.next();
age1 = inputFile.nextInt();
name2 = inputFile.next();
age2 = inputFile.nextInt();
name3 = inputFile.next();
age3 = inputFile.nextInt();

outputFile.println(name1+" "+age1);
outputFile.println(name2+" "+age2);
outputFile.println(name3+" "+age3);
inputFile.close();
outputFile.close();