Counting blank spaces, and a few letters

Brand new to programming period

Page 1 of 1

3 Replies - 1793 Views - Last Post: 03 April 2009 - 09:22 AM Rate Topic: -----

#1 zachjjones   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 28-March 09

Counting blank spaces, and a few letters

Post icon  Posted 02 April 2009 - 10:03 PM

Hello,
I'm brand new to programming and like it so far. It's not easy, but nothing in life is (anything worthy anyway)
Anyway, I have a problem, count the blank spaces in the phrase the user inputs. I know there is a trick to it, but I seem to be getting stumped. Here it is.

import java.util.Scanner;

public class HW_04_04 {

	
	public static void main(String[] args) {
		
		String phrase;
		int countBlank;
		int length;
		char ch;
		
		Scanner scan = new Scanner(System.in);
		
		System.out.println();
		System.out.println("Character counter");
		System.out.println();
		
		System.out.print("Enter a sentence or phrase: ");
		phrase = scan.nextLine();
		length = phrase.length();
		
		countBlank = 0;
		
		for (int i=0; i < length; i++)
		{
			ch = phrase.charAt(i);
			
		}			
		
		
		System.out.println("Number of blank spaces: " + countBlank);
		System.out.println();
		

	}
}
I know the for loop is incorrect because it's not returning the correct amount of blank spaces. What I don't know, however, is why. I also have to count a few letters, would using a switch be an easier way?

switch (ch)
{
case 'a':
case 'A': countA++;
break;
case ....
}

?

Is This A Good Question/Topic? 0
  • +

Replies To: Counting blank spaces, and a few letters

#2 canty   User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 41
  • Joined: 26-January 08

Re: Counting blank spaces, and a few letters

Posted 02 April 2009 - 10:53 PM

hope this can help you
import java.util.Scanner;

public class HW_04_04 {

	public static void main(String[] args) {

		String phrase;

		Scanner scan = new Scanner(System.in);

		System.out.println();
		System.out.println("Character counter");
		System.out.println();

		System.out.print("Enter a sentence or phrase: ");
		phrase = scan.nextLine();

		System.out.println("Number of blank spaces: " + (phrase.split(" ").length - 1));
		System.out.println();

	}
}


Was This Post Helpful? 0
  • +
  • -

#3 zachjjones   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 28-March 09

Re: Counting blank spaces, and a few letters

Posted 03 April 2009 - 06:48 AM

Yes, that code does work! But the problem is I'm supposed to be learning how to use a for loop and this is kind of a loop-hole for learning this! haha, what does the .split method do? And, I have to use a for loop in this. I could stick that assignment inside it, but I have to move to the next character every time. i have no idea how to get to that point.
Was This Post Helpful? 0
  • +
  • -

#4 shwcsmack   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 03-April 09

Re: Counting blank spaces, and a few letters

Posted 03 April 2009 - 09:22 AM

I did this for an assignment not to long ago, it was slightly different, but this might help. I used swing instead of command line but thats an easy fix.

// Programmer:  Shayne Stewart
import javax.swing.JOptionPane;
import java.lang.*;

public class stringCounter
{
	public static void main(String[] args)
	{
		//Declare
		int numA = 0,numE = 0,numI = 0,numO = 0,numU = 0;
		int constenants = 0,spaces = 0,puncMarks = 0;
		int index = 0;
		char check;  
		String entry = "";
		String output = "";
		int lengthout;
		
		//Get the string
		entry = JOptionPane.showInputDialog("Enter a sentance");
		lengthout = entry.length();
		
		//Figure out how many of each vowel
		for (int length = entry.length(); length > 0; length--)
		{
			check = entry.charAt(index);
			if (check == 'A' || check == 'a')
			{
				numA++;
			}
			else if (check == 'E' || check == 'e')
			{
				numE++;
			}
			else if (check == 'I' || check == 'i')
			{
				numI++;
			}
						
			else if (check == 'O' || check == 'o')
			{
				numO++;
			}
			else if (check == 'U' || check == 'u')
			{
				numU++;
			}
			else if (check == ' ')
			{
				spaces++;
			}
			else if (check == '.' || check == ',' || check == '?' || check == '\'' || check == '!')
			{
				puncMarks++;
			}
			else
			{
				constenants++;
			}						
			index++;			
		}
		
		
		output = "In the sentance: " + "\n" 
					 + "\""+ entry + "\"" + "\n"
					 + "There are " + lengthout + " total characters" + "\n"
					 + "\n"
					 + "Broken Down:" + "\n";
		
		output += "A: " + numA + "\n"
					 + "E: " + numE + "\n"
					 + "I: " + numI + "\n"
					 + "O: " + numO + "\n"
					 + "U: " + numU + "\n"
					 + "Spaces: " + spaces + "\n"
					 + "Punctuation Marks: " + puncMarks + "\n"
					 + "Constanants: " + constenants + "\n";
					 
		//Output
		JOptionPane.showMessageDialog(null,output,"",JOptionPane.INFORMATION_MESSAGE);

	}
}

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1