Results 1 to 14 of 14
Thread: how do i count spaces in java?
- 11-29-2012, 04:51 AM #1
Member
- Join Date
- Sep 2012
- Posts
- 68
- Rep Power
- 0
- 11-29-2012, 05:16 AM #2
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.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 11-29-2012, 01:26 PM #3
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)
- 11-29-2012, 06:43 PM #4
Member
- Join Date
- Sep 2012
- Posts
- 68
- Rep Power
- 0
- 11-29-2012, 07:09 PM #5
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?
- 11-29-2012, 10:21 PM #6
Member
- Join Date
- Sep 2012
- Posts
- 68
- Rep Power
- 0
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
- 11-29-2012, 10:34 PM #7
Re: how do i count spaces in java?
So, thats just the length of the String.
String (Java Platform SE 7 )Java Code:String str = "Hey there"; assert str.length() == 9;
- 11-30-2012, 02:43 AM #8
Member
- Join Date
- Sep 2012
- Posts
- 68
- Rep Power
- 0
Re: how do i count spaces in java?
So, thats just the length of the String.
ok that helps but how would i use at with a scanner?Java Code:String str = "Hey there"; assert str.length() == 9;
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( "."); } } }
- 11-30-2012, 03:02 AM #9
Senior Member
- Join Date
- Jun 2007
- Location
- Bali, Indonesia
- Posts
- 697
- Rep Power
- 6
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 :)
Website: Learn Java by Examples
- 11-30-2012, 03:02 AM #10
Member
- Join Date
- Sep 2012
- Posts
- 68
- Rep Power
- 0
Re: how do i count spaces in java?
ok can some one help me heres my code
i am running into a problem, when i try to run it i get this error... "Enter TitleJava 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( "."); } } }
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-30-2012, 03:15 AM #11
Senior Member
- Join Date
- Jun 2007
- Location
- Bali, Indonesia
- Posts
- 697
- Rep Power
- 6
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.
Website: Learn Java by Examples
- 11-30-2012, 03:18 AM #12
Member
- Join Date
- Sep 2012
- Posts
- 68
- Rep Power
- 0
- 11-30-2012, 03:41 AM #13
Senior Member
- Join Date
- Jun 2007
- Location
- Bali, Indonesia
- Posts
- 697
- Rep Power
- 6
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.Website: Learn Java by Examples
- 11-30-2012, 04:07 AM #14
Member
- Join Date
- Sep 2012
- Posts
- 68
- Rep Power
- 0
Similar Threads
-
Easy Question! Print out file, count number of lines, number of spaces w/ Scanner
By LogicalOutlier in forum New To JavaReplies: 8Last Post: 01-21-2012, 01:14 AM -
Help w/ java programming assignment counting number of spaces
By clemsontigers in forum New To JavaReplies: 9Last Post: 03-06-2011, 03:00 AM -
Mac OS X, Swing & Spaces - how to determine on which space given Java window is?
By pepa_u in forum AWT / SwingReplies: 2Last Post: 02-06-2011, 04:38 AM -
Little help with java count++
By ls7897 in forum New To JavaReplies: 4Last Post: 11-23-2010, 04:01 AM -
How do I count empty spaces in a byte array?
By nessa203 in forum New To JavaReplies: 13Last Post: 01-11-2010, 05:46 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks