1

I have to create a simple program that will count the characters in text that you will paste into the cmd. The problem is, that I can't find a way for it to work properly. In my mind it should work somehow like

"please enter your text here" then you put the text there "the character count is: xyz"

  • 4
    https://stackoverflow.com/a/7106086/8089674 should give you some insight. Use this code to extract the copied data in string format, and use `data.length()` – Robo Mop Sep 14 '20 at 06:36

2 Answers2

5

Giving you an idea you can try the code below

Scanner myObj = new Scanner(System.in);  // Create a Scanner object
System.out.println("Please enter your text!");

String text = myObj.nextLine();  // Read user input
System.out.println("the character count is: " + text.Length());  //This will also count spaces
//If you don't want to included white spaces
String noSpaces = text.replace(" ", "");
System.out.println("the character count is: " + noSpaces.Length());

Getting multiple userInput with conditions

Scanner myObj = new Scanner(System.in);  // Create a Scanner object
System.out.println("Please enter your text!");
System.out.println("1=UPPERCASE, 2-Count,3=lowercase,4=remove spaces!");

while(myObj.hasNextLine()){

    String text = myObj.nextLine(); // Read user input

    String text1 = myObj.nextLine();
               switch (text1) {
                   case "1":
                       //your code here
                       break;
                   case "2":
                       //your code here
                       break;
                   case "3":
                       //your code here
                       break;
                   case "4":
                       //your code here
                       break;
                   default:
                       break;
               }

 }
Mikheil Zhghenti
  • 734
  • 8
  • 28
Cyrille Con Morales
  • 918
  • 1
  • 6
  • 21
  • Oh yeah, I did that and it worked perfectly but it doesn't seem to work when I use it with "if" –  Sep 14 '20 at 07:10
  • what condition you want to achieve with your `if`? – Cyrille Con Morales Sep 14 '20 at 07:12
  • the user is supposed to choose if he either wants to count the characters, put the whole text in upper case or anything else. i wanted to put each option in if, because after starting the program he can choose what he wants to do and then paste in the text –  Sep 14 '20 at 07:18
  • Ok, nevermind, I figured it out. The user will be asked to paste in the text and then will he be presented with the options to edit it and therefore - the if. –  Sep 14 '20 at 07:29
1
  1. The idea is simple, take console input and count the number of characters in it.

  2. If you not need the space between the text to get counted replace it with .replace prdefined string function or iterate over the string and update counter variable accordingly. Check the code below for refrence.
    public static int counter=0

      public static int countCharacter(String s)
      {
        int l=s.length;
        for(int i=0;i<l;i++)
        {
          if(s.charAt(i)!=" ")
             {
               counter++;
             }
         }
         return counter;  
       }