Results 1 to 15 of 15
  1. #1
    gamecock is offline Member
    Join Date
    Sep 2013
    Posts
    7
    Rep Power
    0

    Default Write a program that determines the number of blank spaces

    This is making no sense to me. How am I supposed to determine the number of blank spaces?

    Write a program that determines the number of blank spaces in an input line. Read in the line into a string. Next use the charAt( ) method in a loop to access the characters one by one.
    Your program should have comments towards the top of the program. Copy at least two sample runs and paste as a comment at the bottom of the program. At least one sample run would be a string that has a few blanks. At least one sample run would be a string that does not have any blanks.

  2. #2
    Aries. is offline Member
    Join Date
    Jan 2013
    Posts
    22
    Rep Power
    0

    Default Re: Write a program that determines the number of blank spaces

    try posting what have you done on your code, then someone will help you to figured out whats your problem

    thanks

  3. #3
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,776
    Rep Power
    5

    Default Re: Write a program that determines the number of blank spaces

    Could you do the assignment if it asked to determine the number of uppercase A's? This is no different. A space is also a character.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  4. #4
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: Write a program that determines the number of blank spaces

    Do you know how to write a for loop?
    Get in the habit of using standard Java naming conventions!

  5. #5
    gamecock is offline Member
    Join Date
    Sep 2013
    Posts
    7
    Rep Power
    0

    Default Re: Write a program that determines the number of blank spaces

    Here is what I have so far... Everytime I run it, it comes out blank, no changes. It needs to vary. What am I doing wrong?

    Java Code:
    public class Assign5_Neal {
     
        public static void main(String[] args){
             
       
     }
     
       
         
        public static void space(String space){
            {
     
                int len= space.length();
                int count=0;
                char ch;
                for(int i=0;i<len;i++)
                {
                    ch= space.charAt(i);
                    if (ch==' ')
                        count++;
                }
                System.out.println("number of spaces = "+count);
            }
        }
    }
    Last edited by gamecock; 10-11-2013 at 01:57 AM.

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

    Default Re: Write a program that determines the number of blank spaces

    Java Code:
    public static void main(String[] args){
             
       
    }
    Well there's ya problem!

  7. #7
    gamecock is offline Member
    Join Date
    Sep 2013
    Posts
    7
    Rep Power
    0

    Default Re: Write a program that determines the number of blank spaces

    Quote Originally Posted by Junky View Post
    Java Code:
    public static void main(String[] args){
             
       
    }
    Well there's ya problem!
    So do I just erase that part? I'm pretty lost with this. Can anyone give me an example of what will work?

  8. #8
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default Re: Write a program that determines the number of blank spaces

    Quote Originally Posted by gamecock View Post
    So do I just erase that part? I'm pretty lost with this. Can anyone give me an example of what will work?
    In other words, the main method, which is what is "executed" when you use that class as the "main" class, is empty, so OF COURSE, it is not producing anything.

  9. #9
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,776
    Rep Power
    5

    Default Re: Write a program that determines the number of blank spaces

    Where does you input string come from? And once you get it, where do you pass it to the invocation of the space method?

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  10. #10
    gamecock is offline Member
    Join Date
    Sep 2013
    Posts
    7
    Rep Power
    0

    Default Re: Write a program that determines the number of blank spaces

    Quote Originally Posted by masijade View Post
    In other words, the main method, which is what is "executed" when you use that class as the "main" class, is empty, so OF COURSE, it is not producing anything.
    I have no idea what i'm supposed to put there for it to work! That's the issue!

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

    Default Re: Write a program that determines the number of blank spaces

    Then you need to read an IO (input/output) tutorial, chapter in your book or lecture notes.

  12. #12
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: Write a program that determines the number of blank spaces

    OP: Why did you edit your comment? That's not the code I got in my email.

    Re. your original comment: why would the output ever change if the input is hard-coded?
    Get in the habit of using standard Java naming conventions!

  13. #13
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default Re: Write a program that determines the number of blank spaces

    Quote Originally Posted by gamecock View Post
    I have no idea what i'm supposed to put there for it to work! That's the issue!
    So, In other words, "I have no desire to even try to do anything myself, I'll see if someone is stupid enough to do my homework for me, and I'll keep whining and demanding until they do". Well, good luck with that.

  14. #14
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: Write a program that determines the number of blank spaces

    You weren't far off the mark with your original comment. Change it back and I'll try to explain how to get the input from the command line instead of hard-coding it.
    Get in the habit of using standard Java naming conventions!

  15. #15
    gamecock is offline Member
    Join Date
    Sep 2013
    Posts
    7
    Rep Power
    0

    Default Re: Write a program that determines the number of blank spaces

    Quote Originally Posted by masijade View Post
    So, In other words, "I have no desire to even try to do anything myself, I'll see if someone is stupid enough to do my homework for me, and I'll keep whining and demanding until they do". Well, good luck with that.
    No one whining here but you my friend. Thankfully I found a java forum where people don't bitch and complain like yourself!

    Quote Originally Posted by kjkrum View Post
    You weren't far off the mark with your original comment. Change it back and I'll try to explain how to get the input from the command line instead of hard-coding it.
    I really appreciate the help my man, I think I got it figured out though.

Similar Threads

  1. Replies: 9
    Last Post: 09-22-2013, 09:01 PM
  2. Replies: 8
    Last Post: 01-21-2012, 01:14 AM
  3. Replies: 7
    Last Post: 11-30-2011, 06:58 PM
  4. Replies: 2
    Last Post: 12-10-2009, 12:06 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
  •