Android Question Remove leading Zero's

ilan

Expert
Licensed User
Longtime User
hi

i have a list of files. files are named (1.txt,2.txt,.....23.txt,...)
now i add all to a list and i want to sort them but i get

1.txt
11.txt
12.txt
...

so i use numberformat with 4 digits like (0001,0002,..) now i get the right order
but after i have sorted the list i want to remove all leading "0" of all items

i tried to get the item and change it to int r=or double and it didnot work.
numberformat(NR,1,0) does not work either.

what else could i try??

thanx
 

moster67

Expert
Licensed User
Longtime User
something silly like converting string into Int/Long and then reconvert them back to string...
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
If they are all .txt files, you could remove the extension and sort the list (I think it will then sort in true number order) Then add the extension back again afterwards.

Alternatively, you could create a Type and add just the numbers to a sort field and the name to a second field, add the types to a list, sort it, then extract the names in order.
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
Or another thought keep the zero fill, then remove the .txt extension and cast the numeric part to an int, then add the extension back again.
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
Or another thought keep the zero fill, then remove the .txt extension and cast the numeric part to an int, then add the extension back again.

this is exactly what i have done, i remove the ".txt" with string.replace(".txt","")
and change the number to a 4 digit number with numberformat but when i try to convert it to an int i get an error "invalid double..."

so its not working

i also wrote that it on the first post.
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
** Service (starter) Create **
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
** Activity (main) Create, isFirst = false **
** Activity (main) Resume **
start sorting list
item: 0,001
Error occurred on line: 1245 (Main)
java.lang.NumberFormatException: Invalid double: "0,001"
at java.lang.StringToReal.invalidReal(StringToReal.java:63)
at java.lang.StringToReal.initialParse(StringToReal.java:164)
at java.lang.StringToReal.parseDouble(StringToReal.java:282)
at java.lang.Double.parseDouble(Double.java:301)
at anywheresoftware.b4a.debug.RDebugUtils.numberCast(RDebugUtils.java:50)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:697)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:336)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:246)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:134)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:157)
at anywheresoftware.b4a.debug.Debug.delegate(Debug.java:262)
at www.sagital.eightbit.main._sortfilelist(main.java:4168)
at www.sagital.eightbit.main._setframeorder(main.java:1712)
at www.sagital.eightbit.main._resetpixelpanel(main.java:928)
at www.sagital.eightbit.main._activity_resume(main.java:867)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:697)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:339)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:246)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:134)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:153)
at www.sagital.eightbit.main.afterFirstLayout(main.java:108)
at www.sagital.eightbit.main.access$000(main.java:17)
at www.sagital.eightbit.main$WaitForLayout.run(main.java:80)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5832)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
You have a comma in the number, change Numberformat so it doesn't use a thousands indicator.
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
ok now i understand what was the problem when i used numberformat(NR,4,0) it returned 0,001 (i dont know why there was a ,) but if i used 3 instead of 4 it worked
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
Numberformat2 allows you to turn off the number grouping so you could still use 4 digits if you need to.
 
Upvote 0
Top