Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Excel Column Letter Referencing

  1. #1
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,608
    Thanks
    24
    Thanked 316 Times in 295 Posts

    Default Excel Column Letter Referencing

    I'm trying to fix a method I made to reference Excel Column Letters based on the given number >1 where 1 = A.

    Here is the method I have so far:
    public String getLetter(int n)
        {
        	String[] letters = new String[] {"","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
     
     
        	String ans = "";
        	while(n>0)
        	{
        		ans = letters[n%27]+ans;
        		n = n/27;
        		System.out.println(n);
        	}
        	return ans;
        }

    Currently, this works great for numbers 1 - 26, but starting at 27 it craps out on me.

    For 27, it returns A, where it should return AA. For 28, it returns AA, where it should return AB. And it continues offset like that.

    There is definately something I'm missing here, can someone help me out?


  2. #2
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,235
    Thanks
    176
    Thanked 817 Times in 760 Posts
    Blog Entries
    5

    Default Re: Excel Column Letter Referencing

    Any reason why you have your array starting at 1 (eg letters[0] = "")? This is going to effect how your higher indexes are evaluated. I'd suggest removing the empty string and going from there (you'll have to change your while loop a bit to take into account the new indexes).

  3. #3
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,608
    Thanks
    24
    Thanked 316 Times in 295 Posts

    Default Re: Excel Column Letter Referencing

    2 reasons.

    1) My values received start at 1, so it only seemed to make sense to also start at 1.
    2) I couldnt figure out how to change the loop to tell it to stop since I'm using Integer Division and I would need to get a 0 value.
    Last edited by aussiemcgr; July 21st, 2010 at 03:13 PM.

  4. #4
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,235
    Thanks
    176
    Thanked 817 Times in 760 Posts
    Blog Entries
    5

    Default Re: Excel Column Letter Referencing

    Quote Originally Posted by aussiemcgr View Post
    2 reasons.

    1) My values received start at 1, so it only seemed to make sense to also start at 1.
    2) I couldnt figure out how to change the loop to tell it to stop since I'm using Integer Division and I would need to get a 0 value.
    I just mention the zero index because things can get quite messy with higher values. Easier for me to write the code than explain a fix, so have a look at the following...if the parameter you have is 1 based and not zero based, just send the function n-1.

    	public String getLetter(int n)
        {
    		String[] letters = new String[] {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
     
            String ans = "";
            while(n>-1)
            {	
            	ans = letters[n%26]+ans;
                n = n/26 - 1;
            }
            return ans;
        }

Similar Threads

  1. Highlighting both row and column in JTable
    By bschneider14 in forum AWT / Java Swing
    Replies: 4
    Last Post: May 29th, 2010, 09:14 AM
  2. Random Letter problem please help!
    By xs4rdx in forum Java Theory & Questions
    Replies: 1
    Last Post: March 28th, 2010, 11:27 AM
  3. Sentence and Letter Count Program
    By velop in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 9th, 2010, 11:10 PM
  4. help with the logic on this letter grade program.
    By etidd in forum Loops & Control Statements
    Replies: 2
    Last Post: January 28th, 2010, 08:14 PM
  5. letter to number
    By silverspoon34 in forum Java Theory & Questions
    Replies: 1
    Last Post: November 27th, 2009, 06:01 AM