Results 1 to 9 of 9

Thread: Character Count

  1. #1
    drucey is offline Member
    Join Date
    Oct 2010
    Posts
    24
    Rep Power
    0

    Default Character Count

    Hi there,

    Im in the middle of doing something, while trawling through the Java API and any related forum threads i haven't found anything useful.

    Basically i need to count the number of characters in a line from a file.
    The file is already saved line by line in an array so hopefully it will just be a method to count next character while using something like scan.hasNext()
    arraycount ++ // to print the next line of the array
    charcount ++ // to work out the number of chars

    This might not make much sense at the mo but all i need is a method to count characters in a line. I can work out the loops and prints.

    Many thanls

    Chris

  2. #2
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    812
    Rep Power
    6

    Default

    String (Java 2 Platform SE v1.4.2)
    int length()
    Returns the length of this string.
    Ramya:cool:

  3. #3
    drucey is offline Member
    Join Date
    Oct 2010
    Posts
    24
    Rep Power
    0

    Default

    Wow thanx, sorry i just seemed to get lost in the API!

    The reason i was hoping for a charcounter is because the next task is to print the first 10 characters so that would be made easy with a loop

    Thanks again, Ramya.

  4. #4
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    Do you want to include or omit whitespace characters. What about dots, commas etc...?
    Ever seen a dog chase its tail? Now that's an infinite loop.

  5. #5
    drucey is offline Member
    Join Date
    Oct 2010
    Posts
    24
    Rep Power
    0

    Default

    It's not actually specified if we count grammar. So i guess i can do it either way. Which ever way is easiest i can do. I can System.out.println("Number of characters:") or
    ("Number of Letters:")

  6. #6
    RichWade is offline Member
    Join Date
    Nov 2010
    Posts
    33
    Rep Power
    0

    Default

    Here is one way to print the first 10 characters of a string:

    String st = "This is a long string";
    System.out.println(st.subSequence(0, 9));

  7. #7
    drucey is offline Member
    Join Date
    Oct 2010
    Posts
    24
    Rep Power
    0

    Default

    Ah of course, thanx very much, Rich!

    We learned to do that in the beginning, certainly makes things easier!

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,386
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by RichWade View Post
    Here is one way to print the first 10 characters of a string:

    String st = "This is a long string";
    System.out.println(st.subSequence(0, 9));
    Ahem, for a substring of length 10, make that:

    Java Code:
    System.out.println(st.subSequence(0, 10);
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    drucey is offline Member
    Join Date
    Oct 2010
    Posts
    24
    Rep Power
    0

    Default

    Well this is the done deal. I have 2 more tasks but they arent essential. One is to allow the user to search for a word then list the text with that word in caps. I wont bother with this.

    The last one, which is annoying me, is to change the length of text to be printed and sotred by it all to <=300. As youy can see the array holds the entire text file (305 lines) but when i go to change the coe to just count < 299 anywhere it seems to throw up this error:

    Exception in the "main" java.lang.ArrayIndexOutOfBoundsException : 299 at assignment.main(assignment.java:35)
    Last edited by drucey; 11-25-2010 at 10:50 PM. Reason: information overload

Similar Threads

  1. count character in text file as input file
    By aNNuur in forum New To Java
    Replies: 7
    Last Post: 03-25-2010, 04:01 PM
  2. how to count the given character?
    By ashin in forum Advanced Java
    Replies: 1
    Last Post: 10-14-2009, 04:28 PM
  3. Replies: 5
    Last Post: 11-22-2008, 01:34 PM
  4. reading text character by character
    By bugger in forum New To Java
    Replies: 2
    Last Post: 11-09-2007, 08:54 PM

Posting Permissions

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