I'm suppose to create a problem that displays the contents of a file (that the user inputs), and then displays each line with a number in front and a colon after it. So, line 1 and 2 would print out:
1 lineFromFile :
2 lineFromFile :
But, it keeps printing out 1 in front of each line, so I think I didn't write the count part correctly. Can someone help? Here's what I've got.

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

public class orderOfFileLines
{
public static void main (String[ ] args) throws IOException
{


int number; //Loop control variable

//Create a Scanner object for keyboard input.
Scanner keyboard = new Scanner(System.in);

//Get the file name.
System.out.print("Enter the name of a file.");
String filename = keyboard.nextLine();

//Open the file.
File file = new File(filename);
Scanner inputFile = new Scanner(file);

//Read the lines from the file until no more are left.
while (inputFile.hasNext())
{
//Read the next line.
String line = inputFile.nextLine();

for (number = 1; number <=1; number++)
{
//Display the lines with number and ":".
System.out.println(number + line + ":");
}


}


//Close the file.
inputFile.close();

}//end main method
}//end class

.,'you dnt need to put a loop inside the while loop
you can made this one in this way...
try it!

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

public class orderOfFileLines
{
public static void main (String[ ] args) throws IOException
{


//int number; //Loop control variable

//Create a Scanner object for keyboard input.
Scanner keyboard = new Scanner(System.in);

//Get the file name.
System.out.print("Enter the name of a file : ");
String filename = keyboard.nextLine();

//Open the file.
File file = new File(filename);
Scanner inputFile = new Scanner(file);

//Read the lines from the file until no more are left.
int x=1;
while (inputFile.hasNext())
{
//Read the next line.
String line = inputFile.nextLine();
//Display the lines with number and ":".
System.out.println(x + line + ":");
x++;


}


//Close the file.
inputFile.close();

}//end main method
}//end class

-tell me how it works then okey!just email me

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.