User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 428,196 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,147 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Java advertiser: Lunarpages Java Web Hosting

Don't think I'm getting "count" correct

Join Date: May 2008
Posts: 1
Reputation: eec322 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
eec322 eec322 is offline Offline
Newbie Poster

Don't think I'm getting "count" correct

  #1  
May 14th, 2008
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
AddThis Social Bookmark Button
Reply With Quote  
All times are GMT -4. The time now is 3:05 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC