0

here i have a problem. i want a user to input some numbers, then i will convert the input into a string,i will then count the length of the string, and if it is less than 8,i want to add more zeros to the input to make it 8 so that i can do some staff with the number. i have tried to use decimalformat but its not working. plz help.

thanks in advance

int s=Integer.parseInt(s1.readLine());

      String news=String.valueOf(s);
      if(news.length()<8){
          DecimalFormat myformat=new DecimalFormat("00000000");
        String out= myformat.format(s);
        int onth=(Integer.valueOf(out)).intValue();
        s=onth;
      }else{
      System.out.format("your number is: %d\n",s); 
Edijae Crusar
  • 3,473
  • 3
  • 37
  • 74
  • What happens? Put all the relevant code (your else is not closed here...) – lpratlong Jun 06 '14 at 14:04
  • 1
    What is the result and what you expect? – Jens Jun 06 '14 at 14:04
  • With more information on the type of calculations you want to perform on this value, we could offer more specific advice. – JamesA Jun 06 '14 at 14:28
  • @Ipratlong men, it seems like i cant be able to edit my question. but what happens is that when i convert the string out to int, i cant be able to see the added zeros, but i need them so as to be able to convert the number back to string, get a substring, convert the substring to int, the square the int. e.g i want to get something like 00000144(let say the user entered 144), get substring at index[2-5] and square it. plz help coz my int only gives me 144 and removes all APPENDED ZEROS!! BUT I WANT THEM!! – Edijae Crusar Jun 06 '14 at 17:00
  • @JamesA men, it seems like i cant be able to edit my question. but what happens is that when i convert the string (out) to int, i cant be able to see the added zeros, but i need them so as to be able to convert the number back to string, get a substring, convert the substring to int, the square the int. e.g i want to get something like 00000144(let say the user entered 144), get substring at index[2-5] and square it. plz help coz my int only gives me 144 and removes all APPENDED ZEROS!! BUT I WANT THEM!! – – Edijae Crusar Jun 06 '14 at 17:03
  • @Jens okey, if a user type 144, the result is still 144. leading zeros are removed and i need them so as i can convert the nuber back to string, get substring fro index[2-5] e.g 0001, convert the number back to maybe integer and still get 0001, and square the number. thats what i expect sir – Edijae Crusar Jun 06 '14 at 17:17
  • @gikarasojokinene If you convert a string with leading Zeros back to an integer the Zeros are removed. – Jens Jun 06 '14 at 20:54
  • @Jens thanks alot. i finally understood that i cant get the leading zeros if i convert the string to integer and if i want to see the zeros, i have to make it remain as a formated string. – Edijae Crusar Jun 07 '14 at 08:37

4 Answers4

1

Forget about using the DecimalFormat.

Change your format to the following

System.out.format("your number is: %08d\n",s)

The %08d will lead with zeros, to a width of 8.

This will only display the number in the format you've requested. As stated elsewhere in this thread, treating it as a number would remove the leading zeros.

If you want to store it in a String variable however, you can use

String intString = String.format("%08d", s);

to store it.

  • Update *

As you have a specific need to get a series of numbers between a substring the following code will do what you want.

private static int getSubNumber(int startIndex, int stopIndex, int number) {
    String num = String.format("%08d", number);     
    return Integer.parseInt(num.substring(startIndex, stopIndex));
}

If you pass in the number you want to convert, it will change it to a string, and then convert the substring between the two indexes you pass in back into a number

System.out.println(getSubNumber(2,5,12345678));   // = 345
System.out.println(getSubNumber(2,5,12345));      // = 12
System.out.println(getSubNumber(2,5,123));        // = 0

This is non inclusive, getSubNumber(2,5,...) gets values at position 2,3 and 4 NOT 5.

For your example of 144, use start index 2, stop index 6 for positions 2, 3, 4 and 5

System.out.println(getSubNumber(2,6,144));        // = 1
JamesA
  • 365
  • 3
  • 11
  • I think he need to manipulate it as a number to do some calculation on it, according to "that i can do some staff with the number". – lpratlong Jun 06 '14 at 14:11
  • You do not get it as an int but always as a String. But maybe I did not understand the needs. I think he need to add 0 after its value. no? – lpratlong Jun 06 '14 at 14:40
  • @Ipratlong yes! i wanted to add some zeros to number, then convert it to int and still be able to see the added zeros.e.g(00000144) so that i can be able to covert the number again to string, get the substring starting at index 2(which is zero) to index 5(which is 1) convert it to int and squre it. somthing like that. let me update my question – Edijae Crusar Jun 06 '14 at 16:33
0

Even if you prefix an int with zeros the actual value will change to the original value. If you want padding you'll have to go with string. The out variable will give you the result.

Update based on comment

import java.util.Scanner;

public class SquareSubString {

    public static void main(String[] args) {
        String userInputSquare = getSquaredInput();
        int digits2to5 = Integer.parseInt(userInputSquare.substring(2, 6));
        System.out.println("Squre of digits 2 to 5 is : " + (digits2to5 * digits2to5));
    }

    private static String getSquaredInput() {
        System.out.println("Enter a number : ");
        Scanner in = new Scanner(System.in); 
    int input = in.nextInt();
    in.close();
        return String.format("%08d", (input * input));
    }

}
Syam S
  • 8,421
  • 1
  • 26
  • 36
  • so i cant be able to convernt the the number to (int,or long or double or float) and get the added zeros? e.g 00000144? coz i want to convert the number again to string, get the substring at index[2-5] ,lets say it will be 0001, convert the substring to maybe integer, the square the substring. is there any method or formular i can use? coz i have tried the above in my question and never worked. – Edijae Crusar Jun 06 '14 at 16:59
  • Ideally the prefix `0` only make sense in case of displaying a value. For any arithmetic operation the preceding zeros will be ignored. So if you want to manipulate with these zeros do it in a string form and when you want square or other arithmetic operation convert it to integer. – Syam S Jun 06 '14 at 17:03
  • men, so there is no way i can get the zeros even if i convert it to integer or float? there is no formular or method? its like av wasted over 10 hours on this sir! – Edijae Crusar Jun 06 '14 at 17:09
  • By int, float etc. you can have preceding zeros. It'll be ignored. You need to do string manipulation. If you could say about your requirement I can try and create a sample program. – Syam S Jun 06 '14 at 17:12
  • okey, this is what i want. i want a user to type in a number. my program will square the users number. the result of the square must be 8 digit long. if they are not, my program will add zeros at the beggining to make them 8. this is because my program will then take the digits starting at index 2 to index 5(e.g if result of multiplication is 12345678, digits at[2-5] are 3456) and squre them and give the user the result of squre. thats what i want sir. – Edijae Crusar Jun 06 '14 at 17:49
  • it work sir. thanks alot but what will i do to print out the 4 digits. e.g if i enter 12, i want to see 0001 coz 144 is not equal to 8 digits. a user can see the number that was squred. am trying to get the substring of userinputsquare at index 2-6, but i get StringIndexOutOfBoadException – Edijae Crusar Jun 06 '14 at 18:44
  • everything is good. i was able to get what i wanted with help of your sample program. thanks alot. am sorry, i don't have enough reputations to vote up :) – Edijae Crusar Jun 07 '14 at 08:41
0

If you need to add 0 after the value, you can multiply it by 10 pow the number of missing 0:

int result = Integer.parseInt(news);
if(news.length()<8){
    int diff = 8 - news.length();
    result = result * Math.pow(10, diff);    // ==> result = result * 10^(8 - news.length())
}

I think that's the simpliest way to do that.

Edit Ahhh, yes... There is prefix in the question. Nevermind!

lpratlong
  • 1,421
  • 9
  • 17
  • @Ipratlong what is the value of result? is it 0? and where did 10 come from? will this help add some zeros to a user number? e.g user types 144, since 144<8 digits, i add zeros to make it 00000144, i convert again to string, get the substring @ index[2-5] which is 0001 convert the substring to to maybe integer, and square the number. somthing like that sir. – Edijae Crusar Jun 06 '14 at 16:53
  • I take the problem at the opposite. 323 Will be 32300000 :). I did not understand the question I think. – lpratlong Jun 06 '14 at 20:31
0

DecimalFormat is to format numbers in the way we give the pattern.

And to append zeros, please follow this: Add leading zeroes to a string

Community
  • 1
  • 1
DDphp
  • 479
  • 2
  • 5
  • 17
  • av read it sir. my question is, how can you convert the string back to integer or double and still be able to see the added zeros. e.g you format the string and get 000000144, you convert the string to maybe integer e.g int g=(Integer.valueof(String).intValue(); and still get 000000144. – Edijae Crusar Jun 06 '14 at 18:02
  • Check this: http://stackoverflow.com/questions/6615454/converting-string-to-int-without-losing-the-zero-in-the-beginning – DDphp Jun 07 '14 at 11:47