Question: I would like to create a CountAdder class whose objects have a counter and a method that takes a String and adds the latest counter
I would like to create a CountAdder class whose objects have a counter and a method that takes a String and adds the latest counter value to the beginning of it. You shouldn't hesitate to do whatever is necessary to get that processing out of the I/O code.
import java.util.Scanner;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.io.FileOutputStream;
public class HasNextLineUsingResourcesDemo { public static void main(String[] args) { try (Scanner inputStream = new Scanner(new FileInputStream("ch10/original.txt")); PrintWriter outputStream = new PrintWriter(new FileOutputStream("ch10/numbered.txt")); ) { int count = 0; while (inputStream.hasNextLine( )) { String line = inputStream.nextLine( ); count++; outputStream.println(count + " " + line); } } catch(FileNotFoundException e) { System.out.println("Problem opening files."); e.printStackTrace(); System.exit(0); } } } Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
