Results 1 to 8 of 8
  1. #1
    manibby93 is offline Member
    Join Date
    Sep 2013
    Posts
    77
    Rep Power
    0

    Default URGENT: method that generates number of lines, word, characters & longest word

    This is due by 11AM tomorrow. I had to do like 10 of these throughout this wk, so please don't say I procrastinated...

    Write a static method named wordCountAndFixSpace that takes a Scanner representing a file as a parameter and that reports various statistics about the file. The method should generate the number of lines, the total number of words (regard "can't" as 1-word and disregard any marks or signs such as ?, ", ., -, \n, \t, etc. when counting), the total number of characters, and the longest word. The text may contain multiple spaces and tabs. Those spaces should be reduced to single spaces between each word in order to print them to the console. Your program can assume that the text file may contain at least one line of input.

    I JUST WANT IT TO COMPILE. I get the errors that the local variables "word" & "line" are never read. I understand that, but they're clearly there.

    Java Code:
    import java.util.*;
     import java.io.*;
     public class TEST2 {
    //throws the exception
    public static void main(String[] args) throws FileNotFoundException {
        //calls on each counter method and prints each one
        System.out.println("Number of Words: " + wordCounter());
        System.out.println("Number of Lines: " + lineCounter());
        System.out.println("Number of Characters: " + charCounter());
        System.out.println("Longest Word: " + lengthCounter());
    
    }
    
    //static method that counts words in the text file  
    public static int wordCounter() throws FileNotFoundException 
    { 
        Scanner input = new Scanner(new File("words.txt")); 
        int countWords = 0;
    
        while (input.hasNextLine()) {
    
            if(input.hasNext()) {
                String word = input.next();
                countWords++;
            }
        }
    
        return countWords;
    }
    
    //static method that counts lines in the text file  
    public static int lineCounter() throws FileNotFoundException {
    //inputs the text file
    Scanner input2 = new Scanner(new File("writing.txt"));
        int countLines = 0;
        //while there are more lines
        while (input2.hasNextLine()) {
            //casts each line as a string
            String line = input2.nextLine();
            //counts each line
            countLines++;
        }
        return countLines;
       }    
    
    //static method that counts characters in the text file 
    public static int charCounter() throws FileNotFoundException {
    //inputs the text file
    Scanner input3 = new Scanner(new File("words.txt"));
        int countChar = 0;
        int character = 0;
        //while there are more lines
        while(input3.hasNextLine()) {
           //casts each line as a string
            String line = input3.nextLine();
            //goes through each character of the line
            for(int i=0; i < line.length(); i++){
                character = line.charAt(i);
                 //if character is not a space (gets rid of whitespace)
                if (character != 32){
                    //counts each character
                    countChar++;
                }
            }           
        }
       return countChar;
    }
    
    
    //static method that counts characters in the text file 
    public static int lengthCounter() throws FileNotFoundException {
    //inputs the text file
    Scanner input4 = new Scanner(new File("words.txt"));
    
    int largest = 0;
    
    while (input4.hasNextLine ())
    {
    String word = input4.nextLine ();
    if(largest < word.length ())
    
    largest = word.length ();
    }
    return largest;
    }
     }

  2. #2
    manibby93 is offline Member
    Join Date
    Sep 2013
    Posts
    77
    Rep Power
    0

    Default Re: URGENT: method that generates number of lines, word, characters & longest word

    This is an updated version, but I don't think it's any better... I just added random things I thought looked right, lol, such as return(0);

    Java Code:
    import java.util.*;
     import java.io.*;
     public class TEST2 {
    //throws the exception
    public static void main(String[] args) throws FileNotFoundException {
        //calls on each counter method and prints each one
        System.out.println("Number of Words: " + wordCounter());
        System.out.println("Number of Lines: " + lineCounter());
        System.out.println("Number of Characters: " + charCounter());
        System.out.println("Longest Word: " + lengthCounter());
    
    }
    
    //static method that counts words in the text file  
    public static int wordCounter() throws FileNotFoundException 
    { 
        Scanner input = new Scanner(new File("words.txt")); 
        int countWords = 0;
    
        while (input.hasNextLine()) {
    
            if(input.hasNext()) {
    String word = null;
       while ((word = input.nextLine()) != null) {
                countWords++;
            }
        }
        return countWords;
    }
        return(0);
    }
    
    //static method that counts lines in the text file  
    public static int lineCounter() throws FileNotFoundException {
    //inputs the text file
    Scanner input2 = new Scanner(new File("writing.txt"));
        int countLines = 0;
        //while there are more lines
        while (input2.hasNextLine()) {
            //casts each line as a string
    
    String line = null;
       while ((line = input2.nextLine()) != null) {
            //counts each line
            countLines++;
        }
        return countLines;
       }    
        return(0);
    }
    //static method that counts characters in the text file 
    public static int charCounter() throws FileNotFoundException {
    //inputs the text file
    Scanner input3 = new Scanner(new File("words.txt"));
        int countChar = 0;
        int character = 0;
        //while there are more lines
        while(input3.hasNextLine()) {
           //casts each line as a string
            String line = input3.nextLine();
            //goes through each character of the line
            for(int i=0; i < line.length(); i++){
                character = line.charAt(i);
                 //if character is not a space (gets rid of whitespace)
                if (character != 32){
                    //counts each character
                    countChar++;
                }
            }           
        }
       return countChar;
    }
    
    
    //static method that counts characters in the text file 
    public static int lengthCounter() throws FileNotFoundException {
    //inputs the text file
    Scanner input4 = new Scanner(new File("words.txt"));
    
    int largest = 0;
    
    while (input4.hasNextLine ())
    {
      
    String word = null;
       while ((word = input4.nextLine()) != null) {
         
    if(largest < word.length ())
    
    largest = word.length ();
    }
    return largest;
    }
    return(0);
     }
     }

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,782
    Rep Power
    7

    Default Re: URGENT: method that generates number of lines, word, characters & longest word

    Quote Originally Posted by manibby93 View Post
    Write a static method named wordCountAndFixSpace that takes a Scanner representing a file as a parameter and that reports various statistics about the file.
    Nowhere in your code do I see such a method.

  4. #4
    manibby93 is offline Member
    Join Date
    Sep 2013
    Posts
    77
    Rep Power
    0

    Default Re: URGENT: method that generates number of lines, word, characters & longest word

    No offense, but there are indentations and comments. Let me POINT IT OUT FOR YOU:

    THIS is the problem. Somewhere in there, idk exactly where. It probably has to do w/ it not reaching the println statements at the top or however you say that. I just want help w/ THIS, or w/e is causing THIS to not function properly:

    Java Code:
    //Longest Word
    public static int words(Scanner input) {
    String s = "";
    File file = new File("writing.txt");
    try 
    {
      FileReader reader = new FileReader(file);
    StringTokenizer st = new StringTokenizer(s);
    int lengthCount = 0;
    String answer = "";
    while(st.hasMoreTokens()){
    String temp = st.nextToken();
    if(lengthCount < temp.length()){
    answer = temp;
    lengthCount = answer.length();
    }
    }
    reader.close();
    } 
    catch (Exception e)
    {
    e.printStackTrace();
    }
    return lengthCount;
    }
    }
    THIS IS THE FULL CODE THAT I ADJUSTED SINCE LAST TIME:
    Java Code:
    import java.io.*; 
    import java.util.*; 
    
    public class wordCountAndFixSpace
    { 
    private static int wordCount; 
    private static int lineCount; 
    private static int lengthCount; 
    
    public static void main (String[] args) throws FileNotFoundException 
    { 
    Scanner inputLine = new Scanner(new File("writing.txt")); 
    File f = new File("writing.txt"); 
    
    //words(inputLine); 
    lines(inputLine); 
    
    System.out.println("Number of Words: " + wordCount); 
    System.out.println("Number of Lines: " + lineCount); 
    System.out.println("Number of Characters: " + f.length()); 
    System.out.println("The longest word is: " + lengthCount); 
    } 
    
    //[B]Number of Words[/B]
    
    public static int words(String input) 
    { 
    String[] wrds = input.split(" "); 
    wordCount += wrds.length + 1; 
    
    return wordCount; 
    } 
    
    //[B]Number of Lines[/B]
    
    public static int lines(Scanner input) 
    { 
    while (input.hasNextLine()) 
    { 
    String line = input.nextLine(); 
    words(line); 
    lineCount++; 
    } 
    
    return lineCount; 
    }
    
    //[B]Longest Word[/B]
    
    public static int words(Scanner input) {
    String s = "";
    File file = new File("writing.txt");
    try 
    {
      FileReader reader = new FileReader(file);
    StringTokenizer st = new StringTokenizer(s);
    int lengthCount = 0;
    String answer = "";
    while(st.hasMoreTokens()){
    String temp = st.nextToken();
    if(lengthCount < temp.length()){
    answer = temp;
    lengthCount = answer.length();
    }
    }
    reader.close();
    } 
    catch (Exception e)
    {
    e.printStackTrace();
    }
    return lengthCount;
    }
    }
    I would like for the section where I'm trying to find the longest word in the file to work. I just want it to show SOMETHING besides 0 as the output. Okay? I would like for it to actually read the file and state what the longest word is. Everything else works fine. I've seen some funky codes, but mine is not illegible. It's sectioned off. I tried. I repeat, I'm a novice.

    I'm stressed out w/ my 6 classes, compared to one job, honestly, and would like to just make this code fully operate. I've seen people get answers on here before, but I'm tired of getting a hard time. I didn't ASK for the answer, but I DID try to put SOMETHING together at least whether I copied & pasted it & ALTERED it or not because I wasn't born knowing Java. Sorry. I understand some of it, but not all, so all that array business & loop talk will confuse me w/o example, but I'll listen...

    Now, if ANYBODY can just simply give me a DIRECT answer on how I can make this program work, that'd be WONDERFUL as it is due at 11:00AM. I completed the other like 5, but I just need this ONE. SHEESH. Can I get a break or no? I already don't understand the class. I'm NOT a COSC major, so what do you want... Please don't answer that, just help me with this PLEASE & don't give me a speech that would cause me to not be able to turn this in BEFORE 11:00AM.
    Last edited by manibby93; 10-10-2013 at 07:55 AM.

  5. #5
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,782
    Rep Power
    7

    Default Re: URGENT: method that generates number of lines, word, characters & longest word

    Nope! I still cannot see a method called wordCountAndFixSpace in your code. You have changed your original code to call the class this but that is not what your instructions say.

  6. #6
    manibby93 is offline Member
    Join Date
    Sep 2013
    Posts
    77
    Rep Power
    0

    Default Re: URGENT: method that generates number of lines, word, characters & longest word

    It's a small error. I got help elsewhere though, thanks... THEY understood, somehow, smh, because they must be supernatural or something...

  7. #7
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,945
    Rep Power
    19

    Default Re: URGENT: method that generates number of lines, word, characters & longest word

    But, as Junky says, you haven't actually done what the requirements say.
    "
    Write a static method named wordCountAndFixSpace that takes a Scanner representing a file as a parameter and that reports various statistics about the file.
    "

    It's a pretty fundamental flaw.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  8. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,782
    Rep Power
    7

    Default Re: URGENT: method that generates number of lines, word, characters & longest word

    Yeah, I wasn't having any problems understanding your problem or being able to help you. I just thought it would be pointless until you fixed a very basic mistake. When building a house you cannot make the roof first.

Similar Threads

  1. Replies: 5
    Last Post: 10-11-2013, 12:31 AM
  2. Replies: 3
    Last Post: 08-30-2011, 04:44 PM
  3. longest word present in a string
    By somnath6088 in forum New To Java
    Replies: 4
    Last Post: 11-09-2010, 05:43 PM
  4. Longest word in a program...
    By hustlas4ever in forum New To Java
    Replies: 5
    Last Post: 08-20-2010, 01:34 PM
  5. java longest word in array
    By mayhewj7 in forum New To Java
    Replies: 10
    Last Post: 04-24-2009, 01:39 AM

Posting Permissions

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