Wrox Programmer Forums
Javascript How-To Ask your "How do I do this with Javascript?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old March 7th, 2014, 07:28 PM
Registered User
Join Date: Mar 2014
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default help with a java script

im trying to fix my java script to count the dots. here is my code if any one can help. i had it set up to count spaces but cant figure out the dots lol
import java.util.Scanner;
public class Assign5_blanton
public static void main (String[] args)
String phrase; // a string of characters
int countdots; // the number of dots
//Initialize input
Scanner scan = new Scanner(System.in);

// Print a program header
System.out.println ();
System.out.println ("Character Counter");
System.out.println ();

// Read in a string
System.out.print ("Enter dots: ");
phrase = scan.nextLine();

// Initialize counts
countdots = 0;

// a for loop to go through the string character by character

for(int i=0; i < phrase.length(); i++)
if(phrase.charAt(i) == ' ')


// Print the results
System.out.println ("Number of dots: " + countdots);
System.out.println ();
//Close input
Old March 10th, 2014, 05:59 AM
joefawcett's Avatar
Wrox Author
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts

You need to understand the difference between Java and JavaScript, this forum is for the latter.

That being said there are two ways to attack this problem (maybe more...). One is to loop through the string and count the dots (by comparing to '.'), the other is to remove all non-dots (possibly with a regular expression) and then to get the length of the remaining string.

Similar Threads
Thread Thread Starter Forum Replies Last Post
Example of Java script in c# arunumane2002 Javascript How-To 4 March 14th, 2017 07:43 AM
java Script raj1_syed Javascript How-To 1 August 17th, 2006 01:49 PM
java script nalla ASP.NET 1.0 and 1.1 Professional 8 June 23rd, 2006 06:24 AM
java script madangcse@gmail.com Javascript How-To 1 March 22nd, 2006 04:10 AM
Java vs Java Script functions joemorrison74 J2EE 0 July 6th, 2005 04:28 PM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.