Results 1 to 2 of 2

Thread: txt file output

  1. #1
    Yoana is offline Member
    Join Date
    Oct 2013
    Posts
    10
    Rep Power
    0

    Default txt file output

    I have to write a program that reads the input from a text file!

    Beware the Jabberwock, my son,
    the jaws that bite, the claws that catch,
    Beware the JubJub bird and shun
    the frumious bandersnatch.

    I should print the number of lines, the longest line, the number of tokens on each line and the length of the longest token on each line.
    I was able to find the number of tokes in the line but im having having problems reading the longest word ,, my program gives me the number of letter of each line instead of only the number of letter of the longest word!!
    can someone please help !

    this should be my output:

    Line 1 has 5 tokens (longest = 11)
    Line 2 has 8 tokens (longest = 6)
    Line 3 has 6 tokens (longest = 6)
    Line 4 has 3 tokens (longest = 13)
    Longest line : the jaws that bite, the claws that catch,

    import java.io.*;
    import java.util.*;

    public class InputStats{
    public static void main (String [] args )
    throws FileNotFoundException{

    Scanner console = new Scanner ( System.in);
    System.out.println(" ");
    System.out.println();
    System.out.println("Please enter a name of a file " );
    String name = console.nextLine();
    Scanner input = new Scanner ( new File (name));
    while (input.hasNextLine()) {
    String line = input.nextLine();
    inputStats(new Scanner(line));
    }



    }//end of amin
    public static void inputStats (Scanner input)
    throws FileNotFoundException{
    int numLines=0;
    int numwords=0;
    String maxLine = "";


    while (input.hasNextLine()){
    String next = input.nextLine();
    numwords += next.split("\\s+").length;
    numLines++;

    if (next.length() > maxLine.length()) {
    maxLine = next;
    }
    }//end of while


    System.out.print("Line " + numLines + " has ");
    System.out.print(numwords + "tokens " );
    System.out.print("(longest = " + maxLine.length() + ")");
    System.out.println();




    }//end of method

    }// end of class
    Last edited by Yoana; 03-16-2014 at 04:13 AM.

  2. #2
    jashburn is offline Senior Member
    Join Date
    Feb 2014
    Posts
    219
    Rep Power
    1

    Default Re: txt file output

    Do wrap you code using [code] (not [quote]) tags. E.g.,

    [code]
    // your code here
    [/code]

    This preserves formatting and provides syntax highlighting, e.g.,
    Java Code:
        // your code here
    I'd suggest you approach this step by step rather than to try to implement everything at one go. You can first focus on getting the right output for the line number:

    Java Code:
    Line 1 ...
    Line 2 ...
    (and so on)
    Your current implementation has a while (input.hasNextLine()) loop in the main method, and another while (input.hasNextLine()) in the inputStats method. The latter is unnecessary because the former is already parsing the text file line by line. Also, numLines is declared in the wrong place such that at every print out you get "Line 1 ..." without incrementing the line number despite having numLines++. At this point you're better off starting a new Java source file, concentrating on just printing the line numbers as mentioned above using just 1 while (input.hasNextLine()) loop.

    After that, you can use the split method in the while loop to split out the words in each line. Instead of what you've currently done with numwords += next.split("\\s+").length; you should assign it to a String array, e.g.,

    Java Code:
    String[] words = next.split("\\s");
    This is because you'll need to operate on each of the words in the array to find the word's length.

    Try this and see how far you can get with this. If you run into problems post your re-worked code here, and include the print out of what you're getting. Note that it's always better to approach a programming task step by step, getting the current step to work properly first before proceeding to the next step. This makes it easier to see the larger picture and to diagnose problems, rather than to implement everything at one go, only to later find out there are problems in multiple places.

Similar Threads

  1. Replies: 1
    Last Post: 08-05-2011, 09:21 PM
  2. Replies: 0
    Last Post: 09-30-2010, 08:16 PM
  3. Replies: 8
    Last Post: 05-12-2010, 07:19 PM
  4. No output to file
    By shiva in forum Java Servlet
    Replies: 0
    Last Post: 04-24-2009, 02:06 PM
  5. Replies: 7
    Last Post: 11-19-2008, 02:09 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •