how would i find out how many digits this is? System.out.println("Whats up dock?"); like the digits in the parenthesizes.
Printable View
how would i find out how many digits this is? System.out.println("Whats up dock?"); like the digits in the parenthesizes.
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
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)
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?
how to count the characters and spaces so this would be "123 4.5" it would be a total of 7
So, thats just the length of the String.
String (Java Platform SE 7 )Code:String str = "Hey there";
assert str.length() == 9;
So, thats just the length of the String.
ok that helps but how would i use at with a scanner?Code:String str = "Hey there";
assert str.length() == 9;
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( ".");
}
}
}
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 :)
ok can some one help me heres my codei am running into a problem, when i try to run it i get this error... "Enter TitleCode: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"
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, 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.