•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 423,566 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,641 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: 185 | 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
.,'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
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
what you are today!
it's because on what you did yesterday'
what you did today!
is what in future you will be...'
![]() |
•
•
•
•
•
•
•
•
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


Linear Mode