1

I am trying a code problem to convert double to string and then insert that to an array. I tried various methods but these don't give expected output.

public int[] makePi() {
  double PI = Math.PI;

  String sPI = String.valueOf(PI);
  int[] Arr = new int[3];

  for(int i =0; i<3; i++)
  {
    Arr[i] = sPI.charAt(i);
  }

  return Arr; 
}

Output should be an array with first three characters of PI as below :-

[ 3, 1, 4 ] while I am getting [51, 46, 49]

I will handle decimal character if needed.

Just a hint is needed.

Please don't provide full program that will be a spoiler. :-)

4
  • What does your method output? Commented Apr 25, 2016 at 7:03
  • u need string array or int array? Commented Apr 25, 2016 at 7:04
  • @RamanShrivastava any will do.. Commented Apr 25, 2016 at 7:07
  • 1
    Character.isDigit and Character.getNumericValue will help you here. Commented Apr 25, 2016 at 7:33

4 Answers 4

2

Look at the ASCII table. Do you see what are the corresponding chars for the integers you're getting? This should be a good hint for you.

Note that you're assigning the result to an int array, while you're running on characters.

Sign up to request clarification or add additional context in comments.

7 Comments

I think this question(stupidity) doesn't meet the standard of SO.
@RanchiRhino I hope there's no spoiler.
@RanchiRhino It does. You described your problem and provided a reproducible code.
Not ASCII, Unicode/UTF-16 (numbers are hexadecimal and are in rows of 16.) From the question, 51 would be 0x33, which in UTF-16 is the single UTF-16 code unit needed for the Unicode codepoint U+0033. [I think it's better to learn that you know a little about Unicode than to think you need to know ASCII and that it is all you need to know.]
@TomBlodget I'm actually very familiar with unicode/UTF-16. I even wrote a blogpost a while ago.
|
2

you're storing chars into an int array. hence theie respective ascii values will be stored in array (you're effectively converting char to int)

3 (char) -> 51 (ASCII Value)

. (char) -> 46 (ASCII Value)

1 (char) -> 49 (ASCII Value)

your array length is 3, so only first 3 chars are converted to ascii which is 3.1, not 3.14

But now if you want to store it into an char array (which i feel you're trying to do), all you need is -

char[] charArray = sPI.toCharArray();

Plus, I dont think you want to store in int array as though you can convert ascii values int their respective int value, but what about '.' which is not a valid int.

Comments

1

What you get in your array are values of characters (so something like 70 for '3', I neither remember nor want to remember exact values). You must convert value of character into the number itself. Hint: characters are numbered in the following way:

'0' - n

'1' - n + 1

'2' - n + 2

and so on.

Comments

1

If you want to extract the numeric values of the digits, I would advise against doing explicit comparisons and arithmetic on the character values.

The Character class provides helper methods, which are less error-prone and more readable:

int outIndex = 0;
for (int i = 0; i < 3 /* && i < sPI.length() */; ++i) {
  char c = sPI.charAt(i);
  if (Character.isDigit(c)) {
    Arr[outIndex++] = Character.getNumericValue(c);
  }
}
/* assert outIndex == 3 */
return Arr;

I've commented out some code which I'd put in there for more robustness - it's not strictly necessary in this case, since we know that sPI has at least 3 digits in it. (Mind you, if we're going to hard-code that assumption, we may as well simply return new int[] { 3, 1, 4 };).

1 Comment

In that case (hard-coding), I should round the double to two decimal places.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.