?
Solved

How to check if an integer value has eight digits and insert 0's

Posted on 2006-05-31
16
Medium Priority
?
257 Views
Last Modified: 2010-03-31
I am getting an integer value from user, which is a customer ID and I need to search for that ID in the database. Now the problem is that the id is stored in the data base as a eight digit number. so if I receive a number '6578' it will stored in the database as '00006578'. Now what I need is to check the number if it is eight digit long, if not then insert 0's in the beginnng. Any help will be greatly appreciated.
0
Comment
Question by:royalcyber
  • 4
  • 4
  • 3
  • +4
16 Comments
 
LVL 30

Expert Comment

by:Mayank S
ID: 16799591
You can pad the string with 8 - string.length () zeroes using a StringBuffer or simply concatenating using + (bad approach).

Otherwise if you want to use this for some comparision later, you can trim the other string and remove leading 0's from it. Or parse both using Integer.parseInt () to give you the integer values and compare them.
0
 

Author Comment

by:royalcyber
ID: 16799687
I have the number in a string variable

So you are saying that I should check for the length first, if it is less than eight then use String Buffer. But can you please explain how do I use String buffer and why concatenation is a bad appraoch. Thank you very much for your help.

String member_id = request.getParameter("ID");

If(member_id.length() < 8){

   How to use String Buffer ?
   
}
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 16799924
I guess you get it as a String and convert it to int to check it.
Then do this:

   int number;
   String toDatabase;
   DecimalFormat df = new DecimalFormat("00000000"); // 8 0's
   number = .........

   toDatabase = df.format(number);    // Always 8 digits.

;JOOP!
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 10

Expert Comment

by:mukundha_expert
ID: 16799983
Try this,

            StringBuffer custId = new StringBuffer(request.getParameter("ID"));
            while(custId.length()<8){
                  custId.insert(0,'0');
            }
                  

0
 
LVL 86

Accepted Solution

by:
CEHJ earned 1000 total points
ID: 16800787
String custId = String.format("%08d", 6578);
0
 
LVL 13

Expert Comment

by:Webstorm
ID: 16801567
Another short solution if you're using old Java runtime (as can be expected from application users) :

String member_id = "00000000"+request.getParameter("ID");
member_id=member_id.substring(member_id.length()-8);
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16804870
Note that String.format () will only work for Java 5.0
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16804874
>> How to use String Buffer ?

StringBuffer temp = new StringBuffer () ;

for ( int i = 0, length = str.length () ; i < 8 - length ; i ++ )
  temp.append ( "0" ) ;

String result = temp.append ( str ).toString () ;
0
 
LVL 14

Expert Comment

by:hoomanv
ID: 16805217
easy padding

String zeros = "0000000000";
String number = "1234";
int len = 8 - number.length();
if (len > 0)
    number += zeros.substring(0, len);


result --> 00001234
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 16805268
What an ugly solutions!

The only solution to leftzero's (that's the official name) is DecimalFormat,
or as CEHJ stated, the new JAVA 5 .format() and .printf()

;JOOP!
0
 
LVL 14

Expert Comment

by:hoomanv
ID: 16805352
> The only solution to leftzero's (that's the official name) is DecimalFormat
THE <<< ONLY >>> SOLUTION :D

dont scare, the points will be givven to the most BEAUTIFUL one
0
 
LVL 14

Expert Comment

by:hoomanv
ID: 16805381
also my answer should be
number = zeros.substring(0, len) + number;
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 16805390
Beautiful?
0
 
LVL 14

Expert Comment

by:hoomanv
ID: 16805420
solution
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16805581
Well, there can always be many solutions, just that some will not be so good but some good ones will also use the bad ones internally ;-)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16847408
:-)
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses
Course of the Month15 days, 23 hours left to enroll

850 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