Results 1 to 2 of 2

Thread: Need help, leading zeros in java

  1. #1
    Join Date
    Dec 2010
    Posts
    1

    Need help, leading zeros in java

    Hello.
    I need help with a program in Java im trying to make.
    Its supposed to build a program like the one the Quebec Ministry Of Education uses to give studends their codes.

    The thing is that after typing personal info in the program, the program should build the code from the name, last name, day month and year of birth, sex and other stuff.

    But i cant figure out how to print a leading zero when the day or month are under 10 (this must be only when the day or month are under 10)

    And im also supposed to make that if the user chooses sexe as Feminin, it should add 50 to the month value.


    This is my code (sorry its in french , ill put some comments so u can get around)

    Code:
    import java.util.*;
    
    public class TP3B {
    
    	public static void main(String args[] ) {
    
    	Scanner clavier = new Scanner(System.in);
    
    	System.out.print("Saisir le nom: "); //Last name
    	String nom = clavier.next();
    	nom = nom.toUpperCase();
    
    	System.out.print("Saisir le prenom: "); //Name
    	String prenom = clavier.next();
    	prenom = prenom.toUpperCase();
    
    	System.out.print("Saisir le jour de naissance: "); //Day of Birth
    	int jourNaissance = clavier.nextInt();
    
    	if (jourNaissance < 10){
    		
    	}
    
    	System.out.print("Saisir le mois de naissance: "); //Month of birth
    	String moisNaissance = clavier.next();
    
    	System.out.print("Saisir l'annee de naissance: "); //Year of Birth
    	String anneeNaissance = clavier.next();
    
    	System.out.print("Est-il Homme (O/N): "); // Are you a man?
    	String sexe = clavier.next();
    
    System.out.println("Voice votre code permanent: " + nom.substring(0, 3) + prenom.substring(0, 1) + moisNaissance + anneeNaissance.substring(2, 4) + doubles);
    Thank you again for the help!

  2. #2
    Join Date
    Jan 2006
    Posts
    605

    Re: Need help, leading zeros in java

    The below example shows you how to use the String.format() method to add zero padding to a number. If you just want to print out the result you can use System.out.format().

    Code:
    package org.kodejava.example.lang;
    
    public class LeadingZerosExample {
        public static void main(String[] args) {
    	int number = 1500;
    	
    	//
    	// String format below will add leading zeros (the %0 syntax) to the number above. 
            // The length of the formatted string will be 7 characters.
    	//
    	String formatted = String.format("%07d", number);
    	
    	System.out.println("Number with leading zeros: " + formatted);
        }
    }

Similar Threads

  1. Replies: 7
    Last Post: 21-01-2012, 01:25 PM
  2. How to Format a cell to keep leading zeros in Microsoft Excel
    By Raju Chacha in forum MS Office Support
    Replies: 3
    Last Post: 14-01-2012, 07:05 PM
  3. java program to add leading zeros to a number.
    By kamina23 in forum Software Development
    Replies: 4
    Last Post: 05-02-2010, 07:15 PM
  4. How can I Remove the Leading Zeros in SQL?
    By PsYcHo 1 in forum Software Development
    Replies: 4
    Last Post: 04-02-2010, 06:59 AM
  5. EXCEL not display leading zeros
    By Aasha in forum Windows Software
    Replies: 3
    Last Post: 22-10-2009, 06:21 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,587,151.26213 seconds with 17 queries