0

I am looking for a help to my code in java as I wanted to print only the uppercase of ASCII. So I made an array and run a for loop. I made an if condition to access the characters from 65 to 90, then I used this format to print the value system.

out.printf("%c : ",ch[x]).

Unfortunately I got garbage as output. here is the complete code:

           for(int x=0; x<ch.length; x++) {
            if(ch[x]>=65||ch[x]<=90) {
               System.out.printf("%c ",ch[x]);
            }
          }
7
  • Can you post your complete code Commented Dec 10, 2017 at 16:59
  • 1
    Tag the question with java, research string + java + toupper, post your code to help us fix yours. Commented Dec 10, 2017 at 18:30
  • Please read minimal reproducible example and enhance your question accordingly. Commented Dec 10, 2017 at 18:57
  • 1
    Use && instead of || Commented Dec 10, 2017 at 18:57
  • 1
    @Baba, char[] ch = {'A','B','C','D','a','b'}; for(int x=0; x<ch.length; x++) { if(ch[x]>=65&&ch[x]<=90){System.out.printf("%c ",ch[x]);}} output: A B C D Commented Dec 10, 2017 at 19:11

2 Answers 2

1

As @YCF_L said, use &&, and may, I recommend the forEach :

char[] ch = new char[]{'A', 'z', 'S', 'Z', 'H', '1', 'o'};
for (char c : ch) {

    if (c >= 65 && c <= 90) {
        System.out.printf("%c ", c);
    }
}

Outputs

A S Z H 

ideone demo

Sign up to request clarification or add additional context in comments.

Comments

0

Simply use isUpperCase() method:

for(int x=0; x<ch.length; x++) {
        if(isUpperCase(ch[x])) {
           System.out.printf("%c ",ch[x]);
        }
      }

If you don't want to use the method:

for(int x=0; x<ch.length; x++) {
        if(ch[x]>=65 && ch[x]<=90) {
           System.out.printf("%c ",ch[x]);
        }
      }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.