Hi

I'm i beginner in java :$
and i have a problem .... with the code

they want from me to write a program that reads and prints the number of occurrence of each alphabetical letter in the string . the program should call a method to creat an array for the counters of the letters , and use another overloaded method that receives a character and updates the corresponding counter and finally call a third method to print the counts


this is my code and i just cant get it right ....!!

import java.util.*;
import java.lang.*;
public class Q2
{
public static void main (String args[])
{
Scanner console=new Scanner(System.in);

int []coun;



System.out.println("Enter the Sentence");
String str=console.nextLine();
coun=new int[str.length()];
str=str.toLowerCase();
int i=0;
while(i<str.length()){
char s=str.charAt(i);
int counter=0;
if(Character.isLetter(str.charAt(i)))

for(int j=0;j<str.length();j++){
char t=str.charAt(j);
if(Character.isLetter(str.charAt(j)))
if(t==s)
counter++;
i++;
System.out.print(t+"="+(counter)+",");

}}
counterr(counter,coun);// here there is a compile error that symbol  : variable counter
}


public static void counterr (int counter,int[] coun)
{
for(int index=0;index<coun.length;index++)
coun[index]=counter;




}

}

and when i remove the method and leave only the main the run is like that

Enter the Sentence
java
j=1,a=1,v=1,a=1,

:(

Recommended Answers

All 7 Replies

int counter = 0;
This varible need to be declared before while-loop L:18

it can simply be like that

public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here

        System.out.println("please enter your sentence: ");
        Scanner in = new Scanner(System.in);
        String input = in.nextLine();

        int counter = 0;
        String taken = "";
        
        for (int i = 0; i < input.length(); i++) {

            String key = input.substring(i , i+1);

            if(taken.contains(key)) continue;

            for (int j = 0; j < input.length(); j++) {

                if(key.compareTo(input.substring(j , j+1)) == 0){ counter++; }//end of if

            }//end of inner for loop

            System.out.println(key + " : " + counter); taken += key; counter = 0;

        }//end of outer for loop

    }//end of main method

}//end of main class

sorry but i cant get it :(

is my code wrong because i used charAt?!!

i mean why didn't u do it with charAT and == ??!!

isn't it easier than substring ?!! and compare ?!!


and another Question ... is my code correct i mean should i do that
loop then i fill the array ?!!

or i should count with the array ?!!

thank u very much <333

I think you can do it anyway with sub-string or charArt(x)... I think with charArt is easier. Following the K.I.S.S rule.
I tried recraeating the whole code.Please note that my code only tests for the occurence of the first letter of the sentence will try finishing the code during this week using a class and methods.....:D

/*Q2.java...
 *Dennis Wanyoike
 *madawarstudies@gmail.com
 *Please note that i have only tried to check the occurence of the first letter only
 **/
import javax.swing.*;
public class Q2
{
	public static void main(String args[])
	{
		String sentence;
		String c_sentence;
		int size,no=0,post=0;
		
		
		
		
		sentence=JOptionPane.showInputDialog("Enter Your Sentence!");
		sentence=sentence.toUpperCase();
		c_sentence=sentence;
		
		/*gets size of input string with whitespaces
		 *dont know how to remove whitespaces yet as they are counted
		 *in the loop below as characters*/
		
		size=sentence.length();
		
		//prints out inputted characters
		for(int x=0;x<size;x++)
		{
			System.out.println(sentence.charAt(x));
		}
		//checks for occurences of first letter in the sentence only
		for (int y=0;y<size;y++)
		{
			if(sentence.charAt(post)==c_sentence.charAt(y))
			{
				
				no=no+1;
				
			}
			
		
		JOptionPane.showMessageDialog(null,"Occurence of " +sentence.charAt(0)+" is "+no);	
		}
	
	}
}

I also do think that use of ArrayList would be suitable as the sentence has varying number of characters and it is impossible to determine the number of indexes needed

java 1990 you can do it by substring or charAt no problem , using substring seems to be much more harder but i really prefer , by using charAt the code will be like this

public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here

        System.out.println("please enter tour sentence: ");
        Scanner in = new Scanner(System.in);
        String input = in.nextLine();

        String taken = "";

        for (int i = 0; i < input.length(); i++) {

            char key = input.charAt(i); int counter = 0;

            if(check(key, taken)) continue; //the key was previously found so we will not take it

            for (int j = 0; j < input.length(); j++) {

                if(key == input.charAt(j)){ counter++; taken += key;}

            }//end of inner for loop

            System.out.println(key + " : " + counter);

        }//end of outer for loop

    }//end of main method

    public static boolean check(char key , String taken){

        boolean found = false;
        
        for (int i = 0; i < taken.length(); i++) {
            
            if(key == taken.charAt(i)){ found = true; break; }//end of if
            
        }//end of for loop

        return found;

    }//end of check

}//end of main class

by looking at your code , if(Character.isLetter(str.charAt(j)) i don't really know what this method does. bro try to trace it step by step and believe me you will get it.

by the way java 1990 if you look at my code you will find that i created a string called taken which i put in what i have taken , and check every time so i can't take it again(which happened in you code)so TRY it and ENJOY

madawar it is more simple than doing it by array list believe...ENJOY

thank u very very much u helped me a lot ...!!

i want at least the general idea so thanks ...

they want us to do it by array ... i will do like urs but with arrays ...!!

abdel

by looking at your code , if(Character.isLetter(str.charAt(j))

if it space then i dont want to count it ;$

u know i think my problem was with taken ""

thank u <333

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.