Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Graph Databases In Action

Authors: Dave Bechberger, Josh Perryman

1st Edition

1617296376, 978-1617296376

More Books

Students also viewed these Databases questions

Question

5. Who should facilitate the focus group?

Answered: 1 week ago