[Java] Character judgment / character string formatting (AOJ11 --character count)

Determine if the alphabet is lowercase

//A code that is true if c is a lowercase letter
public class Main {
    public static void main(String[] args){
    char c = 'c';
    
    System.out.printf("The character c you want to check is?  '%s'\n", c);
    /*True if c is a lowercase letter*/
    if (c >= 'a' && c <= 'z')
        System.out.println("c is lowercase");
    /*True if c is lowercase*/
    if (c >= 'a' && c <= 'z')        
        System.out.println("c is lowercase");
    else
        System.out.println("c is not lowercase");
    /*True if c is a number*/
    if (c >= '0' && c <= '9')       
        System.out.println("c is a number");
    else
        System.out.println("c is not a number");
    /*c is'+'Or'-'Then true*/
    if (c == '+' || c == '-')     
        System.out.println("c is the sign");
    else
        System.out.println("c is not a sign");
    }
}

The character c you want to check is?  'c'
c is lowercase
c is lowercase
c is not a number
c is not a sign

Repeated judgment with hasNext function of Scanner class

Scanner scanner = new Scanner(System.in);
while (scanner.hasNext()) {
    String str = scanner.next();
    System.out.println(str);
}

String formatting

import java.util.*;
import java.time.LocalDateTime;
import java.util.Locale;

public class Main {
 public static void main(String[] args) {
    System.out.println(String.format("%s weight is an apple%It is for d pieces.","Arukuma",70)); 
    System.out.println(String.format("%1$s weight is an apple%3$It is for d pieces.%1$s is fine.","Arukuma","Kitty",70)); //Arukumaの体重はりんご70個分です。Arukumaは元気です。
    //Specify the minimum width of the character string
    System.out.println(String.format("%.It's 2s.", "Arukuma"));       //This is Al.
    //Specify the width of the character string
    System.out.println(String.format("%It's 8s.", "Arukuma"));        //    Arukumaです。
    //Specify maximum number of digits
    System.out.println(String.format("Decimal number%08d", 12345));          //Decimal number00012345
    //Hexadecimal
    System.out.println(String.format("Hexadecimal%#x", 10));              //Hexadecimal0xa
    //Specify the number of digits after the decimal point
    System.out.println(String.format("Decimal point%.2f", 123.456));      //Decimal point123.46
    //index
    System.out.println(String.format("Exponent / lowercase%.2e", 123.456));  //Exponent / lowercase1.23e+02
    //Date and time
    var d = LocalDateTime.now();
    System.out.println(String.format("%tr", d));                      //03:42:23 AM
    System.out.println(String.format("%1$tY year%1$tm month%1$td day", d));  //October 31, 2020
    //Display with printf method
    System.out.printf("%.It's 2s.\n", "Arukuma");  
  }
}

Character count (ITP1-8)

Create a program that counts the number of each alphabet contained in the given English sentence. Note that lowercase and uppercase letters are not distinguished. Input You will be given one English sentence that spans multiple lines. Output Please output the number of each alphabet contained in the given English sentence in the format shown below: a: Number of a b: Number of b c: Number of c . . z: Number of z Constraints


import java.util.Scanner;

public class Main {
    public static void main(String[] args){
        Scanner scan=new Scanner(System.in);
        int alf[]=new int[26];
        while(scan.hasNext()){
            String str=scan.next().toLowerCase();
            for(int i=0;i<str.length();i++){
                char c=str.charAt(i);
                if('a'<=c&&c<='z'){
                    alf[str.charAt(i)-'a']++;
                }
            }
        }
        for(int i=0;i<26;i++)System.out.println((char)('a'+i)+" : "+alf[i]);
        scan.close();
    }
}

Recommended Posts

[Java] Character judgment / character string formatting (AOJ11 --character count)
Java string
[Java] Divide a character string by a specified character
[Java] Comparison of String type character strings
[Java] String padding
Java character code
Split string (Java)
Search for character string B from character string A (duplicate count)
[Java] How to cut out a character string character by character
[Java] How to erase a specific character from a character string
[Java] Handling of character strings (String class and StringBuilder class)
Character duplicate count (substring)
Java string multiple replacement
[Java] Holiday judgment sample
[Note] Java: String search
[Note] Java: String survey
Memorandum No.4 "Get a character string and decorate it" [Java]
[Java] How to convert a character string from String type to byte type
[Java] How to use substring to cut out a character string
[Java] Character acquisition / boolean type usage (AOJ⑤ Finding missing cards)
[Introduction to Java] Handling of character strings (String class, StringBuilder class)