Converting the string phone number to integer [closed] - java

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions must demonstrate a minimal understanding of the problem being solved. Tell us what you've tried to do, why it didn't work, and how it should work. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am fairly inexperienced in Java as well as Android. I am trying to retrieve a phone number stored in one of the contacts of the android phone emulator. While I am successful to fetch it, the number has been returned in a string in a format like "(987) 654-3210".
I want to convert it to integer or long. How can I do that? If I use Integer.parseInt(String number), it returns a NumberFormatException. Failed while tried using Long.valueOf(String number) too. What should I do then? I want it like "9876543210" without any braces or hyphens.

using the long for storing number will be better
String str="(987) 654-3210";
String stt=str.replaceAll("\\D+","");
long num= Long.parseLong(stt);
System.out.println(num);

This should be simple. You could use regex in java to do this like below
phoneStr = str.replaceAll("\\D+","");
This will delete the non digits from the string and give you only numbers. Then you can use
int number = Integer.parseInt(phoneStr);

Related

Converting string phone number to long: Number Format Exception [duplicate]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions must demonstrate a minimal understanding of the problem being solved. Tell us what you've tried to do, why it didn't work, and how it should work. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am fairly inexperienced in Java as well as Android. I am trying to retrieve a phone number stored in one of the contacts of the android phone emulator. While I am successful to fetch it, the number has been returned in a string in a format like "(987) 654-3210".
I want to convert it to integer or long. How can I do that? If I use Integer.parseInt(String number), it returns a NumberFormatException. Failed while tried using Long.valueOf(String number) too. What should I do then? I want it like "9876543210" without any braces or hyphens.
using the long for storing number will be better
String str="(987) 654-3210";
String stt=str.replaceAll("\\D+","");
long num= Long.parseLong(stt);
System.out.println(num);
This should be simple. You could use regex in java to do this like below
phoneStr = str.replaceAll("\\D+","");
This will delete the non digits from the string and give you only numbers. Then you can use
int number = Integer.parseInt(phoneStr);

How to display a portion of a string at an index of a string array [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a string array
String[] albumnames;
now how to take a string from particular index position with limited number of charachters.
For example,
if, albumnames[position] have value "abcdefghijk"
then i want to take the first 5 characters only.
That is "abcde".
The substring method of String can be used to achieve this. Try
String s = albumnames[position].substring(0,5);
See substring docs
albumnames[position].substring(0,5);
you can see the methods in String class, like substring, indexof

parse string containing number with minus on the right [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a problem - is there any possibility to recognize in the text the negative number, that has a minus sign on the right?
E.g. I'm thinking about number like that: 1500.0- (instead of -1500.0).
Thank you in advance for any help.
Not only is it possible, it is actually easier to handle a trailing sign. You just convert the number as usual, continuing while you keep getting digits, then if it ends with a minus sign just negate it. Easier than having to remember a leading sign.
Despite the names of the methods provided in the JDK, this is not 'parsing', it is radix conversion.
You could use charAt or a regular Expression, and many more...
String s = "1500.00-";
if (s.charAt(s.length()-2) == '-'){
//minus on the right.
}
First move the minus (if any) from the end to the start:
num = num.replaceAll("(.*)(-)?$", "$2$1");
The good thing here is that if there's no minus sign at the end, or yjr minus us alteady at the start, nothing change it made.
Then parse it as normal, eg:
double d = Double.parseDouble(num);

multiply large no. in array [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
multiply any 2 numbers. The numbers can be extremely large (i.e. run into hundreds of digits) and are provided as strings.
The expected output is a string which represents the product of the two numbers.
example-
multiply("268435456","524288")="140737488355328"
multiply("12321412423524534534543","0")="0"
Use BigDecimal, which has a multiply method and a constructor which takes a String. It also contains corresponding toString() and toPlainString() methods to get your result as a string.
(If the numbers are always whole numbers, then use BigInteger instead.)

How to create random cell number for testing purpose [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have to create a random cellnumber 07939393914 for automation testing purpose.
Last 079393(5 digits) digits should change randamly.. each time test runs..
Can any one suggest JAVA code for this ? or else Selenium Java code ?
Thanks
Is this a number or a String? I ask as it has a leading zero.
Take you initial number as 7939300000
then add to it Math.round(Math.Random()*10000)
If you want it as a String, take your string as "079393" and use Integer.toString on the result above, then concatentate them
Use RandomStringUtils class.
String randomNumbers = RandomStringUtils.randomNumeric(5);
String phNo = 079393+randomNumbers;
var num=Math.ceil(Math.Random()*100000)
for random first 5 digit numbers
and add
var your_last_5digits; //as string to your last 5 digits
cellNumber='0'+num+your_last_5digits;

Categories