The first three characters in the statistics string

Title Description

The user enters an English string and writes a code to find the first English character in the string that appears three times.

Enter a description

The user enters a string in English.

Output description

Output the English character that appears three times first.

Example

Input:

      Have you ever gone shopping and

Output:

      e

The difference between next and nextLine

The user obtains the characters entered by the user through next and nextLine in the Scanner class.

next():
1. Be sure to read valid characters before ending the input.
2. The next() method will automatically remove the blanks encountered before entering valid characters.
3. Only after entering valid characters can the blank space after them be used as separator or end character.
4.next() cannot get a string with spaces.

nextLine():
1. With Enter as the end character, the nextLine() method returns all the characters before the carriage return.
2. You can get blank.

Effect demonstration of next and nextLine

java demo code:

import java.util.Scanner;

public class usenext {
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in); 

        String str=sc.next();
//      String str=sc.nextLine();
        System.out.println(str);
    }
}

The user enters the public static void main string, the next method will output the public result, and the nextLine method will output the public static void main.

java code

Source 1:

package experie;

import java.util.*;  


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

        StringBuilder str = new StringBuilder();  

        str.append(sc.nextLine());  

        char[] c = str.toString().trim().toCharArray();  

        int[] a = new int[127];  
        for(char s : c){  

            a[s]++;  
            if((s >= 'a' && s <= 'z' || s >= 'A' && s <= 'Z') && a[s] == 3){  
                System.out.print(s);  
                break;  
            }
        }

    }  
} 

Source 2:

package experie;


import java.util.*;
public class zifu1{

    public static char FindFirstTwoChar(String s)
    {
        char [] ch=s.toCharArray();
        int []data=new int[57];

        for(int i=0;i<data.length;i++)
        {
            data[i]=0;
        }


        for(int i=0;ch[i]!='\0';i++)
        {
            if((ch[i]>='a'&&ch[i]<='z')||(ch[i]>='A'&&ch[i]<='Z'))   //When the letters are lowercase or uppercase.
            {
                int temp=ch[i]-'A';  //This can be converted from char to int.
                data[temp]++;

                if(data[temp]>=3)
                {
                    char c=(char)(temp+'A');
                    return c;
                }
            }

        }
        return 0;
    }
    public static void main(String []args)
    {
        Scanner scan=new Scanner(System.in);

//        String str=scan.next();               
        String str=scan.nextLine();

        char c=FindFirstTwoChar(str);
        System.out.println(c);
    }
}

The above two source codes can realize the statistics of the first three characters.

Effect screenshot




Tags: Java

Posted on Sun, 10 May 2020 08:17:28 -0700 by t0ta11