To calculate the number of elements in a conditional error & array

Asked 2 years ago, Updated 2 years ago, 27 views

public class Testtest{

  public static void main(String[] args){
    String einString = Terminal.askString("String: ");
    String[] array_word = einString.split("");
    char[] arrayChar = einString.toCharArray();
    char[] count = new char[arrayChar.length];

    **for(int i=0; i<array_word.length; i++){
      if((byte)arrayChar[i]>64&&(byte)arrayChar[i]<91){
        count[i] = arrayChar[i];
        if((byte)arrayChar[i]>96&&(byte)arrayChar[i]<123){
          for(int j=0; j<count.length-1; j++){
            count[j] = count[j+1];
          }
          break;
        }
      }
    }**
    **for(int i=0; i<array_word.length; i++){
      if((byte)arrayChar[i]>96&&(byte)arrayChar[i]<123){
        for(int j=0; j<count.length-1; j++){
          count[j] = count[j+1];
        }
        break;
      }
      if((byte)arrayChar[i]>64&&(byte)arrayChar[i]<91){
        count[i] = arrayChar[i];
      }
    }**
    System.out.println(count);
    int b = count.length;
    System.out.println(b);
  }
}

For example, when you enter ABCDefgHIJklmnOPQRstuWXYz, you want to extract only the number of uppercase alphabets. But now I made the code by changing the order of the up and down if in the ** for conditional statement. Then, the first letter is cut off and the result is printed out. If you put ABCDefgHIJklmnOPQRstuWXYz, you'll get BCDHIJOPQRWXY If you insert BCDefgHIJklmnOPQRstuWXYz, CDHIJOPQRWXY.

And to get the number of alphabets in the last line, count.I wrote length, but the number of alphabets I entered is printed out instead of uppercase. I don't know where it went wrong. It seems simple, but I feel like the code is too complicated, but it's the limit of my ability Please give me some advice and corrections.

java

2022-09-22 08:30

1 Answers

The streams have been added since Java 8.

There is also an isUpperCase method in the Character class, which allows you to write code concisely.

jshell> String s = "ABCDefgHIJklmnOPQRstuWXYz"
s ==> "ABCDefgHIJklmnOPQRstuWXYz"

jshell> List<Character> chars = s.chars() // divide the string into each character and express it intstream.
    .filter(c -> Character.isUpperCase(c)) // Only uppercase characters are selected.
    .mapToObj(c -> (char)c) // transform int form to char.
    Convert .collect(Collectors.toList()) // stream to list.
chars ==> [A, B, C, D, H, I, J, O, P, Q, R, W, X, Y]

jshell> chars.size()
$34 ==> 14

jshell> for(char c: chars) System.out.println(c)
A
B
C
D
H
I
J
O
P
Q
R
W
X
Y
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class Test1 {
    public static void main(String args[]) {
        String s = "ABCDefgHIJklmnOPQRstuWXYz";

        List<Character> chars = s.chars()
                            .filter(c -> Character.isUpperCase(c))
                            .mapToObj(c -> (char)c).collect(Collectors.toList());
        System.out.println(chars.size());

        for(char c: chars) System.out.println(c);
    }
}


2022-09-22 08:30

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.