Integer - toString() function [duplicate] - java

This question already has answers here:
How to convert number to words in java
(31 answers)
Int to English words in Java
(4 answers)
Closed 9 years ago.
I search but cannot find on the web.
Is there a function that can represent any integer into its String litteral representation ?
Example :
Integer i1 = new Integer(4);
Integer i2 = new Integer(30);
i1.callSomeFunction();
i2.callSomeFunction();
Output :
"Four"
"Thirty"
If you know another function which isn't in the Integer class, it's fine too.

There is no standard method to do this but take a look at this. There is an example for a number to words in the English language converter.

Related

Comparing 2 complex numbers [duplicate]

This question already has answers here:
Is it possible to overload operators in Java? [duplicate]
(6 answers)
How to compare two double values in Java?
(7 answers)
Closed 2 years ago.
I have created a class for complex numbers and a constructor for the class which takes 2 doubles, like
Complex c = new Complex(3.0,1.0)
Then how do I compare a Complex number to 0? I know that I need to create a tolerance number, say
TOL = 1.0e-10
Do I just do this:
Complex NullComplex = new Complex(TOL,TOL);
c >= NullComplex;
I would like to get a boolean output from this. But whenever I want to compare, I get that the operators "=>" are undefined for the type double, Complex (or Complex, Complex)

Can anybody explain why int x=1_000_000 is a valid and prints 1000000 instead throwing an error? [duplicate]

This question already has answers here:
Java 7 underscore in numeric literals
(8 answers)
Closed 4 years ago.
int x = 100_000_5;
System.out.println(x);
Instead of throwing an error it prints the result 1000005. If this is a valid case what other abnormalities are there with Integer?
int x = 100_000_5 is same as int x = 1000005. JVM7 allows to write literals with more readable format, e.g. separate groups.
See more in Primitive Data Types

Extract/Trim part of string, Java [duplicate]

This question already has answers here:
Java - removing first character of a string
(14 answers)
Closed 5 years ago.
Say String x = MynameisMary;
I want String Y to be "Mary"
How do i remove the first 8 characters to make Y equal to Mary??
You could use the substring method:
String result = x.substring(8);

Convert unicode string to int or long [duplicate]

This question already has answers here:
How do I convert a String to an int in Java?
(47 answers)
Closed 6 years ago.
For some purpose, I want to convert the String to my Unicode to int or long or double. I just need a number. Can anyone tell me how can I do that?
If you want to get primitive values, use :
Integer.parseInt("12");
Long.parseLong("1024");
Double.parseDouble("1.52");
If you want to get objects corresponding to these values (Integer object, Long object, etc), use :
Integer.valueOf("12");
Long.valueOf("1024");
Double.valueOf("4.17");

Calculate a formula from a string [duplicate]

This question already has answers here:
How to evaluate a math expression given in string form?
(26 answers)
Closed 6 years ago.
I want to do calculator that do sums but just in one index like this:
args[0] = "2+2"
and the output should be 4.
I won't do your homework for you, but I'll split it in few steps:
You should get String before the "+" and after the "+":
Have a look at String.indexOf and String.substring for that. Make sure you check for -1 for indexOf.
Parse String to int:
int foo = Integer.parseInt(String s);
Now you can add two ints.
Good luck!

Categories