Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 21 of 21

Thread: Nested Loops & Processing Strings

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    22
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Nested Loops & Processing Strings

    I am reading a file from the user and now I have to count the number of characters, blank spaces, digits, "other characters", words, and lines in a text file using a loop.

    I am having trouble considering what methods of class Character to use and how to increment a counter for each type of character inside a loop.

    Here is what I have thus far: *I only have the basic beginning*

    import java.util.*;
    import java.io.*;
     
     
    public class Reader
    {
      public static void main (String args[]) throws IOException {
     
      Scanner console = new Scanner(System.in);
     
     
      String filename;
      String last;
     
     
    //Declared FileReader and Scanner for the user
     
     
      System.out.println("Please enter the name of the file to be read. ");
      filename = console.nextLine();
     
      FileReader  inputFile = new FileReader(filename);
      Scanner input = new Scanner(inputFile);
     
     
    // Loops to count number of characters, etc.
     
     
        while (input.hasNextLine()) {
     
    last = input.nextLine();
             System.out.println(last);
      }
     }
    }


    I have created another program before hand to decide the characters, but I don't know if I could increment this into my main.

     if (asciiValue >= 49 && asciiValue <= 57)
        System.out.println("Input is a number");
          else if (asciiValue >= 65 && asciiValue <= 90)
        System.out.println("Input is an uppercase letter");
          else if (asciiValue >= 97 && asciiValue <= 122)
        System.out.println("Input is a lowercase letter");
         else if (asciiValue == 32)
        System.out.println("Input is a space");
         else
        System.out.println("Input is an other character");
    Last edited by Usoda; October 18th, 2011 at 10:12 AM.


  2. #2
    Member
    Join Date
    Oct 2011
    Posts
    36
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Nested Loops & Processing Strings

    I think you need to read each character from the last string at first, then implement your second part code. For example:
    for (int j =0; j < last.length(); j++){
    int asciiValue = (int)last.charAt(j);
    if (asciiValue >= 49 && asciiValue <= 57)
        System.out.println("Input is a number");
          else if (asciiValue >= 65 && asciiValue <= 90)
        System.out.println("Input is an uppercase letter");
          else if (asciiValue >= 97 && asciiValue <= 122)
        System.out.println("Input is a lowercase letter");
         else if (asciiValue == 32)
        System.out.println("Input is a space");
         else
        System.out.println("Input is an other character");
    }

  3. #3
    Junior Member
    Join Date
    Sep 2011
    Posts
    22
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Nested Loops & Processing Strings

    I am still unsure how to increment a counter to tell me how many characters, digits, and blank spaces. Also, is there a way to do this with methods from class Character?

  4. #4
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Re: Nested Loops & Processing Strings

    You need to store some variables for each of the digits
    long numChars = 0, numDigits = 0, numSpaces = 0;

    Then you need to declare a BufferedReader, because they are just so much easier. (Or Scanner, whatever you prefer)
    /*
     * Precondition: fr is a FileReader
     */
    BufferedReader br = new BufferedReader(fr);

    then you need to either use a for loop or a while loop to cycle through your code. (A while loop seems more natural here)
    String currentLine = br.readLine();
    while(currentLine != null)
    {
     
    }
    Then, inside of your while loop, you need to figure out if each character is a digit, space, or something else (character)
    Luckily, Character does have some methods to help you out, conveniently explained here
    But wait! We can't use the Character class on Strings, so we need some way to convert. Searching through String's API, we find charAt(int index) and toCharArray(). Deciding to use toCharArray for easier use, we use that method to convert, and use a for loop to cycle through the array.
    char[] charArray = currentLine.toCharArray();
    Character currentChar;
    for(int i = 0; i < charArray.length; i++)
    {
      currentChar = charArray[i];
    }
    But now we need to decide what the char is, and respectively increment the correct long.
    Going back to Characters API, we find the isDigit() and the isWhitespace() methods, and if not either of those, it is some kind of character. Using those, we get
      if(Character.isDigit(currentChar))
        numDigits++;
      else if(Character.isWhitespace(currentChar))
        numSpaces++;
      else
        numChars++;
    Then you need to update our line with (outside our for loop)
    currentLine = br.nextLine();
    And that is all you need to get those, but you still ahve an open reader, so we end this part of the program (Unless you need the reader elsewhere) with
    br.close();
    (Note that is outside the while loop)
    Last edited by Tjstretch; October 19th, 2011 at 06:54 PM. Reason: Typo

  5. The Following User Says Thank You to Tjstretch For This Useful Post:

    Usoda (October 21st, 2011)

  6. #5
    Junior Member
    Join Date
    Sep 2011
    Posts
    22
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Nested Loops & Processing Strings

    Tjstretch I have not read your post yet. I just now created this which counts everything, but it doesn't combine the values.
    while (input.hasNextLine()) {
     
          last = input.nextLine();
     
    char[] c=last.toCharArray();
     
    for(int i=0;i<c.length;i++)
    {
    //System.out.println(c[i]+" "+i);
    boolean flag=true;
    for(int k=0;k<i;k++){
    if(c[i]==(last.charAt(k)))
    flag=false;
    }
    if(flag){
    for(int j=0;j<last.length();j++)
    {
    if(c[i]==last.charAt(j))
    count=count+1;
    }
    System.out.println(c[i]+" "+" "+(count));
    count=0;
    loopcount++;

    Will reply when I am done reading your post, thankyou.

  7. #6
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Re: Nested Loops & Processing Strings

    Use
    [highlight=Java] AND [/highlight]
    Rather then code tags for java

  8. #7
    Junior Member
    Join Date
    Sep 2011
    Posts
    22
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Nested Loops & Processing Strings

    Tjstretch with your method I get an error "Cannot invoke isDigit on the primitive type char?" on both isDigit and isWhitespace

  9. #8
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Re: Nested Loops & Processing Strings

    Oh woops my mistake, it should be
    Character currentChar;
    not char, I'll fix that

    Fixed

  10. #9
    Junior Member
    Join Date
    Sep 2011
    Posts
    22
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Nested Loops & Processing Strings

    Sorry to be so bothersome, but now it is saying:

    The method isWhitespace(char) in the type java.lang.Character is not applicable for the arguments ()

    I don't understand why it is trying to throw an argument when it's a simple read

  11. #10
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Re: Nested Loops & Processing Strings

    Oh I must of read the API wrong, I will fix the post with it in one second. I hadn't realized it was static. Fixed, man I'm tired today

  12. #11
    Junior Member
    Join Date
    Sep 2011
    Posts
    22
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Nested Loops & Processing Strings

    Hey man I feel yeah. I got another error:

    The local variable currentChar may not have been initialized

    I am going to take a break because I am becoming frustrated, and can't stand to stare at this program. Maybe it will come to me when I return, if so I will post with results

  13. #12
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,469
    My Mood
    Grumpy
    Thanks
    0
    Thanked 158 Times in 149 Posts

    Default Re: Nested Loops & Processing Strings

    That error message is self explanatory, you did not give your variable an initial value.
    String s;
    if(5 == 19) {
        s = "hello";
    }
    System.out.println(s);
    In the above code what do you expect to be displayed?
    Improving the world one idiot at a time!

  14. #13
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Re: Nested Loops & Processing Strings

    Sigh, the reason I hadn't fixed the small bugs is because I didn't actually use a compiler, I'm just giving you how you should go about doing it, not copy my code exactly.

    change
    Character currentChar;
    to
    Character currentChar = 'c';

  15. #14
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,469
    My Mood
    Grumpy
    Thanks
    0
    Thanked 158 Times in 149 Posts

    Default Re: Nested Loops & Processing Strings

    char currentChar = ...;

    No need to use objects when it isn't necessary.
    Improving the world one idiot at a time!

  16. #15
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Re: Nested Loops & Processing Strings

    You can do that?

  17. #16
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,469
    My Mood
    Grumpy
    Thanks
    0
    Thanked 158 Times in 149 Posts

    Default Re: Nested Loops & Processing Strings

    I can't do what?
    Improving the world one idiot at a time!

  18. #17
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,469
    My Mood
    Grumpy
    Thanks
    0
    Thanked 158 Times in 149 Posts

    Default Re: Nested Loops & Processing Strings

    Oops! Misread your post.

    char one = '1';
    char two = 't';
    System.out.println(Character.isDigit(one));
    System.out.println(Character.isDigit(two));
    Produces the exact output I expect: true false.
    Improving the world one idiot at a time!

  19. #18
    Junior Member
    Join Date
    Sep 2011
    Posts
    22
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Nested Loops & Processing Strings

    I didn't know you had to assign it. I thought char currentChar; would be all you had to do to initialize it. I guess I was wrong though.

  20. #19
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Re: Nested Loops & Processing Strings

    I meant the
    char one = ...;

    Oh I misread your post too, you meant about not be object not about how I assigned it, thank you.

    So yeah it should be
    char currentChar = 'c';

  21. #20
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,469
    My Mood
    Grumpy
    Thanks
    0
    Thanked 158 Times in 149 Posts

    Default Re: Nested Loops & Processing Strings

    Declaring a variable and initialising it are two different things.

    Declare
    String s;

    Initialise
    s = "hello";

    Declare and initialise
    String s = "hello";
    Improving the world one idiot at a time!

  22. #21
    Junior Member
    Join Date
    Sep 2011
    Posts
    22
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Nested Loops & Processing Strings

    Alright I finished my program however, I need a counter for words. The program below counts words that end with a space.

    I need to do an || statement or a different loop to count words that also end with period, comma, semicolon, colon or a blank space.

           for(int a = 0; a < last.length(); a++) 
          {
        if (last.charAt(a) == ' ') 
          numberOfWords += 1;

Similar Threads

  1. Nested for loops
    By Fordy252 in forum Loops & Control Statements
    Replies: 2
    Last Post: December 8th, 2012, 10:43 PM
  2. Using Nested Loops
    By m2msucks in forum Loops & Control Statements
    Replies: 7
    Last Post: November 5th, 2011, 07:05 PM
  3. Please help! Nested while loops and asterisk triangles!
    By rockout341 in forum Loops & Control Statements
    Replies: 12
    Last Post: September 15th, 2011, 11:50 AM
  4. Help with Nested Loops
    By Plural in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 23rd, 2010, 03:31 PM
  5. how do i draw a shape with nested loops?
    By Kilowog in forum Loops & Control Statements
    Replies: 1
    Last Post: September 25th, 2009, 12:14 AM

Tags for this Thread