7 Replies - 20676 Views - Last Post: 24 October 2010 - 08:42 PM Rate Topic: -----

#1 needHELP!  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 07-March 09

counting spaces in a string using while loop

Posted 06 May 2009 - 08:13 PM

hey im trying to make a program that counts the number of spaces in a string, but i HAVE to use a while loop,
i have this but it jsut executes and nothing happens, :

public class Spaces{

public static void main(String[] args){

int count=0;
String text= "hello, my name is barry";

for(int i=0; i<text.length(); i++)
{
while(text.charAt(i)==' ')
{
count++;
}
}
System.out.println(count);


}
}



Is This A Good Question/Topic? 0
  • +

Replies To: counting spaces in a string using while loop

#2 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • Joined: 14-September 07

Re: counting spaces in a string using while loop

Posted 06 May 2009 - 08:15 PM

If you HAVE to use a while loop, make it a fake-y for loop:

int i = 0, count = 0;
while(i < text.length())
{
	 if(text.charAt(i) == ' ')
		   count++;

	i++;
}




edit: And it doesn't do anything because if there is a space it enters the while loop and never exits.

This post has been edited by KYA: 06 May 2009 - 08:16 PM

Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: counting spaces in a string using while loop

Posted 06 May 2009 - 08:18 PM

do not make it complicated for nothing

String str = "Free advice worthes every cents";
char[] digit = str.toCharArray();

int nbSpaces = 0;
for(int i = 0; i < digit.length; i++) {
	if(digit[i] == ' ')
	   nbSpaces++
}


Was This Post Helpful? 0
  • +
  • -

#4 needHELP!  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 07-March 09

Re: counting spaces in a string using while loop

Posted 06 May 2009 - 08:41 PM

thanks a lot!
Was This Post Helpful? 0
  • +
  • -

#5 Guest_john*


Reputation:

Re: counting spaces in a string using while loop

Posted 24 October 2010 - 09:25 AM

how do u do this if i used a scanner for string? like instead of the string being set in like -word="hello world" ...if i used word=input.next(); ...it doesnt recognize more than 1 word..like if i typed in "hello world", it would only get the hello part..what am i doing wrong
Was This Post Helpful? 0

#6 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • Joined: 14-September 07

Re: counting spaces in a string using while loop

Posted 24 October 2010 - 09:32 AM

Get a whole line rather then tokens.
Was This Post Helpful? 0
  • +
  • -

#7 Guest_JOHN*


Reputation:

Re: counting spaces in a string using while loop

Posted 24 October 2010 - 08:35 PM

tty so much KYA
Was This Post Helpful? 0

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: counting spaces in a string using while loop

Posted 24 October 2010 - 08:42 PM

word=input.nextLine(); :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1