-1

I have a homework assignment in where my teacher has left us the following instructions: Create a new method with String input and String return. Check the string to see if there are any uppercase letter, put together all uppercase together and return this string: Your method header and for-loop provided: No power tools, use ASCII table Your sample output should be as followed: getUpper(“Hello, World”) returns “HW” getUpper(“no way”) returns “”

This is the initial code provided:

public String getUpper(String s) {
    String output = "";
    for (int i = 0; i < s.length(); i++) {
        //i is the index of the string
        //Your work here
    }

    return output;
}

I have managed to create a solution, however this solution is one that is hardcoded as seen below:

public class homework5 {
    public homework5() {
    }
    public String getUpper(String s) {
        String output = "";
        for (int i = 0; i < s.length(); i++) {
            //i is the index of the string
            char c1 = s.charAt(i);
            int a1 = (int)c1;        
            if (a1 < 97) {
                output += c1; // This segment of code works, but is hardcoded. Find a generalized value for it.
            }
        }
        return output;
    }

    public static void main(String args[]) {
        homework5 obj1 = new homework5();
            //method call
        System.out.println(obj1.getUpper("Hello"));
    }
}

How would I go about this problem to find a general way to find a character, checks its ascii value, and see if it is uppercase, and then print it out?

swpalmer
  • 3,890
  • 2
  • 23
  • 31
Stablinz
  • 53
  • 5
  • Your teacher should be scolded for encouraging string concatenation in a loop. Use StringBuilder. They probably just didn't want to introduce the StringBuilder class this early - but they better follow up with "by the way, never do this". – swpalmer Oct 02 '22 at 15:38
  • @swpalmer I already deleted my comment to avoid falling into a silly internet debate. – Basil Bourque Oct 02 '22 at 15:58
  • Poor title. Rewrite to summarize your specific technical issue. – Basil Bourque Oct 02 '22 at 15:58

2 Answers2

0

You can just compare with 'A' and 'Z' (since the uppercase letters are consecutive in ASCII).

char c1 = s.charAt(i);
if (c1 >= 'A' && c1 <= 'Z') output += c1;

Character.isUpperCase is more general and works for Unicode, not just ASCII.

if (Character.isUpperCase(c1)) output += c1;
Unmitigated
  • 76,500
  • 11
  • 62
  • 80
0

You can convert your string to a char array and check if the array`s element at position i is uppercase with method isUpperCase of class Character.

public class homework5{
    public homework5(){
    }
    public String getUpper(String s){
        String output = "";
        for(int i = 0; i < s.length(); i++){
            //i is the index of the string
            if (Character.isUpperCase(s.toCharArray()[i])) {
                output += output + s.toCharArray()[i];
             }
            
         }
        return output;
     }
    public static void main(String args[]){
        homework5 obj1 = new homework5();
            //method call
        System.out.println(obj1.getUpper("Hello"));
    }
}
Marius
  • 55
  • 1
  • 8