Java case-sensitive alphanumeric and symbolic

Source: Internet
Author: User

How does Java differentiate between the four kinds of things that title? The first thing I think of is the use of ASCII code, because the different symbols of the ASCII code is not the same. If you want to use ASCII code to differentiate, at least know the ASCII code of a,z,a,z,0,9, so you can use ASCII code to distinguish.

This is the first method.

1  Packagetest;2 ImportJava.util.Scanner;3  Public classdifferentiate{4      Public Static voidMain (string[] args) {5         //a-65,z-90,a-97,z-122,0-48,9-576Scanner sc =NewScanner (system.in);7String nextline =sc.nextline ();8         Char[] Chararray =Nextline.tochararray ();9          for(inti = 0;i<chararray.length;i++){Ten             intCharascii = (int) chararray[i]; One             if(Charascii >=65 && charascii <=90){ ASystem.out.print ("Capital Letter:"); - System.out.println (Chararray[i]); -}Else if(Charascii >=97 && charascii <=122){ theSystem.out.print ("Lowercase letters:"); - System.out.println (Chararray[i]); -}Else if(Charascii >= && charascii <= 57){ -System.out.print ("Number:"); + System.out.println (Chararray[i]); -}Else{ +System.out.print ("Symbol:"); A System.out.println (Chararray[i]); at             } -         } -     } -}

The second method, on-line search data found, seemingly in Java on the a-z,a-z,0-9 char characters are sorted, so you can directly compare.

1  Packagetest;2 3 ImportJava.util.Scanner;4 5  Public classdifferentiate{6      Public Static voidMain (string[] args) {7Scanner sc =NewScanner (system.in);8String nextline =sc.nextline ();9          for(inti = 0; I < nextline.length (); i++) {Ten             Charc =Nextline.charat (i); One             if(c >= ' A ' && C <= ' Z ') { ASystem.out.print ("Max"); - System.out.println (c); -}Else if(c >= ' a ' && c <= ' z ') { theSystem.out.print ("Min"); - System.out.println (c); -}Else if(c >= ' 0 ' && C <= ' 9 ') { -System.out.print ("number"); + System.out.println (c); -}Else { +System.out.print ("symbol"); A System.out.println (c); at             } -         } -     } -}

There is also a way to use the indexof () function, if a character does not exist in the string, it will return-1, so only need to make a-z,a-z,0-9 three strings, and then each time with the three string to determine the line

1  Packagetest;2 3 ImportJava.util.Scanner;4 5  Public classdifferentiate{
6 Public Static voidMain (string[] args) {7Scanner sc =NewScanner (system.in);8String nextline =sc.nextline ();9String upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";TenString lower = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; OneString num = "0123456789"; A Char[] Chararray =Nextline.tochararray (); - for(inti = 0;i<chararray.length;i++){ - if(Upper.indexof (chararray[i])! =-1){ theSystem.out.print ("Capital Letter:"); - System.out.println (Chararray[i]); -}Else if(Lower.indexof (chararray[i])! =-1){ -System.out.print ("Lowercase letters:"); + System.out.println (Chararray[i]); -}Else if(Num.indexof (chararray[i])! =-1){ +System.out.print ("Number:"); A System.out.println (Chararray[i]); at}Else{ -System.out.print ("Symbol:"); - System.out.println (Chararray[i]); - } - } - } in}

Java case-sensitive alphanumeric and symbolic

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.