0

I have this task where I'm supposed to write a method that counts number of consonants in the input word. I'm not sure what the problem is since my output is always 0 (same as declared in the beginning with int number=0; ) and when I try without declaring the value It just gives me error on method calling. Are there any mistakes that I'm not aware of?

The code:

import java.util.*;
public class Student {
         public static void main(String args[]) {
         Scanner scan = new Scanner(System.in);
         String beseda;
         int number=0;;
         System.out.println("Please input your string: " );
         beseda = scan.nextLine();
         
         new Work().prestej(beseda, number);
         System.out.println("The number of consonants is: " + number);
             
         }
         
}
             
class Work   {
    
    int prestej(String beseda, int number) {
        int lenght = beseda.length();
        for (int i=0; i<lenght; i++) {
            if (beseda.charAt(i) != 'a' || beseda.charAt(i) != 'u' || beseda.charAt(i) != 'e' || beseda.charAt(i) != 'i' || beseda.charAt(i) != 'o' ) {
                number++;
            
            } else {
                break;
            }
           
        }
        
        
        return number;
        
    }
}
Dave Newton
  • 158,873
  • 26
  • 254
  • 302
  • 5
    The method returns an integer which you ignore; don't ignore it :) Your next question will be "why doesn't it count consonants correctly", which is because of the logic condition checking the `charAt`--think carefully about the differences between "and" and "or", and what the condition needs to be to identify a consonant. – Dave Newton Sep 07 '21 at 13:34
  • 1
    This is just a style comment to make your coding life easier. Inside the loop, do `char ch = beseda.charAt(i);` Then use `ch` in your if statement. As far as your actual issue goes, the other comment and [Iván's](https://stackoverflow.com/a/69089238/1552534) answer addresses the issues. – WJS Sep 07 '21 at 13:49
  • Does this answer your question? [How do I pass a primitive data type by reference?](https://stackoverflow.com/questions/4319537/how-do-i-pass-a-primitive-data-type-by-reference) – admlz635 Sep 07 '21 at 14:04

4 Answers4

3

Instead of the break;, remove the else branch. You want to continue counting, not break the loop.

Also, the if conditions should be chained with &&. With || it will be always true (Every letter is different from 'a' or 'b').

Finally, you should assign the return value of the method to a variable. Instead of:

new Work().prestej(beseda, number);

Assign the value:

number = new Work().prestej(beseda, number);
Iván
  • 971
  • 6
  • 20
  • Good answer but *On the other side* is somewhat ambiguous and may not be clear to a novice. – WJS Sep 07 '21 at 14:23
  • You really don't need a `continue` as the last thing in a loop . . . – David Conrad Sep 07 '21 at 14:32
  • Edited the answer with an explanation, and removed the `continue` thing, that yeah, I wanted to be completist, but it could be misleading – Iván Sep 07 '21 at 15:11
0

There are several issue with your code:

  1. in Java, method parameters are passed by value, so when you do ++number in your method prestej, you're not altering the value of the number variable that is declared in your main method,
  2. you are ignoring the value returned by method prestej,
  3. the break in the else branch will exit the loop as soon as a vowel is encountered.
Maurice Perry
  • 9,261
  • 2
  • 12
  • 24
-1

I cleaned up your code just so you can get a clear idea. You don't have to pass "number" variable to method "prestej" since you are always passing the value 0. Instead define a method local variable inside prestej.

public class Student {
    public static void main(String args[]) {
        System.out.println("Please input your string: ");
        Scanner scan = new Scanner(System.in);
        String beseda = scan.nextLine();
        int number = new Work().prestej(beseda);
        System.out.println("The number of consonants is: " + number);
    }
}

class Work {
    int prestej(String beseda) {
        int number = 0;
        for (int i = 0; i < beseda.length(); i++) {
            if (beseda.charAt(i) != 'a' || beseda.charAt(i) != 'u' || beseda.charAt(i) != 'e' || beseda.charAt(i) != 'i' || beseda.charAt(i) != 'o') {
               number++;
            } else {
               break;
            }
        }
        return number;
    }
}
Poorna
  • 72
  • 8
-2

The problem is that although you return the updated number variable in your Work class, you don't assign this return value in the Student class, so in its context the value never changes.

Assigning it like this should fix your problem :

number = new Work().prestej(beseda, number);
iAmAGuest
  • 20
  • 1