0

I am trying to write a program that takes a user's input and outputs the number of characters they typed in. I have to do this by creating a method that calculates the amount of characters, then call that method in main to output the results. I was encouraged to use a for loop, but I don't see how that would work. I can calculate the number of characters using length(), but I can't figure out how to make my method work. This is what I have so far:

public static void main(String[] args) {
 Scanner scnr = new Scanner(System.in);

 String userInput = "";

 System.out.println("Enter a sentence: ");
 System.out.print("You entered: ");

 userInput = scnr.nextLine();
 System.out.println(userInput);


  return;
 }

 public static int GetNumOfCharacters(int userCount) {

  int i = 0;
  String userInput = "";
  userCount = userInput.length();


return userCount;
 }
}

My method is not returning the length of the string, it just gives me 0 or an error.

J Learning
  • 53
  • 1
  • 2
  • 8
  • Your `GetNumOfCharacters()` method doesn't make any sense. You need to pass the string as a parameter and return its length as the result. Passing an integer as a parameter and returning the length of an empty string defined int side the method doesn't begin to make sense. You also need to actually *call* this method somewhere. – user207421 Sep 13 '16 at 02:00
  • I'm sorry, I've only started learning Java this week. The program that is checking my work is saying that my method is failing the Unit Test. I have tried all of the answers on here, but apparently the method is still not returning the length of the string. – J Learning Sep 13 '16 at 02:03
  • No need to apologize: just learn from it. But I fail to see how calling `String.length()` constitutes using a `for` loop, or requires a wrapper method around it. – user207421 Sep 13 '16 at 03:48

5 Answers5

1

Right now, you are never calling your "GetNumOfCharacters" method in your main. The way Java programs work, is by calling the main method and executing line per line what lies there. So you need to call you method from inside the main method. On the other hand, it should get the Stirng as a parameter, so you can get its length. It would look something like this:

public static void main(String[] args) {
    Scanner scnr = new Scanner(System.in);

    String userInput = "";

    System.out.println("Enter a sentence: ");


    userInput = scnr.nextLine();
    System.out.print("You entered: ");
    System.out.println(userInput);

    int lenInput = GetNumOfCharacters(userInput);

    System.out.println("The length was: "+lenInput+" characters");
}

public static int GetNumOfCharacters(String userInput) {
    int len = userInput.length();
    return len;
}
0

Problems with your code:

  • No Function call

    Add function call in main() as int count=GetNumOfCharacters(userInput);

  • Parameter datatype mismatch

    change the datatype in function definition from int to String as public static int GetNumOfCharacters(String userInput) {

  • Unwanted return statement in main()

    remove the return from main()

  • Not displaying the value returned from GetNumOfCharacters

    Add System.out.print("Number of characters: "+ count); inside main()

Code:

   public static void main(String[] args) {
   Scanner scnr = new Scanner(System.in);

   String userInput = "";

   System.out.println("Enter a sentence: ");
   System.out.print("You entered: ");

   userInput = scnr.nextLine();
   System.out.println(userInput);

   int count=GetNumOfCharacters(userInput);
   System.out.print("Number of characters: "+ count);
   }

   public static int GetNumOfCharacters(String userInput) {
   int userCount = userInput.length();
   return userCount;
   }

OR

Function is not really needed,you can remove the function and do it like this:

public static void main(String[] args) {
 Scanner scnr = new Scanner(System.in);

 String userInput = "";

 System.out.println("Enter a sentence: ");
 System.out.print("You entered: ");

 userInput = scnr.nextLine();
 System.out.println(userInput);

 System.out.print("Number of characters: "+ userInput.length());
 }
Jibin Balachandran
  • 3,381
  • 1
  • 24
  • 38
  • Is there a reason my unit test would be failing? I have tried your suggestion for the GetNumOfCharacters method, but the test says it is not returning the number of characters in the string. – J Learning Sep 13 '16 at 02:23
  • I am doing this project for a class, we are learning about methods so I think the point was to call the method. It produces the right result, but the unit test is still failing. – J Learning Sep 13 '16 at 02:37
0

A problem is that you are not actually calling the method

so try

public static void main(String[] args) {

 Scanner scnr = new Scanner(System.in);

 System.out.println("Enter a sentence: ");
 String userInput = scnr.nextLine();
 System.out.print("You entered: ");
 System.out.println(userInput);

 System.out.println ("The length is " + GetNumOfCharacters (userInput))    


 }

 // need to pass string into this method
 public static int GetNumOfCharacters(String myString) {

  int userCount = myString.length();
  return userCount;
 }
}
Scary Wombat
  • 44,617
  • 6
  • 35
  • 64
0

Your question included the line:

I was encouraged to use a for loop, but I don't see how that would work.

There's no elegant way to do this in Java because you are assumed to use String.length() to get the length of strings. There is no 'end of string' marker as there is in, say, C. However you could mimic the same effect by catching the exception thrown when you access past the end of the string:

for (int len = 0; ; len++) {
    try {
        text.charAt(len);
    } catch (StringIndexOutOfBoundsException ex) {
        return len;
    }
}

That's not a nice, efficient or useful piece of code but it does demonstrate how to get the length of a string using a for loop.

sprinter
  • 27,148
  • 6
  • 47
  • 78
0

If you don't want to use predefined methods, you can do like this..

 public static void main(String args[]){
 Scanner scnr = new Scanner(System.in);


 System.out.println("Enter a sentence: ");
 String userInput = scnr.nextLine();

 System.out.println("You entered: "+userInput);

char a[]=userInput.toCharArray();
 int count=0;
for(char c : a){
  count++;
}
System.out.println("length of the string is:"+count);

}
Sai
  • 13
  • 4