[Solved] Java Sentence Analysis Check


Your code is confuse, but for what I understood, you want to analyse a certain phrase.

First, the constructor of the class that handles the analyse must receive the String to analyse, since it is it’s job. (Every class has a job, the result it’s obtained from the classes methods).

Second, there is a convention on Java naming, and for classes the names start with capital letter. Check this document from Oracle: http://www.oracle.com/technetwork/java/codeconventions-135099.html

So, the class should be something like this:

public class Odev1 {

    private final String phase;

    public Odev1( String phrase ) {
        this.phrase = phrase;
    }

    //Remaining code...
}

As for the analyse itself there is several things that you should have in mind:

First, you don’t have to to create an array of char from the String, you can just get the char directly with the String‘s charAt( int index ) method.

Second, when you have a for-loop, you can use the same leters, since the variables are local and therefor they won’t be visible out of the for-loop

Third, when you construct a loop, you must make sure that you can break out of the loop. As an example, you wrote:

int n=0;
for(int a=0;a<characters;a++) {
    do{
        n=n+1;
    }while(array[a]!='0'); 
}
words=characters-n;

Just here you have an infinite loop. Why are you in a do-while looping over the same char? Follow this:
int n = 0; => int a=0; => do => n = n + 1 -> n = 1 => condition while => array[a]!='0' -> true => repeat do-while loop… Forever…

Instead, you could iterate over the String and use if-else conditions to analyse it. Something like this:

int n=0;
boolean isWord = false; //Use it to track the letters
boolean isSpace = false;
for(int i=0;i<characters;i++) {
    char charToAnalyse = input.getCharAt( i );

    if( charToAnalyse == ' ' ) { //Taking the simple case where you don't have breaklines
        isSpace = true;
        isWord = false;
    }
    else {
        isSpace = false;
        if( !isWord ) {
            isWord = true; //once it is counted, we should avoid recount.
            n++; //Its is the same as n = n + 1;
        }
    }
}
words=characters-n;

As a simpler way, you could use the split(String regex) method, but I gave the example above to help you understand.

String[] wordsArray = input.split( " " );
words = wordsArray.lenght;

Forth, use else-if instead of constantly if‘s if the inclusion of one means the exclusion of the remain. That is, in your code above you iterate over the letters to see if they are capital or not.

Also, you don’t have to write a loop for each variable. Each character has several atributes, so just check them all in a single loop:

int capitalletters = 0;
int lowerletters = 0;
int vowel = 0;

for(int i=0;i<characters;i++) {
    char charToAnalyse = input.charAt(i);

    if (Character.isUpperCase( charToAnalyse )) { 
        capitalletters++; 
    }
    else if (Character.isLowerCase( charToAnalyse  )){ 
        lowerletters++; 
    }
    //Imagine if there were many if statement and the condition 
    //was already true in the first...

    //count vowels;
    switch( charToAnalyse  ) {
        case 'a':
        case 'e':
        case 'ı':    
        case 'i':
        case 'o':
        case 'ö':
        case 'u':
        case 'ü':
            vowel++;
        break; //You can omit this one if it is at the end;
    }

}

In the end, to use the class Odev1, just do:

Odev1 cumle = new Odev1( input );
cumle.analyse();

I hope I have helped.

Have a nice day. 🙂

2

solved Java Sentence Analysis Check