?
Solved

converted it to from string to long precedence 0 should be added

Posted on 2006-04-12
38
Medium Priority
?
253 Views
Last Modified: 2010-03-31
String x="07070818";
    System.out.println(Long.parseLong(x));

when i converted it to from string to long precedence 0 should be added.
0
Comment
Question by:chaitu chaitu
  • 16
  • 13
  • 9
38 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 16434831
>>when i converted it to from string to long

Leading zeros are attributes of strings, not numbers
0
 
LVL 20

Author Comment

by:chaitu chaitu
ID: 16434835
as u said it returns in string format but i want to assign this value to long varaible;
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16434846
The code you posted already  does that
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 20

Author Comment

by:chaitu chaitu
ID: 16434857
but i am losing 0 precedence if i do this  Long.parseLong(x)
0
 
LVL 13

Expert Comment

by:Webstorm
ID: 16434901
long l=Long.parseLong(x);

then reformat when you display
0
 
LVL 13

Expert Comment

by:Webstorm
ID: 16434914
then reformat when you display or convert to a string :

String s=String.format("%08d", l );
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16434927
>>then reformat when you display

You don't need to - it was derived  from a leading zero string in the first place ;-)
0
 
LVL 20

Author Comment

by:chaitu chaitu
ID: 16434937
can i use NumberFormat in this case;;
0
 
LVL 20

Author Comment

by:chaitu chaitu
ID: 16434940
my aim is return value should be in long ;
0
 
LVL 13

Expert Comment

by:Webstorm
ID: 16434955
>> can i use NumberFormat in this case;;
yes
>> my aim is return value should be in long
then you won't have leading 0, and should format it when you display its value.
0
 
LVL 20

Author Comment

by:chaitu chaitu
ID: 16434970
"then you won't have leading 0, and should format it when you display its value"

How?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16434980
System.out.printf("%08d\", values);
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16434985
Typo - should be

 System.out.printf("%08d\n", value);
0
 
LVL 20

Author Comment

by:chaitu chaitu
ID: 16435077
Error(18,15): method printf(java.lang.String, java.lang.String) not found in class java.io.PrintStream
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16435101
You will need to use NumberFormat as you have an old version of Java
0
 
LVL 13

Expert Comment

by:Webstorm
ID: 16435211
NumberFormat nf=new NumberFormat();
nf.setMinimumIntegerDigits(8);
String s=nf.format(l);  
0
 
LVL 20

Author Comment

by:chaitu chaitu
ID: 16435341
problem still persists


  String x="0707";
  DecimalFormat fmt = new DecimalFormat("#########0000");

       
        Number n = fmt.parse(x);
       
          String strDoubleValue=fmt.format(n.longValue());
          System.err.println("strDoubleValue :"+strDoubleValue);  --0707
          long doubleValue1=Long.parseLong(strDoubleValue);
          System.err.println("doublevalue :"+doubleValue1);----------707
0
 
LVL 13

Expert Comment

by:Webstorm
ID: 16435351
long is only a binary primitive type, and will old your value as binary : 707
displaying such type discard any leading 0.
-> need to format as a string

   
0
 
LVL 13

Expert Comment

by:Webstorm
ID: 16435356
If you want to keep leading 0, use String
0
 
LVL 20

Author Comment

by:chaitu chaitu
ID: 16435360
i want to assign this long value to some other long varaible
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16435434
>> want to assign this long value to some other long varaible

Then the question of whether it has leading zeros is meaningless
0
 
LVL 20

Author Comment

by:chaitu chaitu
ID: 16435512
my requirment is i want the format with leading zeros say x ="0xxxxxxx" this value should be inserted into database;

 i will assign this  long varaible  say Emp.refno =x somewhere in controller;
0
 
LVL 13

Expert Comment

by:Webstorm
ID: 16435583
->
String x ="0xxxxxxx";
Emp.refno =x;
0
 
LVL 20

Author Comment

by:chaitu chaitu
ID: 16435607
how can u assign string to long varaible???/
0
 
LVL 13

Expert Comment

by:Webstorm
ID: 16435655
then:
Emp.refno =Long.parseLong(x);
0
 
LVL 20

Author Comment

by:chaitu chaitu
ID: 16435662
then leading zero will not come in Emp.refno ;;;
0
 
LVL 20

Author Comment

by:chaitu chaitu
ID: 16435672
String x="0707";
  DecimalFormat fmt = new DecimalFormat("#########0000");

       
        Number n = fmt.parse(x);
       
          String strDoubleValue=fmt.format(n.longValue());
          System.err.println("strDoubleValue :"+strDoubleValue);  ----- 0707
       
long x1 =Long.parseLong(strDoubleValue);
System.out.println(x1);----------707
0
 
LVL 13

Expert Comment

by:Webstorm
ID: 16435676
>> then leading zero will not come in Emp.refno
because you don't need it

if you want to display it -> format
OR
if you want to store it in DB with '0' -> use String instead of long
0
 
LVL 20

Author Comment

by:chaitu chaitu
ID: 16435702
then leading zero will not come in Emp.refno

I need the leading zero



0
 
LVL 13

Expert Comment

by:Webstorm
ID: 16435771
-> use String instead of long

if you need both long and leading '0' -> impossible
0
 
LVL 13

Expert Comment

by:Webstorm
ID: 16435801
but you can use Long, and extend the class :

public class RefNo extends Long
{
   DecimalFormat fmt = new DecimalFormat("#########0000");
   public String toString()
   {
        long l=toLongValue();
        return fmt.format(l);
    }
}

Long refno=new RefNo(707L);
0
 
LVL 20

Author Comment

by:chaitu chaitu
ID: 16435832
toLongValue()????
0
 
LVL 20

Author Comment

by:chaitu chaitu
ID: 16435861
Error(5,28): can't inherit from final class java.lang.Long
0
 
LVL 13

Expert Comment

by:Webstorm
ID: 16435953
public class RefNo
{
   long refno;
   public RefNo(long l){ this.refno=l; }
   DecimalFormat fmt = new DecimalFormat("#########0000");
   public String toString()
   {
        long l=refno
        return fmt.format(l);
    }
}
0
 
LVL 13

Expert Comment

by:Webstorm
ID: 16435958
RefNo refno=new RefNo(707L);
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 2000 total points
ID: 16436034
chaituu - there seems to be a fundamental misunderstanding:

numbers do NOT have leading zeros and such a concept is meaningless. Only graphical representations of numbers (strings) are given leading zeros for formatting purposes. For the purposes of entering the number into the db, leading zeros should be ignored. When it comes to *showing* the db value as a string, that's a different matter and you've been given several different ways to do that
0
 
LVL 20

Author Comment

by:chaitu chaitu
ID: 16436052
yes i understood long before.....
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16436092
>>yes i understood long before.....

Then the question is solved
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month13 days, 9 hours left to enroll

750 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question