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 374,011 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 2,783 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
Views: 148 | Replies: 1
Reply
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  
Join Date: May 2008
Posts: 35
Reputation: alpe gulay is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
alpe gulay's Avatar
alpe gulay alpe gulay is offline Offline
Light Poster

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

  #2  
May 15th, 2008
.,'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
.,'Baby Pro'.,
what you are today!
it's because on what you did yesterday'
what you did today!
is what in future you will be...'
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb Java Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the Java Forum

All times are GMT -4. The time now is 10:50 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC