Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

char count

807607Nov 21 2006 — edited Nov 30 2006
i need to count characters in a string and display output. the way im doing it is below. but its stupid. im sure theres gotta be a better way. im just new to for loops with characters in them.
public ISDecipher(){
	try{
	FileInputStream fin= new FileInputStream("cipher.txt");
	//FileOutputStream fout = new FileOutputStream("decipher.txt");
	//Rot13FilterOutput out = new Rot13FilterOutput(fout);
	
	String cipher = StreamCopier.copyToString(fin);
	
	char[] cipherChar = cipher.toCharArray();
	int a = 0;
	int b = 0;
	int c = 0;
	int d = 0;
	int e = 0;
	int f = 0;
	int g = 0;
	int h = 0;
	int i = 0;
	int j = 0;
	int k = 0;
	int l = 0;
	int m = 0;
	int n = 0;
	int o = 0;
	int p = 0;
	int q = 0;
	int r = 0;
	int s = 0;
	int t = 0;
	int u = 0;
	int v = 0;
	int w = 0;
	int x = 0;
	int y = 0;
	int z = 0;
	
	for(int index = 0; index<cipherChar.length; index++){
				
		if(cipherChar[index]=='A'){// looking for 'a' only
		a++;
		}
		if(cipherChar[index]=='B'){// looking for 'B' only
			b++;
			}
		if(cipherChar[index]=='C'){// looking for 'a' only
			c++;
			}
		if(cipherChar[index]=='D'){// looking for 'a' only
			d++;
			}
		if(cipherChar[index]=='E'){// looking for 'a' only
			e++;
			}
		if(cipherChar[index]=='F'){// looking for 'a' only
			f++;
			}
		if(cipherChar[index]=='G'){// looking for 'a' only
			g++;
			}
		if(cipherChar[index]=='H'){// looking for 'a' only
			h++;
			}
		if(cipherChar[index]=='I'){// looking for 'a' only
			i++;
			}
		if(cipherChar[index]=='J'){// looking for 'a' only
			j++;
			}
		if(cipherChar[index]=='K'){// looking for 'a' only
			k++;
			}
		if(cipherChar[index]=='L'){// looking for 'a' only
			l++;
			}
		if(cipherChar[index]=='M'){// looking for 'a' only
			m++;
			}
		if(cipherChar[index]=='N'){// looking for 'a' only
			n++;
			}
		if(cipherChar[index]=='O'){// looking for 'a' only
			o++;
			}
		if(cipherChar[index]=='P'){// looking for 'a' only
			p++;
			}
		if(cipherChar[index]=='Q'){// looking for 'a' only
			q++;
			}
		if(cipherChar[index]=='R'){// looking for 'a' only
			r++;
			}
		if(cipherChar[index]=='S'){// looking for 'a' only
			s++;
			}
		if(cipherChar[index]=='T'){// looking for 'a' only
			t++;
			}
		if(cipherChar[index]=='U'){// looking for 'a' only
			u++;
			}
		if(cipherChar[index]=='V'){// looking for 'a' only
			v++;
			}
		if(cipherChar[index]=='W'){// looking for 'a' only
			w++;
			}
		if(cipherChar[index]=='X'){// looking for 'a' only
			x++;
			}
		if(cipherChar[index]=='Y'){// looking for 'a' only
			y++;
			}
		if(cipherChar[index]=='Z'){// looking for 'a' only
			z++;
			}
	}
	
		System.out.println("Total chars "+cipherChar.length);
		System.out.println("Number of 'a' are "+a);
		System.out.println("Number of 'b' are "+b);
		System.out.println("Number of 'c' are "+c);
		System.out.println("Number of 'd' are "+d);
		System.out.println("Number of 'e' are "+e);
		System.out.println("Number of 'f' are "+f);
		System.out.println("Number of 'g' are "+g);
		System.out.println("Number of 'h' are "+h);
		System.out.println("Number of 'i' are "+i);
		System.out.println("Number of 'j' are "+j);
		System.out.println("Number of 'k' are "+k);
		System.out.println("Number of 'l' are "+l);
		System.out.println("Number of 'm' are "+m);
		System.out.println("Number of 'n' are "+n);
		System.out.println("Number of 'o' are "+o);
		System.out.println("Number of 'p' are "+p);
		System.out.println("Number of 'q' are "+q);
		System.out.println("Number of 'r' are "+r);
		System.out.println("Number of 's' are "+s);
		System.out.println("Number of 't' are "+t);
		System.out.println("Number of 'u' are "+u);
		System.out.println("Number of 'v' are "+v);
		System.out.println("Number of 'w' are "+w);
		System.out.println("Number of 'x' are "+x);
		System.out.println("Number of 'y' are "+y);
		System.out.println("Number of 'z' are "+z);


	}catch(IOException ioe){}
}
	public static void main(String[] args) {
		new ISDecipher();

	}

}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 28 2006
Added on Nov 21 2006
26 comments
426 views