hello,
I am new to Java programming, I am trying to convert a hand phone such as 0172905383
to a string and store it in a string array. The normal way which uses the toString method
can't handle such a large number, is there way to convert such a large number ? I could
not store it as an integer in an integer array either because of the same reason. To
break it down to three numbers such as 017, 290, and 5383 ? Appreciate your reply.
Integer to string conversion in Java
Page 1 of 19 Replies - 2221 Views - Last Post: 23 October 2009 - 08:20 AM
Replies To: Integer to string conversion in Java
#2
Re: Integer to string conversion in Java
Posted 21 October 2009 - 12:14 PM
To convert a primitive to String, try something like this:
For more information on breaking Strings down, check out the String class:
http://java.sun.com/...ang/String.html
However, before we can help you out by providing source code, you have to show us that you have made an effort. Thanks for helping us help you!
Dream.In.Code has a policy by which we prefer to see a good faith effort on your part before providing source code for homework assignments. Please post the code you have written in an effort to resolve the problem, and our members would be happy to provide some guidance. Be sure to include a description of any errors you are encountering as well.
Please post like this:
Thank you for helping us helping you.
int x = 1; String y = x + ""; //value is "1"
For more information on breaking Strings down, check out the String class:
http://java.sun.com/...ang/String.html
However, before we can help you out by providing source code, you have to show us that you have made an effort. Thanks for helping us help you!
Dream.In.Code has a policy by which we prefer to see a good faith effort on your part before providing source code for homework assignments. Please post the code you have written in an effort to resolve the problem, and our members would be happy to provide some guidance. Be sure to include a description of any errors you are encountering as well.
Please post like this:
Thank you for helping us helping you.
#3
Re: Integer to string conversion in Java
Posted 21 October 2009 - 05:30 PM
sfchin, on 21 Oct, 2009 - 11:04 AM, said:
The normal way which uses the toString method can't handle such a large number,
the toString() methof of which class ?
I am afraid you are mixing apples and oranges here
How is your number stored in the class who's toString() method should return that number as a String ?
#4
Re: Integer to string conversion in Java
Posted 22 October 2009 - 09:35 PM
macosxnerd101, on 21 Oct, 2009 - 11:14 AM, said:
To convert a primitive to String, try something like this:
For more information on breaking Strings down, check out the String class:
http://java.sun.com/...ang/String.html
However, before we can help you out by providing source code, you have to show us that you have made an effort. Thanks for helping us help you!
[rules][/rules]
int x = 1; String y = x + ""; //value is "1"
For more information on breaking Strings down, check out the String class:
http://java.sun.com/...ang/String.html
However, before we can help you out by providing source code, you have to show us that you have made an effort. Thanks for helping us help you!
[rules][/rules]
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
hi, I have generated a java program using your codes, and then I tried differents values,
the results are
values results
---------------------------- -------------------
0172905383 does not work, integer too large
1111111111 works
9172905383 does not work, integer too large
3172905383 does not work, integer too large
2172905383 does not work, integer too large
1999999999 works
2555555555 does not work, integer too large
017 does not work, it gives an answer 15
it seems that it does not work for numbers starting with zero. Why does not work for 017 ?
My requirement is to convert some 10 digits hand phone numbers, all start with 012, 013, 106,
017, 019 and 014 to strings, store them in arrays.
If it works fine with numbers up to 1999999999, then I might have a solution. Appreciate your
comments. Below are the results and the little software.
Have a good day !
regards,
sf chin
&&&&&&$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
public class cont{
public static void main(String [] args){
int x = 017;
String y = x + "";
System.out.println( " answer is :"+ y );
}
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Microsoft Windows XP [Version 5.1.2600]
© Copyright 1985-2001 Microsoft Corp.
C:\Documents and Settings\PC>cd\program files
C:\Program Files>cd new workplace
C:\Program Files\new workplace>javac cont.java
cont.java:4: integer number too large: 0172905383
int x = 0172905383;
^
1 error
C:\Program Files\new workplace>javac cont.java
C:\Program Files\new workplace>java cont
answer is :1111111111
C:\Program Files\new workplace>javac cont.java
cont.java:4: integer number too large: 9172905383
int x = 9172905383;
^
1 error
C:\Program Files\new workplace>javac cont.java
cont.java:4: integer number too large: 3172905383
int x = 3172905383;
^
1 error
C:\Program Files\new workplace>javac cont.java
cont.java:4: integer number too large: 2172905383
int x = 2172905383;
^
1 error
C:\Program Files\new workplace>javac cont.java
C:\Program Files\new workplace>java cont
answer is :1999999999
C:\Program Files\new workplace>javac cont.java
cont.java:4: integer number too large: 2555555555
int x = 2555555555;
^
1 error
C:\Program Files\new workplace>javac cont.java
C:\Program Files\new workplace>java cont
answer is :15
C:\Program Files\new workplace>javac cont.java
C:\Program Files\new workplace>java cont
answer is :15
C:\Program Files\new workplace>
This post has been edited by sfchin: 22 October 2009 - 09:39 PM
#5
Re: Integer to string conversion in Java
Posted 23 October 2009 - 02:37 AM
You need to use String.valueOf() method.
#6
Re: Integer to string conversion in Java
Posted 23 October 2009 - 03:35 AM
Your program is converting to Strings just fine. What it is not doing is storing the ints properly. The maximum balue you can store in an int is 2,147,483,647. If you need a bigger number you will have to use long instead of int.
The second problem is you are trying to store numbers starting with zero. For a start 5 is numerically the same as 05 and 00000000005, int, long, double, etc will all store them without the zeros. So you're never going to get int or long to store numbers beginning with a zero.
To compound this problem, java lets the programmer code in octal values instead of decimal... and the way it recognises octal values is by preceding them by a zero.
When you entered 017 you expected:
(0 * 100) + (1 * 10) + (7 * 1) = 17
but you got
(0 x 64) + (1 * 8) + (7 * 1) = 15
In summary, ints are not a suitable data type for storing telephone numbers. String is a good choice int[] (that is arrays of ints) are OK too.
The second problem is you are trying to store numbers starting with zero. For a start 5 is numerically the same as 05 and 00000000005, int, long, double, etc will all store them without the zeros. So you're never going to get int or long to store numbers beginning with a zero.
To compound this problem, java lets the programmer code in octal values instead of decimal... and the way it recognises octal values is by preceding them by a zero.
When you entered 017 you expected:
(0 * 100) + (1 * 10) + (7 * 1) = 17
but you got
(0 x 64) + (1 * 8) + (7 * 1) = 15
In summary, ints are not a suitable data type for storing telephone numbers. String is a good choice int[] (that is arrays of ints) are OK too.
#7
Re: Integer to string conversion in Java
Posted 23 October 2009 - 04:07 AM
import java.util.ArrayList;
public class PhoneNumber
{
/**
Constructs a phone number
@param aNumber The phone number
*/
public PhoneNumber(String aNumber)
{
phoneNumber = aNumber;
}
/**
method that returns the phone number as an int
*/
// your work here
/**
Returns the phone number
*/
public String getNumber()
{
return phoneNumber;
}
private String name; // to add a person's name later
private String phoneNumber;
}
class PhoneNumberTester
{
public static void main(String[] args)
{
ArrayList<PhoneNumber> numList = new ArrayList<PhoneNumber>();
numList.add(new PhoneNumber("00000000000")); // add numbers to the arraylist
numList.add(new PhoneNumber("11111111111"));
numList.add(new PhoneNumber("22222222222"));
numList.add(new PhoneNumber("33333333333"));
numList.add(new PhoneNumber("44444444444"));
for(PhoneNumber ph: numList)
{
System.out.println(ph.getNumber());
}
}
}
#8
Re: Integer to string conversion in Java
Posted 23 October 2009 - 05:33 AM
d0ulikethat, on 23 Oct, 2009 - 03:07 AM, said:
import java.util.ArrayList;
public class PhoneNumber
{
/**
Constructs a phone number
@param aNumber The phone number
*/
public PhoneNumber(String aNumber)
{
phoneNumber = aNumber;
}
/**
method that returns the phone number as an int
*/
// your work here
/**
Returns the phone number
*/
public String getNumber()
{
return phoneNumber;
}
private String name; // to add a person's name later
private String phoneNumber;
}
class PhoneNumberTester
{
public static void main(String[] args)
{
ArrayList<PhoneNumber> numList = new ArrayList<PhoneNumber>();
numList.add(new PhoneNumber("00000000000")); // add numbers to the arraylist
numList.add(new PhoneNumber("11111111111"));
numList.add(new PhoneNumber("22222222222"));
numList.add(new PhoneNumber("33333333333"));
numList.add(new PhoneNumber("44444444444"));
for(PhoneNumber ph: numList)
{
System.out.println(ph.getNumber());
}
}
}
*******************************************************************************
hi all,
Thanks you for your replies.
I worked out a solution this afternoon, would like to share with you. Here is outline of my method, it needs to take input from scanner.
1. user to enter phone number through scanner, suppose the number is 0xxxxxxxxx.
2. add 1000000000 to input from scanner and assign it to a variable. So the number now becomes 1xxxxxxxxx.
3. minus 1000000000 from the variable. That removes the leading zero and becomes 9 digits.
the number now becomes xxxxxxxxx.
then you can convert it to string. You can't assign scanner's input to a variable, then add/monus
1000000000 from the variable, it won't works.
I put out my codes here, please take a look at the codes. I think it works because scanner takes whatever inputs from user, it has no restrictions, it is ok with numbers starting from zero, and I modified the numbers to a format acceptable to the computer before assigning it to a variable, therefore it works. And when displaying the information to the users, i add a zero to the front of the number so that it reads 0xxxxxxxxx again. I will try your methods .
Have a good day !
regards,
sf chin
***************************************************************************
import java.util.Scanner;
public class chi {
String personArray[] = new String[30];
public chi ( ){
}
public String toString ()
{
int id = 200;
String name = " David";
String sex="MALE";
Scanner input = new Scanner(System.in);
System.out.println (" please enter your hand phone number: ");
int hphone = 1000000000 + input.nextInt();
hphone = hphone - 1000000000;
System.out.println ( "hphone is :"+ hphone );
return personArray[0] = name+"'s "+"hand phone number is :"+0+hphone+" "+sex+" "+"ID is :"+id;
}
}
*****************************************************************************
public class objs {
public static void main( String [] args)
{
chi s1= new chi();
System.out.println(s1.toString());
}
}
*************************************************************************
output by running the program
Microsoft Windows XP [Version 5.1.2600]
© Copyright 1985-2001 Microsoft Corp.
C:\Documents and Settings\PC>cd\
C:\>cd program files
C:\Program Files>cd new workplace
C:\Program Files\new workplace>javac objs.java
C:\Program Files\new workplace>java objs
please enter your hand phone number:
0172905383
hphone is :172905383
David's hand phone number is :0172905383 MALE ID is :200
C:\Program Files\new workplace>
*************************************************************************
Attachment : No file attach...
| Reply To This Message
#9
Re: Integer to string conversion in Java
Posted 23 October 2009 - 06:14 AM
When an integer value has a 0 at the front of it, Java reads it as an Octal (or base 8) value. Therefore, any number with digits greater than 7 will cause an error. Likewise, appending 0x to the front of a number tells the compiler to read the value as a Hexadecimal (base 16) value. I would simply read in the values as Strings and use String manipulation to validate them.
#10
Re: Integer to string conversion in Java
Posted 23 October 2009 - 08:20 AM
Quote
1. user to enter phone number through scanner, suppose the number is 0xxxxxxxxx.
2. add 1000000000 to input from scanner and assign it to a variable. So the number now becomes 1xxxxxxxxx.
3. minus 1000000000 from the variable. That removes the leading zero and becomes 9 digits.
the number now becomes xxxxxxxxx.
2. add 1000000000 to input from scanner and assign it to a variable. So the number now becomes 1xxxxxxxxx.
3. minus 1000000000 from the variable. That removes the leading zero and becomes 9 digits.
the number now becomes xxxxxxxxx.
Oh, very inventive! I like it. However it still has its problems. You're limiting the phone number to 9 digits. Maybe that's OK for your app but you also lose the number of leading zeros. Say I enter 01234, add 1e9 =>1000001234 knock off the first digit => 000001234 which is wrong.
There is also a problem with your code:
int x = 1000001234 - 1000000000; // x now equals 1234 String str = "" + x; // str is now 1234
YOu would need something like this:
int x = 1000001234; String str = "" + x; // str is 1000001234 str = str.substring(1); // str is now 000001234
But anyway, I think you are missing a really simple solution:
read the phone number as a string in the first place
Scanner sc = new Scanner(System.in); String phoneNumber = sc.next();
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote








|