简体   繁体   中英

How can i use an int instead of using Integer?

for(int i = 0; i<N; i++){
    boolean pen15 = false;
    String s = in.next();

    int cur = Integer.valueOf(s.charAt(0));
    int k = -1;
    if(s.length()==1){
        System.out.println("AD "+s);
        pen15 = true;
    }
}

How can I cast an int instead of using Integer ? I tried using
int cur = (int).valueOf(s.charAt(0)); , but it doesn't even compile.

I am not sure what you really want. An integer value for the character? Or its decimal number in ASCII table?

String s = "12345";
int cur = Integer.valueOf(s.charAt(0)); //49
cur = (int)s.charAt(0); //49
cur = Character.getNumericValue(s.charAt(0)); //1
cur = Integer.parseInt(String.valueOf(s.charAt(0))); //1
cur = Character.digit(s.charAt(0), 10); //1
cur = s.charAt(0) - '0'; //1

You can directly use the code like this:

int cur = s.charAt(0);

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