在数字前面填充零,以字母字符串为前缀

时间:2017-03-08 20:46:00

标签: java string number-formatting

我有一个字符串,即AB1234。我正在尝试创建一个方法,我可以将字符串传递到总是在整数前面填充零,以创建一个10位数的字符串。

一些例子:

padZero("AB1234")返回" AB00001234"

padZero("CD001234")返回" CD00001234"

padZero("ABCDEF858")返回" ABCDEF0858"

有没有一种简单的方法可以做到这一点,而不必有大量的角落情况,你必须尝试抓住?假设在方法调用之前捕获了传递给此方法的大于十位的字符串的情况。

3 个答案:

答案 0 :(得分:2)

public static void main(String[] args) {
    System.out.println(padZero("abc123"));
}

public static String padZero(String init) {
    Matcher matcher = Pattern.compile("\\d+").matcher(init);
    matcher.find();
    return String.format("%s%0" + (10-matcher.start()) + "d", init.substring(0, matcher.start()), Integer.valueOf(matcher.group()));
}

答案 1 :(得分:1)

我认为最简单的方法是找到第一个数字然后插入足够的零以填充到10位数字:

null
java.lang.StackOverflowError
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:763)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:642)
    at com.intellij.util.lang.UrlClassLoader._defineClass(UrlClassLoader.java:272)
    at com.intellij.util.lang.UrlClassLoader.defineClass(UrlClassLoader.java:268)
    at com.intellij.util.lang.UrlClassLoader.findClass(UrlClassLoader.java:222)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at com.intellij.openapi.editor.impl.DocumentImpl.getText(DocumentImpl.java:806)
    at com.intellij.notification.EventLog.getStatusText(EventLog.java:279)
    at com.intellij.notification.EventLog.formatForLog(EventLog.java:178)
    at com.intellij.notification.LogModel.addNotification(LogModel.java:57)
    at com.intellij.notification.EventLog$ProjectTracker.printNotification(EventLog.java:507)
    at com.intellij.notification.EventLog$ProjectTracker.access$100(EventLog.java:462)
    at com.intellij.notification.EventLog$ProjectTracker$1.notify(EventLog.java:479)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.intellij.util.messages.impl.MessageBusConnectionImpl.deliverMessage(MessageBusConnectionImpl.java:117)
    at com.intellij.util.messages.impl.MessageBusImpl.doPumpMessages(MessageBusImpl.java:377)
    at com.intellij.util.messages.impl.MessageBusImpl.pumpMessages(MessageBusImpl.java:364)
    at com.intellij.util.messages.impl.MessageBusImpl.pumpMessages(MessageBusImpl.java:350)
    at com.intellij.util.messages.impl.MessageBusImpl.sendMessage(MessageBusImpl.java:343)
    at com.intellij.util.messages.impl.MessageBusImpl.access$200(MessageBusImpl.java:42)
    at com.intellij.util.messages.impl.MessageBusImpl$2.invoke(MessageBusImpl.java:226)
    at com.sun.proxy.$Proxy62.notify(Unknown Source)
    at com.intellij.notification.Notifications$Bus.doNotify(Notifications.java:75)
    at com.intellij.notification.Notifications$Bus.lambda$notify$1(Notifications.java:69)
    at com.intellij.util.ui.UIUtil.invokeLaterIfNeeded(UIUtil.java:2432)
    at com.intellij.notification.Notifications$Bus.notify(Notifications.java:69)
    at com.intellij.notification.Notification.notify(Notification.java:296)
    at com.android.tools.idea.gradle.project.sync.GradleSyncState.addToEventLog(GradleSyncState.java:246)
    at com.android.tools.idea.gradle.project.sync.GradleSyncState.addInfoToEventLog(GradleSyncState.java:242)
    at com.android.tools.idea.gradle.project.sync.GradleSyncState.syncStarted(GradleSyncState.java:160)
    at com.android.tools.idea.gradle.project.sync.GradleSyncState.syncStarted(GradleSyncState.java:146)
    at com.android.tools.idea.gradle.project.sync.GradleSyncInvoker.handlePreSyncCheckFailure(GradleSyncInvoker.java:207)
    at com.android.tools.idea.gradle.project.sync.GradleSyncInvoker.sync(GradleSyncInvoker.java:179)
    at com.android.tools.idea.gradle.project.sync.GradleSyncInvoker.lambda$requestProjectSync$1(GradleSyncInvoker.java:106)
    at com.intellij.ui.AppUIUtil.invokeLaterIfProjectAlive(AppUIUtil.java:97)
    at com.android.tools.idea.gradle.project.sync.GradleSyncInvoker.requestProjectSync(GradleSyncInvoker.java:115)
    at com.android.tools.idea.gradle.project.sync.GradleSyncInvoker.requestProjectSyncAndSourceGeneration(GradleSyncInvoker.java:90)
    at com.android.tools.idea.gradle.sdk.GradleAndroidSdkEventListener.afterSdkPathChange(GradleAndroidSdkEventListener.java:71)
    at com.android.tools.idea.sdk.IdeSdks.afterAndroidSdkPathUpdate(IdeSdks.java:316)
    at com.android.tools.idea.sdk.IdeSdks.setAndroidSdkPath(IdeSdks.java:296)
    at com.android.tools.idea.sdk.IdeSdks.setAndroidSdkPath(IdeSdks.java:224)
    at com.android.tools.idea.gradle.project.sync.SdkSync.lambda$null$0(SdkSync.java:213)
    at com.intellij.openapi.application.impl.ApplicationImpl.runWriteAction(ApplicationImpl.java:898)
    at com.android.tools.idea.gradle.project.sync.SdkSync.lambda$setIdeSdk$1(SdkSync.java:212)
    at com.intellij.util.ui.UIUtil.invokeLaterIfNeeded(UIUtil.java:2432)
    at com.android.tools.idea.gradle.project.sync.SdkSync.setIdeSdk(SdkSync.java:212)
    at com.android.tools.idea.gradle.project.sync.SdkSync.syncIdeAndProjectAndroidSdk(SdkSync.java:126)
    at com.android.tools.idea.gradle.project.sync.SdkSync.syncIdeAndProjectAndroidSdks(SdkSync.java:71)
    at com.android.tools.idea.gradle.project.sync.precheck.AndroidSdkPreSyncCheck.doCheckCanSync(AndroidSdkPreSyncCheck.java:42)
    at com.android.tools.idea.gradle.project.sync.precheck.AndroidStudioSyncCheck.canSync(AndroidStudioSyncCheck.java:30)
    at com.android.tools.idea.gradle.project.sync.precheck.PreSyncChecks.canSync(PreSyncChecks.java:48)
    at com.android.tools.idea.gradle.project.sync.GradleSyncInvoker.sync(GradleSyncInvoker.java:175)
    at com.android.tools.idea.gradle.project.sync.GradleSyncInvoker.lambda$requestProjectSync$1(GradleSyncInvoker.java:106)
    at com.intellij.ui.AppUIUtil.invokeLaterIfProjectAlive(AppUIUtil.java:97)
    at com.android.tools.idea.gradle.project.sync.GradleSyncInvoker.requestProjectSync(GradleSyncInvoker.java:115)
    at com.android.tools.idea.gradle.project.sync.GradleSyncInvoker.requestProjectSyncAndSourceGeneration(GradleSyncInvoker.java:90)
    at com.android.tools.idea.gradle.sdk.GradleAndroidSdkEventListener.afterSdkPathChange(GradleAndroidSdkEventListener.java:71)
    at com.android.tools.idea.sdk.IdeSdks.afterAndroidSdkPathUpdate(IdeSdks.java:316)
    at com.android.tools.idea.sdk.IdeSdks.setAndroidSdkPath(IdeSdks.java:296)
    at com.android.tools.idea.sdk.IdeSdks.setAndroidSdkPath(IdeSdks.java:224)
    at com.android.tools.idea.gradle.project.sync.SdkSync.lambda$null$0(SdkSync.java:213)
    at com.intellij.openapi.application.impl.ApplicationImpl.runWriteAction(ApplicationImpl.java:898)
    at com.android.tools.idea.gradle.project.sync.SdkSync.lambda$setIdeSdk$1(SdkSync.java:212)
    at com.intellij.util.ui.UIUtil.invokeLaterIfNeeded(UIUtil.java:2432)
    at com.android.tools.idea.gradle.project.sync.SdkSync.setIdeSdk(SdkSync.java:212)
    at com.android.tools.idea.gradle.project.sync.SdkSync.syncIdeAndProjectAndroidSdk(SdkSync.java:126)
    at com.android.tools.idea.gradle.project.sync.SdkSync.syncIdeAndProjectAndroidSdks(SdkSync.java:71)
    at com.android.tools.idea.gradle.project.sync.precheck.AndroidSdkPreSyncCheck.doCheckCanSync(AndroidSdkPreSyncCheck.java:42)
    at com.android.tools.idea.gradle.project.sync.precheck.AndroidStudioSyncCheck.canSync(AndroidStudioSyncCheck.java:30)
    at com.android.tools.idea.gradle.project.sync.precheck.PreSyncChecks.canSync(PreSyncChecks.java:48)
    at com.android.tools.idea.gradle.project.sync.GradleSyncInvoker.sync(GradleSyncInvoker.java:175)
    at com.android.tools.idea.gradle.project.sync.GradleSyncInvoker.lambda$requestProjectSync$1(GradleSyncInvoker.java:106)
    at com.intellij.ui.AppUIUtil.invokeLaterIfProjectAlive(AppUIUtil.java:97)
    at com.android.tools.idea.gradle.project.sync.GradleSyncInvoker.requestProjectSync(GradleSyncInvoker.java:115)
    at com.android.tools.idea.gradle.project.sync.GradleSyncInvoker.requestProjectSyncAndSourceGeneration(GradleSyncInvoker.java:90)
    at com.android.tools.idea.gradle.sdk.GradleAndroidSdkEventListener.afterSdkPathChange(GradleAndroidSdkEventListener.java:71)
    at com.android.tools.idea.sdk.IdeSdks.afterAndroidSdkPathUpdate(IdeSdks.java:316)
    at com.android.tools.idea.sdk.IdeSdks.setAndroidSdkPath(IdeSdks.java:296)
    at com.android.tools.idea.sdk.IdeSdks.setAndroidSdkPath(IdeSdks.java:224)
    at com.android.tools.idea.gradle.project.sync.SdkSync.lambda$null$0(SdkSync.java:213)
    at com.intellij.openapi.application.impl.ApplicationImpl.runWriteAction(ApplicationImpl.java:898)
    at com.android.tools.idea.gradle.project.sync.SdkSync.lambda$setIdeSdk$1(SdkSync.java:212)
    at com.intellij.util.ui.UIUtil.invokeLaterIfNeeded(UIUtil.java:2432)
    at com.android.tools.idea.gradle.project.sync.SdkSync.setIdeSdk(SdkSync.java:212)
    at com.android.tools.idea.gradle.project.sync.SdkSync.syncIdeAndProjectAndroidSdk(SdkSync.java:126)
    at com.android.tools.idea.gradle.project.sync.SdkSync.syncIdeAndProjectAndroidSdks(SdkSync.java:71)
    at com.android.tools.idea.gradle.project.sync.precheck.AndroidSdkPreSyncCheck.doCheckCanSync(AndroidSdkPreSyncCheck.java:42)
    at com.android.tools.idea.gradle.project.sync.precheck.AndroidStudioSyncCheck.canSync(AndroidStudioSyncCheck.java:30)
    at com.android.tools.idea.gradle.project.sync.precheck.PreSyncChecks.canSync(PreSyncChecks.java:48)
    at com.android.tools.idea.gradle.project.sync.GradleSyncInvoker.sync(GradleSyncInvoker.java:175)
    at com.android.tools.idea.gradle.project.sync.GradleSyncInvoker.lambda$requestProjectSync$1(GradleSyncInvoker.java:106)
    at com.intellij.ui.AppUIUtil.invokeLaterIfProjectAlive(AppUIUtil.java:97)
    at com.android.tools.idea.gradle.project.sync.GradleSyncInvoker.requestProjectSync(GradleSyncInvoker.java:115)
    at com.android.tools.idea.gradle.project.sync.GradleSyncInvoker.requestProjectSyncAndSourceGeneration(GradleSyncInvoker.java:90)
    at com.android.tools.idea.gradle.sdk.GradleAndroidSdkEventListener.afterSdkPathChange(GradleAndroidSdkEventListener.java:71)
    at com.android.tools.idea.sdk.IdeSdks.afterAndroidSdkPathUpdate(IdeSdks.java:316)
    at com.android.tools.idea.sdk.IdeSdks.setAndroidSdkPath(IdeSdks.java:296)
    at com.android.tools.idea.sdk.IdeSdks.setAndroidSdkPath(IdeSdks.java:224)
    at com.android.tools.idea.gradle.project.sync.SdkSync.lambda$null$0(SdkSync.java:213)
    at com.intellij.openapi.application.impl.ApplicationImpl.runWriteAction(ApplicationImpl.java:898)
    at com.android.tools.idea.gradle.project.sync.SdkSync.lambda$setIdeSdk$1(SdkSync.java:212)
    at com.intellij.util.ui.UIUtil.invokeLaterIfNeeded(UIUtil.java:2432)
    at com.android.tools.idea.gradle.project.sync.SdkSync.setIdeSdk(SdkSync.java:212)
    at com.android.tools.idea.gradle.project.sync.SdkSync.syncIdeAndProjectAndroidSdk(SdkSync.java:126)
    at com.android.tools.idea.gradle.project.sync.SdkSync.syncIdeAndProjectAndroidSdks(SdkSync.java:71)
    at com.android.tools.idea.gradle.project.sync.precheck.AndroidSdkPreSyncCheck.doCheckCanSync(AndroidSdkPreSyncCheck.java:42)
    at com.android.tools.idea.gradle.project.sync.precheck.AndroidStudioSyncCheck.canSync(AndroidStudioSyncCheck.java:30)
    at com.android.tools.idea.gradle.project.sync.precheck.PreSyncChecks.canSync(PreSyncChecks.java:48)
    at com.android.tools.idea.gradle.project.sync.GradleSyncInvoker.sync(GradleSyncInvoker.java:175)
    at com.android.tools.idea.gradle.project.sync.GradleSyncInvoker.lambda$requestProjectSync$1(GradleSyncInvoker.java:106)
    at com.intellij.ui.AppUIUtil.invokeLaterIfProjectAlive(AppUIUtil.java:97)
    at com.android.tools.idea.gradle.project.sync.GradleSyncInvoker.requestProjectSync(GradleSyncInvoker.java:115)
    at com.android.tools.idea.gradle.project.sync.GradleSyncInvoker.requestProjectSyncAndSourceGeneration(GradleSyncInvoker.java:90)
    at com.android.tools.idea.gradle.sdk.GradleAndroidSdkEventListener.afterSdkPathChange(GradleAndroidSdkEventListener.java:71)
    at com.android.tools.idea.sdk.IdeSdks.afterAndroidSdkPathUpdate(IdeSdks.java:316)
    at com.android.tools.idea.sdk.IdeSdks.setAndroidSdkPath(IdeSdks.java:296)
    at com.android.tools.idea.sdk.IdeSdks.setAndroidSdkPath(IdeSdks.java:224)
    at com.android.tools.idea.gradle.project.sync.SdkSync.lambda$null$0(SdkSync.java:213)
    at com.intellij.openapi.application.impl.ApplicationImpl.runWriteAction(ApplicationImpl.java:898)
    at com.android.tools.idea.gradle.project.sync.SdkSync.lambda$setIdeSdk$1(SdkSync.java:212)
    at com.intellij.util.ui.UIUtil.invokeLaterIfNeeded(UIUtil.java:2432)
    at com.android.tools.idea.gradle.project.sync.SdkSync.setIdeSdk(SdkSync.java:212)
    at com.android.tools.idea.gradle.project.sync.SdkSync.syncIdeAndProjectAndroidSdk(SdkSync.java:126)
    at com.android.tools.idea.gradle.project.sync.SdkSync.syncIdeAndProjectAndroidSdks(SdkSync.java:71)
    at com.android.tools.idea.gradle.project.sync.precheck.AndroidSdkPreSyncCheck.doCheckCanSync(AndroidSdkPreSyncCheck.java:42)
    at com.android.tools.idea.gradle.project.sync.precheck.AndroidStudioSyncCheck.canSync(AndroidStudioSyncCheck.java:30)
    at com.android.tools.idea.gradle.project.sync.precheck.PreSyncChecks.canSync(PreSyncChecks.java:48)
    at com.android.tools.idea.gradle.project.sync.GradleSyncInvoker.sync(GradleSyncInvoker.java:175)
    at com.android.tools.idea.gradle.project.sync.GradleSyncInvoker.lambda$requestProjectSync$1(GradleSyncInvoker.java:106)
    at com.intellij.ui.AppUIUtil.invokeLaterIfProjectAlive(AppUIUtil.java:97)
    at com.android.tools.idea.gradle.project.sync.GradleSyncInvoker.requestProjectSync(GradleSyncInvoker.java:115)
    at com.android.tools.idea.gradle.project.sync.GradleSyncInvoker.requestProjectSyncAndSourceGeneration(GradleSyncInvoker.java:90)
    at com.android.tools.idea.gradle.sdk.GradleAndroidSdkEventListener.afterSdkPathChange(GradleAndroidSdkEventListener.java:71)
    at com.android.tools.idea.sdk.IdeSdks.afterAndroidSdkPathUpdate(IdeSdks.java:316)
    at com.android.tools.idea.sdk.IdeSdks.setAndroidSdkPath(IdeSdks.java:296)
    at com.android.tools.idea.sdk.IdeSdks.setAndroidSdkPath(IdeSdks.java:224)
    at com.android.tools.idea.gradle.project.sync.SdkSync.lambda$null$0(SdkSync.java:213)
    at com.intellij.openapi.application.impl.ApplicationImpl.runWriteAction(ApplicationImpl.java:898)
    at com.android.tools.idea.gradle.project.sync.SdkSync.lambda$setIdeSdk$1(SdkSync.java:212)
    at com.intellij.util.ui.UIUtil.invokeLaterIfNeeded(UIUtil.java:2432)
    at com.android.tools.idea.gradle.project.sync.SdkSync.setIdeSdk(SdkSync.java:212)
    at com.android.tools.idea.gradle.project.sync.SdkSync.syncIdeAndProjectAndroidSdk(SdkSync.java:126)
    at com.android.tools.idea.gradle.project.sync.SdkSync.syncIdeAndProjectAndroidSdks(SdkSync.java:71)
    at com.android.tools.idea.gradle.project.sync.precheck.AndroidSdkPreSyncCheck.doCheckCanSync(AndroidSdkPreSyncCheck.java:42)
    at com.android.tools.idea.gradle.project.sync.precheck.AndroidStudioSyncCheck.canSync(AndroidStudioSyncCheck.java:30)
    at com.android.tools.idea.gradle.project.sync.precheck.PreSyncChecks.canSync(PreSyncChecks.java:48)
    at com.android.tools.idea.gradle.project.sync.GradleSyncInvoker.sync(GradleSyncInvoker.java:175)
    at com.android.tools.idea.gradle.project.sync.GradleSyncInvoker.lambda$requestProjectSync$1(GradleSyncInvoker.java:106)
    at com.intellij.ui.AppUIUtil.invokeLaterIfProjectAlive(AppUIUtil.java:97)
    at com.android.tools.idea.gradle.project.sync.GradleSyncInvoker.requestProjectSync(GradleSyncInvoker.java:115)
    at com.android.tools.idea.gradle.project.sync.GradleSyncInvoker.requestProjectSyncAndSourceGeneration(GradleSyncInvoker.java:90)
    at com.android.tools.idea.gradle.sdk.GradleAndroidSdkEventListener.afterSdkPathChange(GradleAndroidSdkEventListener.java:71)
    at com.android.tools.idea.sdk.IdeSdks.afterAndroidSdkPathUpdate(IdeSdks.java:316)
    at com.android.tools.idea.sdk.IdeSdks.setAndroidSdkPath(IdeSdks.java:296)
    at com.android.tools.idea.sdk.IdeSdks.setAndroidSdkPath(IdeSdks.java:224)
    at com.android.tools.idea.gradle.project.sync.SdkSync.lambda$null$0(SdkSync.java:213)
    at com.intellij.openapi.application.impl.ApplicationImpl.runWriteAction(ApplicationImpl.java:898)
    at com.android.tools.idea.gradle.project.sync.SdkSync.lambda$setIdeSdk$1(SdkSync.java:212)
    at com.intellij.util.ui.UIUtil.invokeLaterIfNeeded(UIUtil.java:2432)
    at com.android.tools.idea.gradle.project.sync.SdkSync.setIdeSdk(SdkSync.java:212)
    at com.android.tools.idea.gradle.project.sync.SdkSync.syncIdeAndProjectAndroidSdk(SdkSync.java:126)
    at com.android.tools.idea.gradle.project.sync.SdkSync.syncIdeAndProjectAndroidSdks(SdkSync.java:71)
    at com.android.tools.idea.gradle.project.sync.precheck.AndroidSdkPreSyncCheck.doCheckCanSync(AndroidSdkPreSyncCheck.java:42)
    at com.android.tools.idea.gradle.project.sync.precheck.AndroidStudioSyncCheck.canSync(AndroidStudioSyncCheck.java:30)
    at com.android.tools.idea.gradle.project.sync.precheck.PreSyncChecks.canSync(PreSyncChecks.java:48)
    at com.android.tools.idea.gradle.project.sync.GradleSyncInvoker.sync(GradleSyncInvoker.java:175)
    at com.android.tools.idea.gradle.project.sync.GradleSyncInvoker.lambda$requestProjectSync$1(GradleSyncInvoker.java:106)
    at com.intellij.ui.AppUIUtil.invokeLaterIfProjectAlive(AppUIUtil.java:97)
    at com.android.tools.idea.gradle.project.sync.GradleSyncInvoker.requestProjectSync(GradleSyncInvoker.java:115)
    at com.android.tools.idea.gradle.project.sync.GradleSyncInvoker.requestProjectSyncAndSourceGeneration(GradleSyncInvoker.java:90)
    at com.android.tools.idea.gradle.sdk.GradleAndroidSdkEventListener.afterSdkPathChange(GradleAndroidSdkEventListener.java:71)
    at com.android.tools.idea.sdk.IdeSdks.afterAndroidSdkPathUpdate(IdeSdks.java:316)
    at com.android.tools.idea.sdk.IdeSdks.setAndroidSdkPath(IdeSdks.java:296)
    at com.android.tools.idea.sdk.IdeSdks.setAndroidSdkPath(IdeSdks.java:224)
    at com.android.tools.idea.gradle.project.sync.SdkSync.lambda$null$0(SdkSync.java:213)
    at com.intellij.openapi.application.impl.ApplicationImpl.runWriteAction(ApplicationImpl.java:898)
    at com.android.tools.idea.gradle.project.sync.SdkSync.lambda$setIdeSdk$1(SdkSync.java:212)
    at com.intellij.util.ui.UIUtil.invokeLaterIfNeeded(UIUtil.java:2432)
    at com.android.tools.idea.gradle.project.sync.SdkSync.setIdeSdk(SdkSync.java:212)
    at com.android.tools.idea.gradle.project.sync.SdkSync.syncIdeAndProjectAndroidSdk(SdkSync.java:126)
    at com.android.tools.idea.gradle.project.sync.SdkSync.syncIdeAndProjectAndroidSdks(SdkSync.java:71)
    at com.android.tools.idea.gradle.project.sync.precheck.AndroidSdkPreSyncCheck.doCheckCanSync(AndroidSdkPreSyncCheck.java:42)
    at com.android.tools.idea.gradle.project.sync.precheck.AndroidStudioSyncCheck.canSync(AndroidStudioSyncCheck.java:30)
    at com.android.tools.idea.gradle.project.sync.precheck.PreSyncChecks.canSync(PreSyncChecks.java:48)
    at com.android.tools.idea.gradle.project.sync.GradleSyncInvoker.sync(GradleSyncInvoker.java:175)
    at com.android.tools.idea.gradle.project.sync.GradleSyncInvoker.lambda$requestProjectSync$1(GradleSyncInvoker.java:106)
    at com.intellij.ui.AppUIUtil.invokeLaterIfProjectAlive(AppUIUtil.java:97)
    at com.android.tools.idea.gradle.project.sync.GradleSyncInvoker.requestProjectSync(GradleSyncInvoker.java:115)
    at com.android.tools.idea.gradle.project.sync.GradleSyncInvoker.requestProjectSyncAndSourceGeneration(GradleSyncInvoker.java:90)
    at com.android.tools.idea.gradle.sdk.GradleAndroidSdkEventListener.afterSdkPathChange(GradleAndroidSdkEventListener.java:71)
    at com.android.tools.idea.sdk.IdeSdks.afterAndroidSdkPathUpdate(IdeSdks.java:316)
    at com.android.tools.idea.sdk.IdeSdks.setAndroidSdkPath(IdeSdks.java:296)
    at com.android.tools.idea.sdk.IdeSdks.setAndroidSdkPath(IdeSdks.java:224)
    at com.android.tools.idea.gradle.project.sync.SdkSync.lambda$null$0(SdkSync.java:213)
    at com.intellij.openapi.application.impl.ApplicationImpl.runWriteAction(ApplicationImpl.java:898)
    at com.android.tools.idea.gradle.project.sync.SdkSync.lambda$setIdeSdk$1(SdkSync.java:212)
    at com.intellij.util.ui.UIUtil.invokeLaterIfNeeded(UIUtil.java:2432)
    at com.android.tools.idea.gradle.project.sync.SdkSync.setIdeSdk(SdkSync.java:212)
    at com.android.tools.idea.gradle.project.sync.SdkSync.syncIdeAndProjectAndroidSdk(SdkSync.java:126)
    at com.android.tools.idea.gradle.project.sync.SdkSync.syncIdeAndProjectAndroidSdks(SdkSync.java:71)
    at com.android.tools.idea.gradle.project.sync.precheck.AndroidSdkPreSyncCheck.doCheckCanSync(AndroidSdkPreSyncCheck.java:42)
    at com.android.tools.idea.gradle.project.sync.precheck.AndroidStudioSyncCheck.canSync(AndroidStudioSyncCheck.java:30)
    at com.android.tools.idea.gradle.project.sync.precheck.PreSyncChecks.canSync(PreSyncChecks.java:48)
    at com.android.tools.idea.gradle.project.sync.GradleSyncInvoker.sync(GradleSyncInvoker.java:175)
    at com.android.tools.idea.gradle.project.sync.GradleSyncInvoker.lambda$requestProjectSync$1(GradleSyncInvoker.java:106)
    at com.intellij.ui.AppUIUtil.invokeLaterIfProjectAlive(AppUIUtil.java:97)
    at com.android.tools.idea.gradle.project.sync.GradleSyncInvoker.requestProjectSync(GradleSyncInvoker.java:115)
    at com.android.tools.idea.gradle.project.sync.GradleSyncInvoker.requestProjectSyncAndSourceGeneration(GradleSyncInvoker.java:90)
    at com.android.tools.idea.gradle.sdk.GradleAndroidSdkEventListener.afterSdkPathChange(GradleAndroidSdkEventListener.java:71)
    at com.android.tools.idea.sdk.IdeSdks.afterAndroidSdkPathUpdate(IdeSdks.java:316)
    at com.android.tools.idea.sdk.IdeSdks.setAndroidSdkPath(IdeSdks.java:296)
    at com.android.tools.idea.sdk.IdeSdks.setAndroidSdkPath(IdeSdks.java:224)
    at com.android.tools.idea.gradle.project.sync.SdkSync.lambda$null$0(SdkSync.java:213)
    at com.intellij.openapi.application.impl.ApplicationImpl.runWriteAction(ApplicationImpl.java:898)
    at com.android.tools.idea.gradle.project.sync.SdkSync.lambda$setIdeSdk$1(SdkSync.java:212)
    at com.intellij.util.ui.UIUtil.invokeLaterIfNeeded(UIUtil.java:2432)
    at com.android.tools.idea.gradle.project.sync.SdkSync.setIdeSdk(SdkSync.java:212)
    at com.android.tools.idea.gradle.project.sync.SdkSync.syncIdeAndProjectAndroidSdk(SdkSync.java:126)
    at com.android.tools.idea.gradle.project.sync.SdkSync.syncIdeAndProjectAndroidSdks(SdkSync.java:71)
    at com.android.tools.idea.gradle.project.sync.precheck.AndroidSdkPreSyncCheck.doCheckCanSync(AndroidSdkPreSyncCheck.java:42)
    at com.android.tools.idea.gradle.project.sync.precheck.AndroidStudioSyncCheck.canSync(AndroidStudioSyncCheck.java:30)
    at com.android.tools.idea.gradle.project.sync.precheck.PreSyncChecks.canSync(PreSyncChecks.java:48)
    at com.android.tools.idea.gradle.project.sync.GradleSyncInvoker.sync(GradleSyncInvoker.java:175)
    at com.android.tools.idea.gradle.project.sync.GradleSyncInvoker.lambda$requestProjectSync$1(GradleSyncInvoker.java:106)
    at com.intellij.ui.AppUIUtil.invokeLaterIfProjectAlive(AppUIUtil.java:97)
    at com.android.tools.idea.gradle.project.sync.GradleSyncInvoker.requestProjectSync(GradleSyncInvoker.java:115)
    at com.android.tools.idea.gradle.project.sync.GradleSyncInvoker.requestProjectSyncAndSourceGeneration(GradleSyncInvoker.java:90)
    at com.android.tools.idea.gradle.sdk.GradleAndroidSdkEventListener.afterSdkPathChange(GradleAndroidSdkEventListener.java:71)
    at com.android.tools.idea.sdk.IdeSdks.afterAndroidSdkPathUpdate(IdeSdks.java:316)
    at com.android.tools.idea.sdk.IdeSdks.setAndroidSdkPath(IdeSdks.java:296)
    at com.android.tools.idea.sdk.IdeSdks.setAndroidSdkPath(IdeSdks.java:224)
    at com.android.tools.idea.gradle.project.sync.SdkSync.lambda$null$0(SdkSync.java:213)
    at com.intellij.openapi.application.impl.ApplicationImpl.runWriteAction(ApplicationImpl.java:898)
    at com.android.tools.idea.gradle.project.sync.SdkSync.lambda$setIdeSdk$1(SdkSync.java:212)
    at com.intellij.util.ui.UIUtil.invokeLaterIfNeeded(UIUtil.java:2432)
    at com.android.tools.idea.gradle.project.sync.SdkSync.setIdeSdk(SdkSync.java:212)

答案 2 :(得分:1)

以防您不想做出假设,而是在无效输入字符串上快速失败:

private static final Pattern STRING_FORMAT = Pattern.compile("(\\D+)(\\d+)");

public static final String padZeros(String s) {
    Matcher matcher = STRING_FORMAT.matcher(s);
    if (!matcher.matches() || s.length() > 10)
        throw new IllegalArgumentException("Invalid format");
    char[] result = new char[10];
    Arrays.fill(result, '0');
    String nonDigits = matcher.group(1);
    String digits = matcher.group(2);
    nonDigits.getChars(0, nonDigits.length(), result, 0);
    digits.getChars(0, digits.length(), result, 10 - digits.length());
    return String.valueOf(result);
}