Finding all uppercase letters of a string in java

前端 未结 10 1114
后悔当初
后悔当初 2021-01-12 14:28

So I\'m trying to find all the uppercase letters in a string put in by the user but I keep getting this runtime error:

Exception in thread \"main\" java.lan         


        
10条回答
  •  长发绾君心
    2021-01-12 15:01

    You can increase the readability of your code and benefit from some other features of modern Java here. Please use the Stream approach for solving this problem. Also, I suggest importing the least number of libraries into your class. Please avoid using .* while importing.

    import java.util.Scanner;
    
    public class P43 {
        public static void main(String[] args) {
            Scanner in = new Scanner(System.in);
            System.out.print("Please give a string: ");
            String x = in.next();
            x.chars().filter(c -> Character.isUpperCase(c))
                    .forEach(c -> System.out.print((char) c + " "));
        }
    }
    

    Sample input:

    saveChangesInTheEditor

    Sample output:

    C I T E

提交回复
热议问题