Strings


#1

<PLEASE USE THE FOLLOWING TEMPLATE TO HELP YOU CREATE A GREAT POST!>

Input was Mostwanted50

And need output as

Digits-2
Alpha-10
<Below this line, add a link to the EXACT exercise that you are stuck at.>

public class Main{
  public static void main(String args[]){
  String str="Mostwanted50";
  String p="Digits=";
  String q="Alphabets=";
  String r="";
  String u="";
  int len=str.length();
  int i,x=0,y=0;
  for(i=0;i<len;i++){
    char ch=str.charAt(i);//getting Character from string
    
  if((ch>='A' && ch<='Z')||( ch>='a' && ch<='z'));
  {
    x=x+1;
  }
  if(ch>='1' && ch<='0'){
    
    y=y+1;
  }  

  
  }
  
  r=q+x;
  u=p+y;
  System.out.println(r);
  System.out.println(u);
  
  }
}

<In what way does your code behave incorrectly? Include ALL error messages.>


#2

use isdigit:

https://www.tutorialspoint.com/java/character_isdigit.htm

comparing numbers with each other while the numbers are of type string, can cause a lot of unexpected behaviour to your code.


#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.