简体   繁体   中英

how to input letters and numbers using joptionpane

I want to input something like: number23 using JOptionPane , but all I know how to do is either input a number or input a string.
How do I input word + number?

What I have so far:

import javax.swing.JOptionPane;

public class morePractice {

    public static void main(String[] args) {
        String str;
        int num1;
        int total=0;
        char letter;

        str=JOptionPane.showInputDialog(null,"Enter phrase");

        for(int i=0;i<str.length();i++){
            letter=str.charAt(i);
            if(letter>='A'&&letter<='Z'){
                total++;
            }

        }
        JOptionPane.showMessageDialog(null,"There are " + total + " upper case letters");

    }

}

Use Regular Expression
Try this approach

String s = "Number23";
System.out.print(s.replaceAll("[^0-9]","")); 

it gives you just a number

or s.replaceAll("[^az]","") gives you just characters

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM