•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 426,498 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,167 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: 193 | Replies: 1
•
•
Join Date: May 2008
Posts: 1
Reputation:
Rep Power: 0
Solved Threads: 0
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
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
•
•
•
•
•
•
•
•
DaniWeb Java Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Similar Threads
- Please help me "I WANT IT FOR TOMORROW" (C++)
- Please help me :"login form use drag and drop toolbox" ! (C#)
- tell me how to solve "counting Problem" in a BTree ? (C++)
Other Threads in the Java Forum
- Previous Thread: custom control
- Next Thread: Problem with methods and writing to file



Threaded Mode