運行此代碼時,我收到此 java.lang.NumberFormatException:對于輸入字串:“5.3”例外。我們如何解決這種型別的例外。任何其他方式我們可以在不嘗試和捕獲的情況下處理這個問題?值可以是任何東西。它始終是浮點數并不是固定的。它也可以是整數和雙精度數。有什么方法可以處理所有這三個,無論發生什么。不使用 try catch。
在第 79 行傳遞這個 PreferenceConstants.DEVICE_LAST_ERROR_CODE
public static final String DEVICE_LAST_ERROR_CODE = "device_last_error_code";
請在下面找到代碼。
package com.pds.ftp.view.activity;
import android.content.res.Configuration;
import android.os.Bundle;
import androidx.annotation.NonNull;
import com.pds.ftp.R;
import com.pds.ftp.app.FTPApp;
import com.pds.ftp.constant.UIConstant;
import com.pds.ftp.infrastructure.ReaderUsbInterf;
import com.pds.ftp.middleware.FTPPrivilegedServiceProvider;
import com.pds.ftp.middleware.PollingLooperThread;
import com.pds.ftp.middleware.ShutDownManager;
import com.pds.ftp.model.dbsync.DBSynchronizationPool;
import com.pds.ftp.transaction.shutdown.EntryExitCheck;
import com.pds.ftp.transaction.shutdown.ShutdownStateCache;
import com.pds.ftp.utils.Util;
import com.pds.hardwareadapter.ingenicoreader.IngenicoReaderDirect;
import com.pds.infrastructure.constants.PreferenceConstants;
import com.pds.infrastructure.interfaces.ILogOperation;
import com.pds.infrastructure.logger.LogCriticalEvent;
import com.pds.infrastructure.logger.LogInfoEvent;
import com.pds.infrastructure.logger.LogWarningEvent;
import javax.inject.Inject;
public class PowerOffActivity extends BaseActivity {
public static final String TYPE = "action_type";
public static final int TYPE_SHUTDOWN = 0;
public static final int TYPE_RESTART = 1;
public static final int TYPE_END_OF_BUSINESS_HOURS = 2;
public static final int SHUTDOWN_DELAY = 1000 * 30;
public static final int SHUTDOWN_WAIT = 1000;
public static final int READER_REBOOT = 3;
@Inject
ILogOperation logOperation;
@Inject
EntryExitCheck entryExitCheck;
@Inject
ShutdownStateCache shutdownStateCache;
@Inject
IngenicoReaderDirect ingenicoReaderDirect;
@Inject
PollingLooperThread pollingLooperThread;
@Inject
ReaderUsbInterf usbInterf;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
isShutDownRequestInitiated = true;
setContentView(R.layout.activity_power_off);
shutdownStateCache.setShutdownState(true); //Shutdown state is true as power is disconnected
logOperation.logInfo(LogInfoEvent.GENERAL_INFO, "PowerOff Screen Started");
}
boolean keepOnRunning = true;
private void checkForPendingTaskBeforeShutdown() {
keepOnRunning = true;
long startTime = System.currentTimeMillis();
new Thread(new Runnable() {
@Override
public void run() {
pollingLooperThread.onpause();
if (getIntent() != null) {
int type = getIntent().getIntExtra(TYPE, 0);
int currentErrorCode = Integer.parseInt(configParams.getString(PreferenceConstants.DEVICE_LAST_ERROR_CODE));
int restartCount = configParams.getInt(PreferenceConstants.PREF_FTP_RESTART_COUNT);
if (type == TYPE_END_OF_BUSINESS_HOURS || (currentErrorCode == LogCriticalEvent.CARD_READER_MALFUNCTION && restartCount == 1)) {
logOperation.logInfo(LogInfoEvent.GENERAL_INFO, "Calling reader reset command due to EOB or CARD_READER_MALFUNCTION. currentErrorCode:: " currentErrorCode);
try {
Thread.sleep(3500);
} catch (Exception e) {
logOperation.logVerbose(LogInfoEvent.GENERAL_INFO, e,"Exception caused in Thread.sleep during reset reader");
}
ingenicoReaderDirect.resetReader();
}
} else{
logOperation.logInfo(LogInfoEvent.GENERAL_INFO, "getIntent() is null");
}
while (keepOnRunning) {
if (entryExitCheck.getShutDownDBTaskCounter() == 0) {
logOperation.logVerbose(LogInfoEvent.GENERAL_INFO, "All threads are completed");
keepOnRunning = false;
} else {
if ((System.currentTimeMillis() - startTime) >= SHUTDOWN_DELAY) {
logOperation.logVerbose(LogInfoEvent.GENERAL_INFO, "Time exceeds 30 sec,initiating shutdown");
keepOnRunning = false;
} else {
logOperation.logVerbose(LogInfoEvent.GENERAL_INFO, "Threads still running");
}
}
try {
Thread.sleep(SHUTDOWN_WAIT);
} catch (Exception e) {
logOperation.logInfo(LogInfoEvent.GENERAL_INFO,e,"checkForPendingTaskBeforeShutdown Exception");
}
}
performShutdownProcess();
}
}).start();
}
@Override
protected void onResume() {
super.onResume();
checkForPendingTaskBeforeShutdown();
}
private void performShutdownProcess() {
if (!Util.shutdown_file_available()) {
FTPApp.isShuttingDownState = true;
Util.setLastShutDownDateTime(configParams);
if (getIntent() != null) {
int type = getIntent().getIntExtra(TYPE, 0);
String state = getIntent().getStringExtra(UIConstant.UISTATE);
if (type == TYPE_END_OF_BUSINESS_HOURS) {
logOperation.logWarning(LogWarningEvent.DEVICE_SCHEDULED_OUT_OF_SERVICE, 2, "Device restarting because of end of business hours");
}
/*if (state.contains(UIConstant.IGNITION_OFF)) {
FTPApp.isShuttingDownState = true;
}*/
if (type == TYPE_RESTART || type == TYPE_END_OF_BUSINESS_HOURS|| type == READER_REBOOT) {
logOperation.logWarning(LogWarningEvent.DEVICE_REBOOTED, "Device is RESTARTING.. Coming from:- " state);
} else {
logOperation.logWarning(LogWarningEvent.DEVICE_SHUT_DOWN, "Device is shutting down Reason:- IGNITION... Coming From:- " state);
}
} else {
logOperation.logWarning(LogWarningEvent.DEVICE_REBOOTED, "Device is RESTARTING. Reason:- OTHER");
}
usbInterf.close();
ShutDownManager.getInstance().shutDownDevice();
}
}
@Override
void prepareTextToSpeechAndSpeak() {
}
@Override
public void onConfigurationChanged(@NonNull Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}
}
在日志中獲取它看起來這個例外來自第 79 行
4,0,05.16.2022_09.09.00.822,java.lang.NumberFormatException: For input string: "5.3"
java.lang.Integer.parseInt(Integer.java:608)
java.lang.Integer.parseInt(Integer.java:643)
com.pds.ftp.view.activity.PowerOffActivity$1.run(PowerOffActivity.java:79)
java.lang.Thread.run(Thread.java:764)
Line 8922: 2,1000,05.16.2022_09.09.00.910,Info,Current Error Code = 0
uj5u.com熱心網友回復:
任何其他方式我們可以在不嘗試捕獲的情況下處理這個問題?
您需要使用正確的決議方法對其進行決議,以免引發例外。
價值可以是任何東西。
好吧,如果這確實是真的,則意味著您無法決議它,除非是最瑣碎的意義。
它始終是浮點數并不固定。它也可以是整數和雙精度數。
啊...所以當您說“任何事情”時,您沒有任何意思嗎?
有三種可能的方法來處理這個問題:
只需將錯誤代碼作為字串讀取,不要嘗試決議它。
問問自己:您真的需要將錯誤代碼視為數字嗎?那給你買什么?如果錯誤代碼根本不是一個可識別的數字怎么辦?(您的應用程式能夠應付嗎?)
使用 決議錯誤代碼
parseDouble,并將其表示為double. 決議的語法parseDouble與 for 相同,是 .parseFloat所接受內容的超集parseInt。具體來說,parseDouble將愉快地決議一個整數值......并為您double提供該整數的表示。缺點是對于足夠大的整數,
double表示將不精確/不準確。根據實際的代碼值,這可能會導致問題。如果你必須用正確的型別來決議它,那么你避免 try / catch 的方法是使用
Pattern實??現正則運算式來測驗錯誤代碼的預期格式。然后呼叫相應的parseXXX方法來決議它。缺點是您現在需要三個(或更多)不同型別
int的變數float來double表示決議的錯誤代碼值。并且您稍后在應用程式中使用錯誤代碼的所有地方都必須處理它。當然,您可以只使用序列嘗試捕獲。
uj5u.com熱心網友回復:
有問題的行是
int currentErrorCode = Integer.parseInt(configParams.getString(PreferenceConstants.DEVICE_LAST_ERROR_CODE));
這決議一個整數,而不是一個浮點數;它需要一個整數的字串表示。5.3不是整數并且包含小數點,導致NumberFormatException. 使用Float.parseFloatorDouble.parseDouble代替;根據您的用例,您可能希望將結果四舍五入為 int。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/481341.html
