12 Replies - 3187 Views - Last Post: 27 August 2010 - 01:20 PM Rate Topic: -----

#1 saleem015   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 08-August 10

read in text file and output number of lines in file

Posted 26 August 2010 - 04:30 AM

hi all,
I need to Write a program to read in a text file and output the number of lines in the file.
Your program must use a single command-line parameter to provide the name of the file to process. Command-line parameters are stored in the args parameter of the main method. The following is an example of how your program can be called from the command-line:

java Question1 inputfile.txt
Sample Input File

hello
world
123

Sample Output

Number of lines: 3

any help would be welcomed and appreciated. - Saleem from cape town

Is This A Good Question/Topic? 0
  • +

Replies To: read in text file and output number of lines in file

#2 aniri   User is offline

  • D.I.C Addict
  • member icon

Reputation: 54
  • View blog
  • Posts: 657
  • Joined: 24-November 09

Re: read in text file and output number of lines in file

Posted 26 August 2010 - 05:24 AM

we are not here to do your homework! we are here to help in case YOUR code does not work properly...

have you tried solving this yourself? what have you come up with? show us and we'll help then ;)
Was This Post Helpful? 1
  • +
  • -

#3 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3786
  • View blog
  • Posts: 13,716
  • Joined: 08-August 08

Re: read in text file and output number of lines in file

Posted 26 August 2010 - 05:33 AM

...and javascript != java
Was This Post Helpful? 0
  • +
  • -

#4 saleem015   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 08-August 10

Re: read in text file and output number of lines in file

Posted 26 August 2010 - 06:14 AM

ok so this is what i've got so far, it compiles and runs but theres some kind of error. because i dont think my program has a single command-line parameter to provide the name of the file to process. Command-line parameters are stored in the args parameter of the main method. The following is an example of how your program can be called from the command-line:
but im not too sure what to do about that, also im not sure wether to use a buffer reader or a scanner?

java Question1 inputfile.txt
import java.io.*; 
import java.util.Scanner; 
  public class Question1
  {

  public static void main(String[] args)
   {
  
    try{
   
 FileInputStream textFile = new FileInputStream ("java Question1 inputfile.txt");
 Scanner inFile = new Scanner (System.in);  
	  
	        String str= inFile.readLine();
  
      File file = new File(str);

      if (file.exists()){

        FileReader fr = new FileReader(file);
   
     LineNumberReader ln = new LineNumberReader(fr);
  
      int count = 0;   
  
        while (ln.readLine() != null){
  
          count++;
     
   }
 
       System.out.println("Number of lines: " + count);
  
        ln.close();
  
      }
  
      else{
 
       System.out.println("File does not exists!"); 
 
     }
  
    }

    catch(IOException e)
	 {

      e.printStackTrace();

    }

  }

}

This post has been edited by JackOfAllTrades: 26 August 2010 - 09:47 AM
Reason for edit:: Added code tags.

Was This Post Helpful? 0
  • +
  • -

#5 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6246
  • View blog
  • Posts: 24,014
  • Joined: 23-August 08

Re: read in text file and output number of lines in file

Posted 26 August 2010 - 09:48 AM

As stated up above, Javascript and Java are two entirely different, unrelated things.

Moved to Java.
Was This Post Helpful? 0
  • +
  • -

#6 Dogstopper   User is offline

  • The Ninjaducky
  • member icon

Reputation: 2965
  • View blog
  • Posts: 11,222
  • Joined: 15-July 08

Re: read in text file and output number of lines in file

Posted 26 August 2010 - 03:30 PM

Are you expecting it to open this file?
FileInputStream ("java Question1 inputfile.txt");

I'm not sure that Files can have spaces like that. What does your error say?
Was This Post Helpful? 0
  • +
  • -

#7 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: read in text file and output number of lines in file

Posted 26 August 2010 - 04:39 PM

View PostDogstopper, on 26 August 2010 - 04:30 PM, said:

Are you expecting it to open this file?
FileInputStream ("java Question1 inputfile.txt");

I'm not sure that Files can have spaces like that. What does your error say?

As Dogstopper mentionned it what do you need that one as later on you prompt the user for a filename an open that file with a FileReader ?
Was This Post Helpful? 0
  • +
  • -

#8 Luckless   User is offline

  • </luck>
  • member icon

Reputation: 293
  • View blog
  • Posts: 1,146
  • Joined: 31-August 09

Re: read in text file and output number of lines in file

Posted 26 August 2010 - 06:16 PM

also, why not just go with a BufferedReader and increment a counter as you read in lines? I know you kinda do that, but it seems like that LineNumberReader is unnecessary

This post has been edited by Luckless: 26 August 2010 - 06:17 PM

Was This Post Helpful? 0
  • +
  • -

#9 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: read in text file and output number of lines in file

Posted 26 August 2010 - 06:48 PM

A Scanner on file will also do the job

while(fileScanner.hasNext() {
  fileScanner.nextLine();
  ++count;
}


and will avoid using all the try/catch block
Was This Post Helpful? 1
  • +
  • -

#10 Luckless   User is offline

  • </luck>
  • member icon

Reputation: 293
  • View blog
  • Posts: 1,146
  • Joined: 31-August 09

Re: read in text file and output number of lines in file

Posted 26 August 2010 - 07:04 PM

yeah, either one works, I just don't like implementing unnecessary classes such as that LineNumberReader
Was This Post Helpful? 0
  • +
  • -

#11 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: read in text file and output number of lines in file

Posted 26 August 2010 - 08:10 PM

Agree ... didn't even noticed a used class wasn't posted
Was This Post Helpful? 0
  • +
  • -

#12 Dogstopper   User is offline

  • The Ninjaducky
  • member icon

Reputation: 2965
  • View blog
  • Posts: 11,222
  • Joined: 15-July 08

Re: read in text file and output number of lines in file

Posted 27 August 2010 - 01:10 PM

Quote

... 
FileInputStream textFile = new FileInputStream ("java Question1 inputfile.txt");
...



I think you are using this as a command line argument without meaning to. As the filename, use "inputfile.txt" or use args[0] if you want to use a command line arguments.

I would personally use a File object like so:
public static void main(String[] args) {
    File file;
    if (!args[0].equals(""))
        file = new File(args[0]);
    else
        file = new File("filename.txt");
 
    ... Now use the file object
}


Was This Post Helpful? 0
  • +
  • -

#13 Codebug   User is offline

  • D.I.C Head

Reputation: 31
  • View blog
  • Posts: 244
  • Joined: 11-October 09

Re: read in text file and output number of lines in file

Posted 27 August 2010 - 01:20 PM

View Postpbl, on 26 August 2010 - 05:48 PM, said:

A Scanner on file will also do the job

while(fileScanner.hasNext() {
  fileScanner.nextLine();
  ++count;
}


and will avoid using all the try/catch block


Instead of saying:

while(fileScanner.hasNext())



Could you also have said:

while(fileScanner.hasNextLine())



How did you decide to use hasNext instead of hasNextLine? I could be wrong with my statement but it seems like it should work.

EDIT: I see now why my code won't work. If there is one line only, and hasNextLine() is used for the loop condition, the loop won't run and the line count won't be incremented. My mistake :)

This post has been edited by Codebug: 27 August 2010 - 01:27 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1