Skip to content
Advertisement

Removing the integers in the output when inputted with a String [closed]

I am struggling on my code. My prof told us to make a line of code in Eclipse which, when a string like a “q1w2e3r4t5y” is input, will output only “qwerty”. The numbers are removed. This can be easily achieved using a regular expression. My problem is, it is my first year in coding and I’m not yet familiar with the code using Java.

The code using regex is

String inputString = "q1w2e3r4t5y";

System.out.println(inputString.replaceAll("\d",""));

And the console/output is

qwerty

Please tell me what code is the best way to execute this output without using the regex.

Advertisement

Answer

  public static void main(String[] args) {
        String inputString = "q1w2e3r4t5y";
        String newS ="";
        for (int i = 0; i < inputString.length(); i++) {
            String s = inputString.charAt(i)+"";
            try { 
                Integer.parseInt(s);            
            
            } catch (Exception e) {
                //TODO: build the string here
                newS+=s;              
            }       
           
        }

        System.out.println(newS);//qwerty

    
      }
}
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement