• Post Reply Bookmark Topic Watch Topic
  • New Topic

converting long to string?  RSS feed

 
Leyli Majnun
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I have to use a long primitive type for the input of a credit card number and ID the credit card by using the first number of the input; however, the only way I know for that is to use charAt, which is used for a String. Is there a way to convert long to String, or am I missing a better solution? (There's no code because I'm still doing the pseudocode).
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37462
537
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your instinct to use charAt() is a good way. There are a few ways to convert a long to a String. Here are two:

 
Leyli Majnun
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks! Especially for the quick reply!
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16058
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would use the static toString() method of class Long:

However, using the type long to store a credit card number is not a good idea. you should not use long at all; just use String to store the credit card number.

Just because a credit card number is a number doesn't mean that a numeric type such as long is appropriate. You should really only use numeric types (int, long etc.) to store data that you for example need to do arithmetic with. A credit card number is conceptually not really a number, it's a "label" that identifies a credit card, that happens to contain only digits.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16058
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne Boyarsky wrote:Your instinct to use charAt() is a good way. There are a few ways to convert a long to a String. Here are two:


The first possibility that Jeanne showed will not work, class String does not have a constructor that takes a long.
 
Leyli Majnun
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper, I see why you'd think that, but the assignment involves the Luhn method, which does involve math. Also, it specifies the use of long to store the input in, so I can't use String from the get-go, unfortunately.
 
Steve Fahlbusch
Bartender
Posts: 612
7
Mac OS X Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you have to use a long and the user enters a long number then remember it can't start with a 0 <- thats a zero
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37462
537
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:The first possibility that Jeanne showed will not work, class String does not have a constructor that takes a long.

You're right. I almost use ""+num (and yes, I know that Long.toString() is more efficient.)
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!