1 2 Previous Next 16 Replies Latest reply: Oct 15, 2006 7:05 PM by 807598 RSS

    need to keep a 0 in front of an int

    807598
      Here's what i've done:

      int dayarrival;
      do{

      System.out.print("Enter the day of arrival: ");
      dayarrival= Clavier.lireInt();

      }while(!((dayarrival >= 001) && (dayarrival <= 366)));

      now if dayarrival is <99 i need to keep a 0 in front of it or yet again if dayarrival is <10 i need to keep 2 0's in front of it

      Any idea's?
        • 1. Re: need to keep a 0 in front of an int
          3004
          Use java.text.DecimalFormat.
          • 2. Re: need to keep a 0 in front of an int
            807598
            whats that and how to i apply it?
            • 3. Re: need to keep a 0 in front of an int
              3004
              http://java.sun.com/docs/books/tutorial/i18n/format/decimalFormat.html
              http://java.sun.com/j2se/1.5.0/docs/api/java/text/DecimalFormat.html


              If you can't follow those, or are not allowed to use DecimalFormat, then do it manually.

              if the value is les than 10, then str = str + String.vlaueOf(the int), etc.
              • 4. Re: need to keep a 0 in front of an int
                807598
                Any other simple ideas, cuz those aren't working for me!
                • 5. Re: need to keep a 0 in front of an int
                  3004
                  Any other simple ideas, cuz those aren't working for
                  me!
                  It doesn't get any simpler than that.

                  What, exactly, does "not working" mean?

                  Post code and details of what problems you experienced.

                  When you post code, please use[code] and [/code] tags as described in Formatting tips on the message entry page. It makes it much easier to read.
                  • 6. Re: need to keep a 0 in front of an int
                    807598
                    Alright, first of all my program tells me it canot find symbol - class string

                    And here is my code
                      do{
                                System.out.print("Entrez le jour d'arriv? du lot: ");
                                jourarriver = Clavier.lireInt();
                                
                            }while(!((jourarriver >= 001) && (jourarriver <= 366)));
                    Now all i need is to add 0's as prefix to number entered under 99 or 2 0's to numbers entered under 10!

                    im new to this but thanks!
                    • 7. Re: need to keep a 0 in front of an int
                      3004
                      Alright, first of all my program tells me it canot
                      find symbol - class string
                      The code you posted doesn't have string in it anywhere. If you have an error, show the actual code.

                      In this case, it's easy: String, not string. Java identifiers are case-sensitive.



                      Now all i need is to add 0's as prefix to number
                      entered under 99 or 2 0's to numbers entered under
                      10!
                      Try one of the approaches I suggested above.
                      • 8. Re: need to keep a 0 in front of an int
                        807598
                        i know they are case-sentive, but do you need to declare str as a string, if so my program tells me that error
                        do{
                                    System.out.print("Entrez le jour d'arriv? du lot: ");
                                    jourarriver = Clavier.lireInt();
                                    
                                }while(!((jourarriver >= 001) && (jourarriver <= 366)));
                              
                                  if (jourarriver <10){
                                      str = str + String.vlaueOf(jourarriver); 
                                    }
                        is that correct or do i need to put my if in the do-while?
                        • 9. Re: need to keep a 0 in front of an int
                          807598
                          By the way please realize that the way you format numbers in Java code will have little to do with how numbers get formatted in output. To control output formatting you specify the format separately (with, say, a DecimalFormat). Number formatting in basic Java code may have effects you may not expect. For example:
                          public class TestOctal {
                            public static void main(String[] argv) {
                              System.out.println(0023);
                            } 
                          }
                          This prints "19". That's because the leading zero told Java that you're specifying the 23 as an octal number. In your code, the leading 00 doesn't make any difference because 1 in octal is the same as 1 in decimal.
                          • 10. Re: need to keep a 0 in front of an int
                            807598
                            my project specifies that if the number entered is under 99 then it should print 099 and if its under 10 then 009 and so on!
                            • 11. Re: need to keep a 0 in front of an int
                              807598
                              }while(!((dayarrival >= 001) && (dayarrival <=
                              366)));

                              now if dayarrival is <99 i need to keep a 0 in front
                              of it or yet again if dayarrival is <10 i need to
                              keep 2 0's in front of it

                              Any idea's?
                              Is it your impression in the above code that the integer literal 001 is somehow different from 1?

                              When you specify an integer with a leading 0, you are saying it is an octal representation of an integer, i.e. in base 8. However, the octal integer 001 and the decimal integer 1 are the same. On the other hand, the integer literals 010 and 10 are not the same.

                              You can put as many leading 0's as you want in front of 1, but internally java isn't going to store the number any differently. Try this code:
                              public class AATest1
                              {
                                   public static void main(String[] args) 
                                   {
                                        int a = 010;
                                        System.out.println(a);
                              
                                        int b = 1;
                                        System.out.println(b);
                              
                                        int c = 000000001;
                                        System.out.println(c);
                                   }
                              }
                              • 12. Re: need to keep a 0 in front of an int
                                3004
                                i know they are case-sentive, but do you need to
                                declare str as a string, if so my program tells me
                                that error
                                What you're saying here doesn't make sense.

                                There is a class called String. There is no class called string, unless you created one or brought it in from a 3rd party library.

                                So change it to String.
                                • 13. Re: need to keep a 0 in front of an int
                                  807598
                                  do you need to declare str as a string, if so my program tells me that error
                                  You must declare str as a String. As paulcw says it is this string that you
                                  will be formatting, not the jourarriver int.
                                  is that correct or do i need to put my if in the do-while?
                                  It goes after the do-while as you have it. But the way you set str looks a little
                                  odd. Try:
                                  do {
                                      // etc
                                  } while(!((jourarriver >= 1) && (jourarriver <= 366)));
                                  
                                  String str = String.vlaueOf(jourarriver);
                                      // put a 0 at the front if the number is less than *100*
                                  if (jourarriver <100) {
                                      str = "0" + str; 
                                  }
                                      // put another 0 at the front if needed
                                  
                                      // then, later, output
                                  System.out.println("jourarriver = " + str);
                                  • 14. Re: need to keep a 0 in front of an int
                                    3004
                                    do{
                                    System.out.print("Entrez le jour d'arriv?
                                    du lot: ");
                                    jourarriver = Clavier.lireInt();
                                    
                                    }while(!((jourarriver >= 001) && (jourarriver
                                    <= 366)));
                                    
                                    if (jourarriver <10){
                                    str = str + String.vlaueOf(jourarriver); 
                                    }
                                    ode]
                                    Use 1, not 001. If you precede an integer literal with a 0, it's taken as base-8.

                                    Also note that, while 001 and 1 are the same, note the following diferences


                                    010 is different from 10

                                    001 is different from "001"

                                    1 is different from "1"
                                    1 2 Previous Next