a simple problem but i have a problem.

a simple problem but i have a problem.

Page 1 of 1

3 Replies - 387 Views - Last Post: 25 August 2009 - 04:10 AM Rate Topic: -----

#1 lyrad37  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 29-January 09

a simple problem but i have a problem.

Post icon  Posted 24 August 2009 - 10:01 AM

import javax.swing.JOptionPane;
import java.util.*;
import java.util.Scanner;

public class binotbot {


	
	public static void main(String args[]) {
		Scanner sc = new Scanner(System.in);
		
	String[] sentence;
	
	int a, count;
	
	
	System.out.println("Enter sentence: ");
	sentence = sc.next().charAt();

	for ( a=0 , count=0; a < sentence.length; a++)
	{
		if (sentence[a] == " ")
			count++;
	}
	
	JOptionPane.showMessageDialog(null, "there are "+count+" words");

		
		System.exit(0);
	}
	
}




i'm having problem on how will read the string sentence. my objective here is to print out the number of words entered. i am not sure if this would end up on counting the number of words.

Is This A Good Question/Topic? 0
  • +

Replies To: a simple problem but i have a problem.

#2 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: a simple problem but i have a problem.

Posted 24 August 2009 - 10:41 AM

String s = "blah blah blah blah";

String [] words = s.split(" "); //splits the string into an array of strings 



to find out the number of words just print the size of the array
Was This Post Helpful? 0
  • +
  • -

#3 AntonWebsters  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 88
  • View blog
  • Posts: 428
  • Joined: 15-August 09

Re: a simple problem but i have a problem.

Posted 24 August 2009 - 10:58 AM

Hope this helps.

import javax.swing.JOptionPane;
import java.util.*;
import java.util.Scanner;

public class binotbot {


	
	public static void main(String args[]) {
		Scanner sc = new Scanner(System.in);
		
	String sentence;
	
	int a;
	int count = 0;
	
	
	System.out.println("Enter sentence: ");
	sentence = sc.nextLine();

	count = isAlpha(sentence);
	
	System.out.println(count);
	
		
		System.exit(0);
	}
	public static int isAlpha(String s)
	{
		
		int count = 0;
		
		s = s.toUpperCase();
  
		for (int i = 0; i < s.length(); i ++)
  
		{
	
			int c = (int) s.charAt(i);

	
			if (c < 65 || c > 90)
	  
				System.exit(0);
			
			else
				count++;
		}
		
		

  
		return count;

	}
}



Was This Post Helpful? 0
  • +
  • -

#4 lyrad37  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 29-January 09

Re: a simple problem but i have a problem.

Posted 25 August 2009 - 04:10 AM

how do i use this .split thing? can u give me a sample line on how to use .split. please. i am still studying the other code replied to me.

This post has been edited by lyrad37: 25 August 2009 - 04:11 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1