9 Replies - 2648 Views - Last Post: 23 October 2009 - 08:20 AM Rate Topic: -----

#1 sfchin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 21-October 09

Integer to string conversion in Java

Posted 21 October 2009 - 12:04 PM

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.

Is This A Good Question/Topic? 0
  • +

Replies To: Integer to string conversion in Java

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10488
  • View blog
  • Posts: 38,868
  • Joined: 27-December 08

Re: Integer to string conversion in Java

Posted 21 October 2009 - 12:14 PM

To convert a primitive to String, try something like this:
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.
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8329
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Integer to string conversion in Java

Posted 21 October 2009 - 05:30 PM

View Postsfchin, 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 ?
Was This Post Helpful? 0
  • +
  • -

#4 sfchin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 21-October 09

Re: Integer to string conversion in Java

Posted 22 October 2009 - 09:35 PM

View Postmacosxnerd101, on 21 Oct, 2009 - 11:14 AM, said:

To convert a primitive to String, try something like this:
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

Was This Post Helpful? 0
  • +
  • -

#5 leska  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 20
  • Joined: 17-November 08

Re: Integer to string conversion in Java

Posted 23 October 2009 - 02:37 AM

You need to use String.valueOf() method.
Was This Post Helpful? 0
  • +
  • -

#6 cfoley  Icon User is online

  • Cabbage
  • member icon

Reputation: 1956
  • View blog
  • Posts: 4,058
  • Joined: 11-December 07

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.
Was This Post Helpful? 0
  • +
  • -

#7 d0ulikethat  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 14
  • Joined: 22-March 09

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());
	  }
	  
   }
}


Was This Post Helpful? 0
  • +
  • -

#8 sfchin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 21-October 09

Re: Integer to string conversion in Java

Posted 23 October 2009 - 05:33 AM

View Postd0ulikethat, 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
Was This Post Helpful? 0
  • +
  • -

#9 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10488
  • View blog
  • Posts: 38,868
  • Joined: 27-December 08

Re: Integer to string conversion in Java

Posted 23 October 2009 - 06:14 AM

:code:

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.
Was This Post Helpful? 0
  • +
  • -

#10 cfoley  Icon User is online

  • Cabbage
  • member icon

Reputation: 1956
  • View blog
  • Posts: 4,058
  • Joined: 11-December 07

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.


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();

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1