Results 1 to 10 of 10
  1. #1
    clemsontigers is offline Member
    Join Date
    Oct 2010
    Posts
    44
    Rep Power
    0

    Default Help w/ java programming assignment counting number of spaces

    could someone help w/ this assignment that im stuck on

    here are the instructions
    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.

    here is my code

    Java Code:
    import java.util.Scanner;
    
    public class Spaces {
    
    public void findSpaces(String str)
    {
    	
    //Create a scanner
    Scanner in = new Scanner(System.in);
    	
    	 
    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);
    String userInput = in.nextLine();
    }
    }
    Im getting an error under space saying that space cannot be resolved

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

    Default

    Quote Originally Posted by clemsontigers View Post
    Im getting an error under space saying that space cannot be resolved

    Yes, where it is declared? Do you mean str?
    --> public void findSpaces(String str) <--

    But if you get a String, why do you want to read a second string with the scanner?

  3. #3
    clemsontigers is offline Member
    Join Date
    Oct 2010
    Posts
    44
    Rep Power
    0

    Default

    still not right but heres my updated code

    Java Code:
    public class Assign5_Roberts
    {
    public void findSpaces(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);
    
    }
    }

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

    still not right

    What's not right about it?

  5. #5
    clemsontigers is offline Member
    Join Date
    Oct 2010
    Posts
    44
    Rep Power
    0

    Default

    well it shows no errors but whenever i run there program there is no output

  6. #6
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Quote Originally Posted by clemsontigers View Post
    well it shows no errors but whenever i run there program there is no output
    Well, what you've posted isn't a program, because there's no main() method. If there's another piece involved, it's going to be difficult for us to guess what it is.

    -Gary-

  7. #7
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

    And if there is not another piece of the program have a look at your textbooks etc to see how the examples use a method called main().

  8. #8
    clemsontigers is offline Member
    Join Date
    Oct 2010
    Posts
    44
    Rep Power
    0

    Default

    here is my updated code but still no output when i run the program...should there be a string under the println?

    Java Code:
    public class Assign5_Roberts
    {
    	public static void main(String[] args){
    	}
    	
        public 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);
    
    }
    }
    }

  9. #9
    mobosecomin is offline Member
    Join Date
    Oct 2010
    Posts
    23
    Rep Power
    0

    Default

    your main method is empty dude its very important to learn how methods work. here is your code with a call in your main method

    Java Code:
    public class Assign5_Roberts {
    
        public static void main(String[] args){
            
        space(" This is the main method where all the action happens when you are learning to program java");
      
     }
    
        // below is your second method for checking for spaces it is not linked to your main method in anyway 
        // to use this you need to call it in your main method e.g space(String in here)
        // It is important to learn where methods start and finish and how to call them because its all methods in future
        // its also useful to learn what public static void mean. 
        
        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);
            }
        }
    }

  10. #10
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Just and alternative.

    You can store your string in an array and check indexes.

    Java Code:
    String str = "This is my string with 6 spaces.";
    char[] digit = str.toCharArray();
    
    int iSpaces = 0;
    for(int i = 0; i < digit.length; i++) {
    	if(digit[i] == ' ') {
    	   iSpaces++
            }
    }

Similar Threads

  1. help..stuck w/ a java programming assignment
    By clemsontigers in forum New To Java
    Replies: 16
    Last Post: 02-19-2014, 05:16 AM
  2. Replies: 2
    Last Post: 12-10-2009, 01:06 PM
  3. counting number of lines of system.out
    By IYIaster in forum New To Java
    Replies: 1
    Last Post: 07-21-2009, 01:37 AM
  4. Counting the number of columns in a 2D array,
    By KalEl in forum New To Java
    Replies: 9
    Last Post: 10-21-2008, 06:27 AM
  5. java programming challenge Number 1
    By tacosalad in forum New To Java
    Replies: 5
    Last Post: 01-16-2008, 11:25 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
  •