Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: output using delimiters

  1. #1
    Junior Member
    Join Date
    Jun 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default output using delimiters

    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.


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: output using delimiters

    Quote Originally Posted by bkoo View Post
    Basically, how do I make the delimiter print what I want? ... it creates a compile-time error.
    What do you want?
    Always post the full text of error messages with your code and question.

  3. #3
    Junior Member
    Join Date
    Jun 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: output using delimiters

    There are no error messages. I stated what I want in the whole post.

    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.

Similar Threads

  1. how do I get this output?
    By my21 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 13th, 2013, 09:23 PM
  2. how could I output to a text area the output of a method
    By mia_tech in forum What's Wrong With My Code?
    Replies: 6
    Last Post: July 12th, 2012, 07:49 PM
  3. [SOLVED] Not sure how to work with multiple delimiters. Also, there is random tabbing.
    By javapenguin in forum What's Wrong With My Code?
    Replies: 5
    Last Post: April 17th, 2012, 08:23 PM
  4. [SOLVED] Splitting String by Multiple Delimiters
    By relixus in forum What's Wrong With My Code?
    Replies: 0
    Last Post: February 4th, 2011, 08:54 PM
  5. Help with output
    By jch02140 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: January 8th, 2011, 01:09 PM

Tags for this Thread