Pergunta

Please help me with this HW assignment. I am supposed to modify the EchoNumber class which extends the Echo class to count the number of characters in every line in a text file in addition to displaying the text. Here is the Echo class:

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

public class Echo{
  String fileName; // external file name
  Scanner scan; // Scanner object for reading from external file

  public Echo(String f) throws IOException
  {
    fileName = f;
    scan = new Scanner(new FileReader(fileName));
  }

  // reads lines, hands each to processLine
  public void readLines(){
    while(scan.hasNext()){
      processLine(scan.nextLine());
    }
    scan.close();
  }

  // does the real processing work
  public void processLine(String line){
    System.out.println(line);
  } 
}

Here is the EchoNumber class, notice where it says "Your code goes here":

import java.io.*;

public class EchoNumber extends Echo
{

  // the current line number
  private int lineNumber;

  public EchoNumber (String datafile) throws IOException
  {
    super( datafile);
    lineNumber=1;
  }

  // Prints a line with a leading line number and a trailing line length
  // Overrides the processLine method in Echo class
  public void processLine(String line){
    /* your code goes here */
  }
}

Here is the EchoTester class:

import java.io.*;
import java.util.Scanner;
public class EchoTester
{

  public static void main(String[] args)
  {
    // uses try/catch to handle IOExceptions in main
    try
    {
      String fileName;
      Scanner nameReader = new Scanner(System.in);
      System.out.println("Enter a file name");
      fileName = nameReader.nextLine();
      EchoNumber e = new EchoNumber(fileName);
      e.readLines();
    }
    catch(IOException e)
    {
      e.printStackTrace();
    }
  }
}

And finally the .txt file:

The best things in life are free
A stitch in time saves nine
Still waters run deep
He teaches ill who teaches all
You can not take it with you when you die
Better untaught than ill taught
Do not cross your bridges before you come to them
Soon learnt soon forgotten
Even a worm will turn
It was the last straw that broke the camels back
The way to a mans heart is through his stomach
If the stone fall upon the egg alas for the egg If the egg fall upon the stone alas for     the egg
Where there is a will there is a way
Marry in haste and repent at leisure
One tongue is enough for a woman
If you wish good advice consult an old man
The best advice is found on the pillow
All clouds bring not rain
You can not tell a book by its cover
No news is good news
Bad news travels fast
Live and let live
Birds of a feather flock together
Now is the time
For all good men who actually have the time
To come to the aid of the country in which they live

The output is supposed to be something like:

1 The best things in life are free-32

2 A stitch in time saves nine-27

3 Still waters run deep-21

4 He teaches ill who teaches all-30

5 You can not take it with you when you die-41

6 Better untaught than ill taught-31

7 Do not cross your bridges before you come to them-49

8 Soon learnt soon forgotten-26

9 It was the last straw that broke the camels back-48

Except without spaces between each line. For some reason it fuses into one paragraph if I did not separate each line.

Foi útil?

Solução

Something like this:

  public void processLine(String line){
    System.out.println(lineNumber + " " + line + "-" + line.length());
    ++lineNumber;
  }

I haven't tested it, so if it isn't 100% correct, I'll leave it as an exercise for you to complete, but it should put you on the right track. Good luck.

Outras dicas

THIS is the correct code. It is important that lineNumber++ is below the print statement!

System.out.println(lineNumber + " " + line + "-" + line.length()); lineNumber++;

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top