Hello,
I have completed most of my code but having difficulty with the delimiters. Although delimiters are not exclusively related to File I/O, majority of my code is based on therefore, I posted it in this sub-forum.
/** Program: Reads a text file with input, reads each line and sends it to the output file preceded by line numbers. The numbers are enclosed in delimiters so that the program can be used for numbering java source files */ import java.io.File; import java.io.FileNotFoundException; import java.io.PrintWriter; import java.util.Scanner; import java.util.*; public class inputOutputDelimiters { public static void main(String[] args) throws FileNotFoundException { // Prompt for the input and output file names Scanner console = new Scanner(System.in); System.out.print("Input file: "); String inputFileName = console.next(); System.out.print("Output file: "); String outputFileName = console.next(); // Construct the Scanner and PrintWriter objects for reading and writing File inputFile = new File(inputFileName); Scanner in = new Scanner(inputFile); in.useDelimiter("[^A-Za-z]+"); PrintWriter out = new PrintWriter(outputFileName); int counter = 1; while (in.hasNextLine()) { String input = in.nextLine(); // print the delimiter this scanner is using System.out.print(console.delimiter()); System.out.println(counter +" "+ input); counter++; } } }
Output:
Input file: input.txt
Output file: output.txt
\p{javaWhitespace}+1 Mary had a little lamb
\p{javaWhitespace}+2 Whose fleece was white as snow.
\p{javaWhitespace}+3 And everywhere that Mary went,
\p{javaWhitespace}+4 The lamb was sure to go!
Wanted output:
Input file: input.txt
Output file: output.txt
/*1 Mary had a little lamb
/*2 Whose fleece was white as snow.
/*3 And everywhere that Mary went,
/*4 The lamb was sure to go!
Question: Basically, how do I make the delimiter print what I want? I know you can hard code around it but the assignment is to do so... Tried my textbook and online but because I am a beginner most of the stuff that is online is foreign to me. I have tried to change what comes out through the delimiter command but it does not allow to do: System.out.print("/*" console.delimiter());... it creates a compile-time error.