7 Replies - 374 Views - Last Post: 06 August 2017 - 11:08 AM Rate Topic: -----

#1 afg229  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 21-October 16

Need help formatting data on an output file.

Posted 06 August 2017 - 10:04 AM

Create a Java NetBeans project named AddLineNumbers with the main class named AddLineNumbers . This program will read data from a text file "dataInput.txt" and while there are more records, prepend a line number to the text data and output the concatenated data to "dataOutput.txt" implementing a try/catch block to catch any file errors thrown by the JVM. After you have created your NetBeans project AddLineNumbers and before you attempt to execute your application download and/or copy the text data file dataInput.txt from below to your AddLineNumbers project folder.
When I run the program, the data written onto the output file from the input file is written horizontally. Please try to use basic terms in your answer as I am a beginner in programming and am still not used to the terminology. The code is below. I will also provide the dataInput.txt and dataOutput.txt below:
package addlinenumbers;

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

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
         /* Declare variable identifiers to handle text data input from the file and count/increment the linenumber */
        int count=0;
        FileOutputStream fop = null;
        File fileIP,fileOP;
        /* Start try block */
        try {
        /* Instantiate Scanner object for data input from file “dataInput.txt”. There should be no path specification as your input file should be located in your AddLineNumbers project folder */
            Scanner input = new Scanner("dataInput.txt");
            fileIP = new File(input.nextLine());
            input = new Scanner(fileIP);
        /* Instantiate a FileOutputStream object for file “dataOutput.txt”. Do not specify a path. Your file will be created in the project folder by NetBeans */
            fileOP = new File("dataOutput.txt");
            fop = new FileOutputStream(fileOP);
        /* Instantiate a PrintWriter object for writing data to your FileOutputStream object */
            PrintWriter pw = new PrintWriter(fileOP);
        /* Using a while loop control structure, read one line of data from input file, prepend the line number to data read from the file and output concatenated data to output file */
            while (input.hasNextLine()) {
                String line = input.nextLine();
                pw.write(++count + " " +line);
            }
        /* Close files and output message indicating the file has been written */
            input.close();
            pw.close();
            System.out.println("The file has been written");
        /* End try block */
        }
        /* Code FileNotFoundException and IOException catch blocks. Call getMessage(), toString() and printStackTrace() methods on exception object thrown */
        catch (FileNotFoundException fne){
            System.out.println(fne.getMessage());
            System.out.println(fne.toString());
            fne.printStackTrace();
        }
        catch (IOException ioe){
            System.out.println(ioe.getMessage());
            System.out.println(ioe.toString());
            ioe.printStackTrace();
        }
    }
    
}


dataInput.txt:

/** data input
*    Anderson, Franceschi
*/

public class Hello
{
	public static void main( String [] args )
	{
		System.out.println( "Hello" );
	}
}



dataOutput.txt:
1 /** data input2 *    Anderson, Franceschi3 */4 5 public class Hello6 {7 	public static void main( String [] args )8 	{9

This post has been edited by ndc85430: 06 August 2017 - 10:35 AM
Reason for edit:: Added code tags.


Is This A Good Question/Topic? 0
  • +

Replies To: Need help formatting data on an output file.

#2 NormR  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 635
  • View blog
  • Posts: 4,815
  • Joined: 25-December 13

Re: Need help formatting data on an output file.

Posted 06 August 2017 - 10:14 AM

Quote

data written onto the output file from the input file is written horizontally.

I assume you are saying that all the data is written to one line.
If you want data to be written to different lines, add a newline character to the output at the place where you want one line to end. For example add a "\n" character.
Different text editors and OS use different new line characters.

Some methods will add new line characters for you. For example the println() method adds a new line character(s).

This post has been edited by NormR: 06 August 2017 - 10:33 AM

Was This Post Helpful? 0
  • +
  • -

#3 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3416
  • View blog
  • Posts: 15,400
  • Joined: 20-September 08

Re: Need help formatting data on an output file.

Posted 06 August 2017 - 10:26 AM

Quote

For example the println() method adds a new line character.

More precisely, it creates a new line for whatever platform you're on. That might involve more than one character
Was This Post Helpful? 0
  • +
  • -

#4 afg229  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 21-October 16

Re: Need help formatting data on an output file.

Posted 06 August 2017 - 10:26 AM

View PostNormR, on 06 August 2017 - 05:14 PM, said:

Quote

data written onto the output file from the input file is written horizontally.

I assume you are saying that all the data is written to one line.
If you want data to be written to different lines, add a newline character to the output at the place where you want one line to end. For example add a "\n" character.
Different text editors and OS use different new line characters.

Some methods will add new line characters for you. For example the println() method adds a new line character.

So i added the newline character and the dataOutput.txt file output still came out kind of funky:

1
/** data input2
*    Anderson, Franceschi3
*/4
5
public class Hello6
{7
	public static void main( String [] args )8
	{9
		System.out.println( "Hello" );10
	}11
}

This post has been edited by ndc85430: 06 August 2017 - 10:36 AM
Reason for edit:: Added code tags.

Was This Post Helpful? 0
  • +
  • -

#5 NormR  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 635
  • View blog
  • Posts: 4,815
  • Joined: 25-December 13

Re: Need help formatting data on an output file.

Posted 06 August 2017 - 10:34 AM

Quote

the dataOutput.txt file output still came out kind of funky:

Please explain what is wrong with the output.
What do you want to change?

This post has been edited by NormR: 06 August 2017 - 10:35 AM

Was This Post Helpful? 0
  • +
  • -

#6 afg229  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 21-October 16

Re: Need help formatting data on an output file.

Posted 06 August 2017 - 10:41 AM

View PostNormR, on 06 August 2017 - 05:34 PM, said:

Quote

the dataOutput.txt file output still came out kind of funky:

Please explain what is wrong with the output.
What do you want to change?

It should be something like this:
1 public class Hello
2 {
3 public static void main( String [] args )
4 {
5 System.out.println( "Hello" );
6 }
7 }
Was This Post Helpful? 0
  • +
  • -

#7 NormR  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 635
  • View blog
  • Posts: 4,815
  • Joined: 25-December 13

Re: Need help formatting data on an output file.

Posted 06 August 2017 - 10:47 AM

To get that output, you need to build a String with the leading number followed by the data and then output that String followed by the new line character.

Somehow the ordering of the number, data and newline character is messed up.
Was This Post Helpful? 0
  • +
  • -

#8 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3416
  • View blog
  • Posts: 15,400
  • Joined: 20-September 08

Re: Need help formatting data on an output file.

Posted 06 August 2017 - 11:08 AM

pw.printf("%02d %s%n", ++count,line); // (For lines <= 99 in number)

would probably be more appropriate
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1