Line Numbers

Displaying line numbers in the output

Page 1 of 1

2 Replies - 697 Views - Last Post: 31 October 2010 - 09:41 PM Rate Topic: -----

#1 Perplexedwon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 01-March 10

Line Numbers

Posted 31 October 2010 - 09:28 PM

I need to write a program that will ask for the name of a file and display the contents of that file. The program should also display a line number and a colon preceeding each line of the file. The line numbering should start with 1.

I have written the program and created a quick text file on my Desktop to test it out and it list the contents of the file. However, I have not been able to get the line numbers and colon to list. I tried to use the random class, but didn't get what I was looking for, so I removed it. So far, here is what I have (this will do what I want except number each line in my output.)

/*********************************************************************************
Programer:  Paul J. Williams																		*
Date:			October 31,2010																		*
Purpose:		This program will read the contents of a file and display each line	*
				with the sequential line number to left of the line.						*
**********************************************************************************/

import java.util.Scanner;		// Needed for the Scanner class
import java.io.*;					// Needed for the file and IOException

public class ReadFile
{
	public static void main(String[] args) throws IOException
	{
	
		// Create a Scanner object for the input
		Scanner keyboard = new Scanner(System.in);
		
		// Get the filename from the user
		System.out.println("Enter the name of the file: ");
		String filename = keyboard.nextLine();
		
		// Open the file
		File file = new File(filename);
		Scanner inputFile = new Scanner(file);
		
		// Read all the lines from the file
		while (inputFile.hasNext())
		{
			// Read the next line
			String friendlist = inputFile.nextLine();
			
			// Display the last line read from the file
			System.out.println(friendlist);
		}
		
		// Close the file
		inputFile.close();

	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: Line Numbers

#2 SegFaulty  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 14
  • View blog
  • Posts: 35
  • Joined: 11-October 10

Re: Line Numbers

Posted 31 October 2010 - 09:36 PM

Here's how I would personally do it:

//make an int that will count the line number
int linenumber = 1;
while(inputFile.hasNext()){
     String friendlist = inputFile.nextLine();
     //print out the number and the line
     System.out.println(linenumber+": "+friendlist);
     //add one to the number to make it correct for the next run through the loop
     linenumber++;
}


Was This Post Helpful? 1
  • +
  • -

#3 Moshambi  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 8
  • View blog
  • Posts: 280
  • Joined: 20-November 07

Re: Line Numbers

Posted 31 October 2010 - 09:41 PM

View PostSegFaulty, on 31 October 2010 - 09:36 PM, said:

Here's how I would personally do it:

//make an int that will count the line number
int linenumber = 1;
while(inputFile.hasNext()){
     String friendlist = inputFile.nextLine();
     //print out the number and the line
     System.out.println(linenumber+": "+friendlist);
     //add one to the number to make it correct for the next run through the loop
     linenumber++;
}



I was just about to post that exact same thing. That is what I would do too.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1