0
public class Main {

   public static void main(String []args){
      Scanner reader = new Scanner(System.in);
      System.out.print("Enter word: ");
      String text = reader.nextLine();
      System.out.println(calculateCharacters(tex));
      reader.close();
   }

  public static int calculateCharacters(String text, int tex){    
     tex = text.length();
     return tex;
  }
}

So I receive a string from String text, then I send it to the method to calculate it's length and return a number which should be intercepted by System.out.println(calculateCharacters(tex)); and the probram should show me the number of letters in the string that was entered, the problem is: nothing reaches System.out.println(calculateCharacters(tex)); why ? where is this return tex; returning it then ?

Jees K Denny
  • 536
  • 5
  • 24
Liky
  • 11
  • 1

3 Answers3

1

Well I'm not entirely sure why you've got an int texvariable, but removing it lets this work perfectly. Try rewriting your method:

public static int calculateCharacters(String text) {
    int tex = text.length();
    return tex;
}

or if you're ready to be snazzy:

public static int calculateCharacters(String text) {
    return text.length();
}

Java is pass-by-value (Is Java "pass-by-reference" or "pass-by-value"?), so keeping an extra int in there that you only use to store things locally won't actually change your value of tex.

Also, you instantiate the String text, but then pass tex to calculateCharacters(). Since you haven't created a texvariable before this, your compiler doesn't know what to pass to calculateCharacters(). Try changing that to:

System.out.println(calculateCharacters(text)); 

instead.

Community
  • 1
  • 1
Lord Farquaad
  • 662
  • 1
  • 11
  • 29
0
public class Main {
    public static void main(String[] args) {
        Scanner reader = new Scanner(System.in);
        System.out.print("Enter word: ");
        String text = reader.nextLine();
        System.out.println(calculateCharacters(text));
        reader.close();
    }

    public static int calculateCharacters(String text) {
        int tex = text.length();
        return tex;

    }
}

It works

Ankush soni
  • 1,325
  • 1
  • 13
  • 29
  • It works, thanks, but I am returning a variable `tex` and in the `System.out.println(calculateCharacters(text));` I haven't mentioned it, how does it know it should send it there ? – Liky Aug 16 '16 at 18:21
  • You'll probably want to look up a bit about scopes: https://www.cs.umd.edu/~clin/MoreJava/Objects/local.html , but I can also try to help a bit. Anything that you create inside those curly braces only exists within them. It's true that you've got an int tex inside your calculateCharacters() method, but the moment that method returns and your control flow leaves that method, tex is forgotten. However, in your main() method, you call calculateCharacters(text). When a method returns, it pretty much replaces it's own declaration with it's value. – Lord Farquaad Aug 16 '16 at 18:27
  • So if `calculateCharacters(text)` returns `4` (just as an example), the line `System.out.println(calculateCharacters(text))` pretty much can just be read as `System.out.prinln(4)`. – Lord Farquaad Aug 16 '16 at 18:31
  • Ok I understood that part, what about the other way around, when the method receives the string, this `(String text)` text word can be changed to anything and it will still receive the same string, so this means the method is listening for whatever string comes first right ? – Liky Aug 16 '16 at 18:53
0

For counting the string use below code...

public class Main {
    static int stringCount;
public static void main(String []args){
Scanner reader = new Scanner(System.in);
System.out.print("Enter word: ");
String text = reader.nextLine();
calculateCharacters(text);
System.out.println(stringCount);
reader.close();
}
public static int calculateCharacters(String text){
    stringCount = text.length();
    return stringCount;

}
}
Ashwani
  • 1
  • 4