Results 1 to 14 of 14
  1. #1
    MrPosotive is offline Member
    Join Date
    Sep 2012
    Posts
    68
    Rep Power
    0

    Default how do i count spaces in java?

    how would i find out how many digits this is? System.out.println("Whats up dock?"); like the digits in the parenthesizes.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,193
    Rep Power
    19

    Default Re: how do i count spaces in java?

    Moved from Advanced Java. What made you think that the question belonged there?

    You need to clarify your question. As it stands, it doesn't make much sense, and the subject isn't in agreement with the text.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: how do i count spaces in java?

    The parenthesis do not contain digits, they contain a String which is comprised of characters. Are you asking how you find the length of a String? Have you looked at the API docs for the String class? String (Java Platform SE 6)

  4. #4
    MrPosotive is offline Member
    Join Date
    Sep 2012
    Posts
    68
    Rep Power
    0

    Default Re: how do i count spaces in java?

    Quote Originally Posted by DarrylBurke View Post
    Moved from Advanced Java. What made you think that the question belonged there?

    You need to clarify your question. As it stands, it doesn't make much sense, and the subject isn't in agreement with the text.

    db
    sorry Darry i must have accidentally clicked advanced, i didn't purposefully mean for it to go there.

  5. #5
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    356
    Rep Power
    5

    Default Re: how do i count spaces in java?

    So is your question how to find out the number of characters in your string, or how to find out the number of spaces in your string?

  6. #6
    MrPosotive is offline Member
    Join Date
    Sep 2012
    Posts
    68
    Rep Power
    0

    Default Re: how do i count spaces in java?

    how to count the characters and spaces so this would be "123 4.5" it would be a total of 7

  7. #7
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: how do i count spaces in java?

    So, thats just the length of the String.

    Java Code:
    String str = "Hey there";
    assert str.length() == 9;
    String (Java Platform SE 7 )

  8. #8
    MrPosotive is offline Member
    Join Date
    Sep 2012
    Posts
    68
    Rep Power
    0

    Default Re: how do i count spaces in java?

    Quote Originally Posted by quad64bit View Post
    So, thats just the length of the String.

    Java Code:
    String str = "Hey there";
    assert str.length() == 9;
    ok that helps but how would i use at with a scanner?
    Java Code:
    import java.io.*;
    import java.util.*;
    public class Prog500e {
    	public static void main(String args[])
    	{
    		
    		Scanner sc = new Scanner(System.in);
    		
    		System.out.println("Enter Title");
    		String c = sc.next();
    		assert c.length() == 25; 
    		int s = c;
    		int x;
    		for(x = s; x <=25; x++)
    		{
    			System.out.print( ".");
    		}
    	}
    }

  9. #9
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    758
    Rep Power
    8

    Default Re: how do i count spaces in java?

    What are you trying to do with the scanner? You program where the line says "int s = c;" is not valid. You cannot assign a string to an integer. Are you trying to read an integer number using scanner? Please take a look at other method provided by the Scanner class. A please make you question clearly next time :)

  10. #10
    MrPosotive is offline Member
    Join Date
    Sep 2012
    Posts
    68
    Rep Power
    0

    Default Re: how do i count spaces in java?

    ok can some one help me heres my code
    Java Code:
    import java.io.*;
    import java.util.*;
    public class Prog500e {
    	public static void main(String args[])
    	{
    		
    		Scanner sc = new Scanner(System.in);
    		
    		System.out.println("Enter Title");
    		String c = sc.next();
    		assert c.length() == 25; 
    		int x;
    		int s = Integer.parseInt(c);
    		for(x = s; x <=25; x++)
    		{
    			System.out.print( ".");
    		}
    	}
    }
    i am running into a problem, when i try to run it i get this error... "Enter Title
    cool cats are cool
    Exception in thread "main" java.lang.NumberFormatException: For input string: "cool"
    at java.lang.NumberFormatException.forInputString(Unk nown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at Prog500e.main(Prog500e.java:13)
    "

    if it helps this is the class assignment i got..."
    Program 500e
    (dot leader)

    Program Description: A table of contents in many books use periods to align text with the page numbers. These are called “dot leaders”. You are to write a program that accepts from the user a chapter title and a page number. Then you will output these to a line that contains exactly fifty characters. The space between the title and the page number will be filled with periods (“.”).

    For extra credit you may want to try alternating periods and spaces but be sure that there is always a space before the page number and after the chapter title.

    Statements Required: input, output, loop control, strings, and string methods

    Sample Output:
    Enter the title: An Introduction to Java
    Enter the page number: 5

    An Introduction to Java……………………….5

    Enter the title: Simple Data Types
    Enter the page number: 27


    Simple Data Types………………………..…27"

  11. #11
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    758
    Rep Power
    8

    Default Re: how do i count spaces in java?

    The java.util.Scanner have methods that allows you to read number. For example there is a method called Scanner.nextInt() which will give you an Integer. So that you don't need to convert from string to integer. There are also some validation methods, for example to check if the next token in the scanner contain an integer you can use Scanner.hasNextInt() method.

  12. #12
    MrPosotive is offline Member
    Join Date
    Sep 2012
    Posts
    68
    Rep Power
    0

    Default Re: how do i count spaces in java?

    Quote Originally Posted by wsaryada View Post
    The java.util.Scanner have methods that allows you to read number. For example there is a method called Scanner.nextInt() which will give you an Integer. So that you don't need to convert from string to integer. There are also some validation methods, for example to check if the next token in the scanner contain an integer you can use Scanner.hasNextInt() method.
    Ok but what would i do to count the spaces in the string?

  13. #13
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    758
    Rep Power
    8

    Default Re: how do i count spaces in java?

    Ok, let say you target output is this "An Introduction to Java……………………….5". It consists of the title, numbers of dots and the page number. Let say you want the total length of the string is 100 characters. Subtract by the title length and the number of characters for the page you will have the number of how many dots you have to print.

    As the instruction of your assignment said:
    1. Take the title string, you can use Scanner.nextLine();
    2. Take the page number, you can use Scanner.nextInt();
    3. Based on this information how many dots you have to fill to make it 100 characters length.
    4. Print the title
    5. You can create a loop to print the dots.
    6. Print the page number.

  14. #14
    MrPosotive is offline Member
    Join Date
    Sep 2012
    Posts
    68
    Rep Power
    0

    Default Re: how do i count spaces in java?

    Quote Originally Posted by wsaryada View Post
    Ok, let say you target output is this "An Introduction to Java……………………….5". It consists of the title, numbers of dots and the page number. Let say you want the total length of the string is 100 characters. Subtract by the title length and the number of characters for the page you will have the number of how many dots you have to print.

    As the instruction of your assignment said:
    1. Take the title string, you can use Scanner.nextLine();
    2. Take the page number, you can use Scanner.nextInt();
    3. Based on this information how many dots you have to fill to make it 100 characters length.
    4. Print the title
    5. You can create a loop to print the dots.
    6. Print the page number.
    yea i get that part but how am i supposed to get the number for the string, thats what im having trouble with.

Similar Threads

  1. Replies: 8
    Last Post: 01-21-2012, 01:14 AM
  2. Replies: 9
    Last Post: 03-06-2011, 03:00 AM
  3. Replies: 2
    Last Post: 02-06-2011, 04:38 AM
  4. Little help with java count++
    By ls7897 in forum New To Java
    Replies: 4
    Last Post: 11-23-2010, 04:01 AM
  5. How do I count empty spaces in a byte array?
    By nessa203 in forum New To Java
    Replies: 13
    Last Post: 01-11-2010, 05:46 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
  •