4 Replies - 243 Views - Last Post: 16 February 2012 - 06:30 AM Rate Topic: -----

#1 Kalle114  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 01-February 12

counting letters

Posted 16 February 2012 - 05:38 AM

import java.util.Scanner;
class indexerad_variable
{ 
public static void main(String[] args)
{ 

	Scanner scan = new Scanner(System.in);

	String word = " ";
	int count = 0;
	int index = 0;

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

	while (index < word.length())
	{ 
	switch (word.toUpperCase().charAt(index))
	{ 
	case 'A': count++; 
	break; 

	case 'E': count++;
	break; 

	case 'I': count++;
	break; 

	case 'O': count++;
	break; 

	case 'U': count++;
	break; 
	} 
	index++;
	} 
	System.out.println(count+" st "+'A'+":n");
	System.out.println(count+" st "+'E'+":n");
	System.out.println(count+" st "+'I'+":n");
	System.out.println(count+" st "+'O'+":n");
	System.out.println(count+" st "+'U'+":n");
	} 
	} 


when i type in a sentence like mom its going to count that it is only 1 o in it but now it to it to every one and that i know way but i want to know how to do it like it only type 1 in to o and have 0 in to the rest. dont need the code because it is a homework only help so i can do it my self

Is This A Good Question/Topic? 0
  • +

Replies To: counting letters

#2 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2657
  • View blog
  • Posts: 11,217
  • Joined: 20-September 08

Re: counting letters

Posted 16 February 2012 - 05:47 AM

I don't understand your question but consider using an array. Here's a clue

array[word.toUpperCase().charAt(index) - 'A']++;

Was This Post Helpful? 1
  • +
  • -

#3 Kalle114  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 01-February 12

Re: counting letters

Posted 16 February 2012 - 05:51 AM

ok when i type in mom into Enter a word or sentence: then i whant it to look like this

0 st A:n
0 st E:n
0 st I:n
1 st O:n
0 st U:n
Was This Post Helpful? 0
  • +
  • -

#4 Karel-Lodewijk  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 449
  • View blog
  • Posts: 849
  • Joined: 17-March 11

Re: counting letters

Posted 16 February 2012 - 06:07 AM

The problem is that you have but one count variable, but you want to count each vowel independently. you could add some more counters i.e.:

int count_a = 0;
int count_e = 0;
...


This post has been edited by Karel-Lodewijk: 16 February 2012 - 06:08 AM

Was This Post Helpful? 1
  • +
  • -

#5 Kalle114  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 01-February 12

Re: counting letters

Posted 16 February 2012 - 06:30 AM

thx for the help
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1