Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4013552
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T09:23:08+00:00 2026-05-20T09:23:08+00:00

So this probably would be easy if done like this public static [] nums(int

  • 0

So this probably would be easy if done like this

public static [] nums(int j)
{
    int num = 12345;
    int[]thenums = [5];

    for(int i=0; i<5; i++)
    {
       thenums[4-i] = num%10;
       num = num/10;
    }

return the nums
}

i get {1,2,3,4,5}

but for some reason if the number starts with a 0 then it does not work
how to make this work if the number were

int num = 02141;

thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T09:23:09+00:00Added an answer on May 20, 2026 at 9:23 am

    EDIT: Doh… I’d completely forgotten about octal literals. That explains why the value is showing up differently. (02141 is treated as an octal literal; it’s not the same value as 2141.)

    However, it sounds like the OP wants to “remember” the number of leading zeroes in a number. There’s no way of doing that as an integer, because it’s just remembering a value. What’s the difference between seeing “3” birds and seeing “0000003” birds?

    If you have a number representation where the leading zeroes are important, you’re not just talking about an integer quantity, which is all that an int represents.

    Where are you getting your input from? It sounds like you should just be maintaining it as a string from the start.

    If you always want 5 digits, that’s easy to do – and your current code should do it (when amended to actually compile) – something like this:

    public class Test
    {
        public static void main(String[] args) 
        {
            int[] digits = getDigits(123);
            for (int digit : digits)
            {
                System.out.print(digit); // Prints 00123
            }
        }
    
        public static int[] getDigits(int value)
        {
            int[] ret = new int[5];
            for (int i = 4; i >=0 ; i--)
            {
                ret[i] = value % 10;
                value = value / 10;
            }
            return ret;
        }
    }
    

    Now that’s hard-coded to return 5 digits. If you don’t know the number of digits at compile-time, but you will know it at execution time, you could pass it into the method:

    public static int[] getDigits(int value, int size)
    {
        int[] ret = new int[size];
        for (int i = size - 1; i >=0 ; i--)
        {
            ret[i] = value % 10;
            value = value / 10;
        }
        // Perhaps throw an exception here if value is not 0? That would indicate
        // we haven't captured the complete number
    
        return ret;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If this was PHP, I would probably do something like this: function no_more_half_widths($string){ $foo
OK, this sounds simple and or like something that should have probably been done
I am sure that this probably something extremely easy (or it can't be done),
I know this is probably a very simple question but how would I do
Warning: Note that this is a dumb question of something that I would probably
I'm new to network programming. This is probably a stupid question, would it be
Ok, I thought this would be simpler but I was probably wrong.. My parser
This probably sounds like a terrible idea at first glance, but here is my
This probably has probably been asked before, but I couldn't find anything relevant. Would
This is probably an easy question, but I want to understand better how Apache

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.