Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript How-To
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old March 7th, 2014, 07:28 PM
Registered User
Points: 5, Level: 1
Points: 5, Level: 1 Points: 5, Level: 1 Points: 5, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
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) == ' ')
{
countdots++;
}

}

// Print the results
System.out.println();
System.out.println ("Number of dots: " + countdots);
System.out.println ();
//Close input
scan.close();
}
}
Reply With Quote
  #2 (permalink)  
Old March 10th, 2014, 06:59 AM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

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.
__________________
Joe
http://joe.fawcett.name/
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Example of Java script in c# arunumane2002 Javascript How-To 4 March 14th, 2017 08:43 AM
java Script raj1_syed Javascript How-To 1 August 17th, 2006 02:49 PM
java script nalla ASP.NET 1.0 and 1.1 Professional 8 June 23rd, 2006 07: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 05:28 PM



All times are GMT -4. The time now is 10:35 PM.


Powered by vBulletin®
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.