Digital (number) length in Java [closed] - java

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
How to get the length of some number in Java?
Length of string is string.length(), but what is it for an integer?
I tried this:
int lengthNumber = (String.valueOf(maxNumber)).length();
But it always returns 1, for every number.

Try
Integer#toString().length();
For Example
Integer a=222;
int length=a.toString().length();
Output
3

When I ran this:
int Number = 100003;
int lengthNumber = (String.valueOf(Number)).length();
System.out.println(lengthNumber);
My output was 6, indicating that it works correctly.
Just make sure that your variables are declared properly.
This method will work if the above isn't working.
int x = 100003;
String y = "" + x;
Now you can use y.length(). Printing y gives 100003, and printing y.length() gives 6.
System.out.println(y);
System.out.println(y.length());

Related

Problem with adding strings to proper positions of an array (java) [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I am working to make an Instagram type project where you can follow up to five people. For my follow method I tried to loop through a string array (length 5) and say if 'user getting followed' isn't on the list of followers, add that person to the array. But when I do instead of displaying the array like this (when I call the Arrays.toString()):
follows: [user 1, user 2, user 3, user 4, user 5]
It displays the array like this:
follows: [user1user2user3user4user5, , , , ]
Here is my code I am talking about:
for (int i = 0; i < 5; i++) {
if(...) {
this.following[i] += gettingFollowed.getHandle();
/* getHandle() gets the name of the user getting followed and adds it to the string array, 'following' */
this.following[i] += gettingFollowed.getHandle();
The += instruction appends to a String - which is why you are seeing your output. You are appending all your users to the same index.
this.following[i] = gettingFollowed.getHandle();
Simply remove the '+' and you are now setting the value.
Still, without knowing what .getHandle() does - this might not completely fix your code.

python big integer multiplication doesn't calculate most significant digits [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I have a python program which should multiply 2 big integers.
The problem is I get a different result in java with BigInteger class with same input.
I have tried DecInt library for python but gives the same answer as using pure python.
Here are my variables:
d = 372049305848826709205673800090501485720867768816
r = 5452188953055713107393819158892374332916586527736541041226026450382
Result I get in python from d * r:
2028483115341019294875069650745272851135156323450218238187883716036516369477015140871224045070868977706272670887712
Result I get in java with BigInteger class:
9530687378863294988874153740700860249994095546182028483115341019294875069650745272851135156323450218238187883716036516369477015140871224045070868977706272670887712
Here is my java code:
BigInteger d = new BigInteger("372049305848826709205673800090501485720867768816");
BigInteger r = new BigInteger("5452188953055713107393819158892374332916586527736541041226026450382");
BigInteger tmp1 = d.multiply(r);
System.out.println(tmp1);
As you can see, there are some most significant digits that are missed in python's result.
Is there any solution for that?
Both Java and Python give the same answer:
2028483115341019294875069650745272851135156323450218238187883716036516369477015140871224045070868977706272670887712
The problem must be in your Java code then. Make sure you preform multiplication correctly and verify your input.
Also, check if there is anything that might print digits just before the result. The simplest way I can think of is:
System.out.println("the result is: " + int1.multiply(int2));
That will separate the output you are interested in from everything that is already printed.

Value assigning from text file variables [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
Got a problem and I don't know how to approach this one.
From the text file I get a line like this: "x = 45 + 3". (Variables are only single lowercase/uppercase letters). Output should be 48, like normal calculation.
Now I need to calculate that equation and use the 'x' in another equation like
"y = x + 15 - 7".
So far I've come up an idea to use substring to get the calculation part, i.e '45+3'. But I can't think any good idea how to keep variable and use it in another equation since next equation is a string form a text file also.
Any ideas are welcome!
use replace all x with 45 do the technique of arithmetic you had done before for 45+3
Hope my help works.
ScriptEngineManager factory = new ScriptEngineManager();
ScriptEngine engine = factory.getEngineByName("JavaScript");
engine.eval("x = 45 + 3");
engine.eval("y = x + 15 - 7");
double y = (Double)engine.get("y");
System.out.println(y);

Java Arrays Assistance [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I have the following 2 array codes.
int max = 100;
int length = 50;
String [] cars = new String[length];
int [] nums = new int [max];
I have 2 questions.
What is the value of nums[6]?
And,
What is the value of nums[max] ?
From the Java Language Spec:
Each class variable, instance variable, or array component is initialized with a default value when it is created (§15.9, §15.10)
For type int, the default value is zero, that is, 0.
So the answer to your first question is 0.
In Java, array indexes start with 0. Thus array has array.length elements with indexes 0, 1, 2, ... ,array.length - 1, and array[array.length] would throw an ArrayOutOfBoundsException.
nums[6] will be 0 because the int array would hold default values.
nums[max] will throw an exception, because your array's length equals max.
I hope this answers your question. nuns[6] is 0, because its the default value that java gives to arrays that haven't been specified.

parseInt: invalid int "0" [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
Whenever I want to parse the string "0" in Java to 0 (int) it throws a InvalidInt-Error.
However strings like "1", "2" etc. work.
UPDATE: Other numbers don't work as well.
I'm fetching the HTML source code of a PHP-File from my web page and this web page only displays one number.
Code:
String[] result = sourceCode.trim().split("<br>");
for (int i = 0; i < result.length; i++)
{
result[i] = result[i].trim().replace("\n", "").replace("\r", "");
}
if (Integer.parseInt(result[0]) > 0)
{
//Do Something
}
With the information provided in the comments below the question, it turns out that you have the Unicode Character 'ZERO WIDTH NO-BREAK SPACE' (U+FEFF) repeatedly at the beginning of your String. You can do the following to remove it:
final String ZERO_WIDTH_NO_BREAK_SPACE = "\uFEFF";
String good = result[0].replace(ZERO_WIDTH_NO_BREAK_SPACE, "");
Also, there is one TAB hidden inbetween all those characters. Get rid of it with
good = good.replaceAll("\\s+", "");

Categories