Excel Sheet Column Number

Source: Internet
Author: User

Package Cn.edu.xidian.sselab;
/**
* Title:excel Sheet Column number
* Content:
* related to question Excel Sheet Column Title
* Given A column title as appear in an Excel sheet, return the its corresponding column number.
* For example:

* A-1
* B-2
* C-3
 *  ...
*-Z
* AA
* AB
 */
public class Titletonumber {

This is a failed case, because the way to solve the problem with the switch cases, the result of a very large amount of code
Actually want to know a corresponds to 1,b corresponding to 2 such result, completely can use the obtained character minus a plus 1 can get the value
public int titletonumbers (String s) {
int len = S.length ();
int result = 0;
int position = 0;
for (int i=0;i<len;i++) {
Char temp = S.charat (i);
Position = position (temp);
Result + = Math.pow (len-i-1) * position;
}
return result;
}
public int position (char temp) {
int position = 0;
Switch (temp) {
Case ' A ': position = 1;break;
Case ' B ': position = 2;break;
Case ' C ': position = 3;break;
Case ' D ': position = 4;break;
Case ' E ': position = 5;break;
Case ' F ': position = 6;break;
Case ' G ': position = 7;break;
Case ' H ': position = 8;break;
Case ' I ': position = 9;break;
Case ' J ': position = 10;break;
Case ' K ': position = 11;break;
Case ' L ': position = 12;break;
Case ' M ': position = 13;break;
Case ' N ': position = 14;break;
Case ' O ': position = 15;break;
Case ' P ': position = 16;break;
Case ' Q ': position = 17;break;
Case ' R ': position = 18;break;
Case ' S ': position = 19;break;
Case ' T ': position = 20;break;
Case ' U ': position = 21;break;
Case ' V ': position = 22;break;
Case ' W ': position = 23;break;
Case ' X ': position = 24;break;
Case ' Y ': position = 25;break;
Case ' Z ': position = 26;break;
}
return position;
}

The correct solution:
This eliminates the hassle of the switch case to solve the corresponding relationship code, very concise
This can actually convert the title into a 26-character conversion process, so be careful not to mistake it.
public int Titletonumber (String s) {
int len = S.length ();
int result = 0;
for (int i=0;i<len;i++) {
Char temp = S.charat (i);
Result + = Math.pow (len-i-1) * (temp-' A ' + 1);
}
return result;
}

}

Excel Sheet Column Number

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.