GIDForums  

Go Back   GIDForums > Computer Programming Forums > Java Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 15-May-2008, 00:33
javachallenged javachallenged is offline
New Member
 
Join Date: May 2008
Posts: 6
javachallenged is on a distinguished road

Not sure where to go from here...


I'm trying to write a program that reads lines from a txt file, then prints them out with the number of the line it is in front and a colon afterwards as follows:
1 textFromLine1 :
2 textFromLine2 :
etc...

The program I wrote, however, calls the text lines up, but puts a one in front of each line, rather than numbering them. I thought I put the number part in right, but I'm confused. Any help?


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

public class numberTextLines
{
   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
Last edited by LuciWiz : 16-May-2008 at 05:03. Reason: Please insert your Java code between [java] & [/java] tags
  #2  
Old 15-May-2008, 04:34
TurboPT's Avatar
TurboPT TurboPT is offline
Senior Member
 
Join Date: Feb 2006
Location: Atlanta, GA
Posts: 1,140
TurboPT is a jewel in the roughTurboPT is a jewel in the roughTurboPT is a jewel in the rough

Re: Not sure where to go from here...


'number' is always set to one by this:
JAVA Code:
for (number = 1; number <=1; number++)
 // every iteration, number is set to 1.
 // so with every line, number WILL BE 1.
So, I would suggest eliminating the 'for loop', use 'number' as a counter initialized with one where it's declared, then where the loop was, just use two statements:
JAVA Code:
//Display the lines with number and ":".
System.out.println(number + line + ":");
number++; // next line#
__________________
Use the force...read the source!!
WYCIWYG -- what you code is what you get!
Last edited by TurboPT : 15-May-2008 at 05:59.
 
 

Recent GIDBlogProgramming ebook direct download available by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 17:06.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.