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.



      Have you ever gone shopping and



The difference between next and nextLine

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

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. cannot get a string with spaces.

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(; 

//      String str=sc.nextLine();

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(; 

        StringBuilder str = new StringBuilder();  


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

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

            if((s >= 'a' && s <= 'z' || s >= 'A' && s <= 'Z') && a[s] == 3){  


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++)

        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.

                    char c=(char)(temp+'A');
                    return c;

        return 0;
    public static void main(String []args)
        Scanner scan=new Scanner(;

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

        char c=FindFirstTwoChar(str);

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