A cikin duniyar shirye-shirye, yana da mahimmanci a rubuta lambar da ta dace kuma tana iya magance al'amuran da ba zato ba tsammani. ฦayan irin wannan yanayin ba zato ba tsammani ya taso lokacin da muka yi ฦoฦarin tantance ฦimar lamba daga kirtani, kuma kirtani ta ฦunshi haruffa marasa inganci. A cikin wannan labarin, za mu tattauna da gwada kama inji da kuma amfani da shi Integer.parseInt hanyar in Java.
Idan ya zo ga canza kirtani zuwa lamba, Java yana ba mu Integer.parseInt hanya. Duk da haka, wannan hanya na iya jefa a Lambar FormatException idan igiyar ta ฦunshi haruffa waษanda basu da inganci don nau'in bayanan lamba. Anan ne tsarin gwada kamawa ya shigo cikin wasa, yana ba mu damar sarrafa keษanta da alheri da samar da mafita mai dacewa ga matsalar.
Yin amfani da gwada-catch tare da Integer.parseInt
Tsarin kama-karya yana ba mu damar aiwatar da toshe lambar kuma mu kama duk wani keษantacce wanda zai iya faruwa yayin aiwatar da shi. Ta hanyar naษa lambar mu ta Integer.parseInt a cikin shingen gwada kama, za mu iya kama kowane NumberFormatException wanda za a iya jefawa, don haka ba mu damar ษaukar matakin da ya dace dangane da ban.
Anan ga bayanin mataki-mataki na yadda ake amfani da gwada-catch tare da Integer.parseInt:
public class TryCatchExample { public static void main(String[] args) { String numberString = "123a"; int result; try { result = Integer.parseInt(numberString); } catch (NumberFormatException e) { System.out.println("Invalid number format: " + e.getMessage()); result = 0; // default value in case of error } System.out.println("Result: " + result); } }
1. ayyana masu canjin 'numberString' da 'sakamako', inda 'numberString' ke ฦunshe da kirtan da muke son musanyawa, kuma 'sakamako' zai adana ฦimar integer da aka tantance.
2. Kewaye kiran 'Integer.parseInt' a cikin toshe gwadawa.
3. Idan a Lambar FormatException ana jefawa yayin kiran 'Integer.parseInt', kama keษantacce a cikin shingen kama.
4. A cikin katangar kama, buga saฦon kuskure tare da cikakkun bayanai na keษanta kuma saita ฦimar tsoho zuwa madaidaicin 'sakamako'.
5. A ฦarshe, buga ฦimar 'sakamako'.
Fahimtar NumberFormatException
A Lambar FormatException ฦaramin aji ne na RuntimeException kuma ana jefa shi lokacin da aka yi ฦoฦarin canza kirtani zuwa ษaya daga cikin nau'ikan lambobi idan igiyar ba ta da tsari mai dacewa. A cikin lamarin Integer.parseInt, ban da za a jefa idan kirtani ta ฦunshi haruffa waษanda ba su da ingantattun ฦimar lamba (misali, haruffa, haruffa na musamman).
- Ingantacciyar ฦimar lamba yakamata ta ฦunshi lambobi (0-9), da alamar jagorar zaษi (+ ko -).
- Ba a yarda da haruffan farin sararin samaniya a cikin kirtani don tantancewa ba.
- Idan kirtanin ya fara da ingantaccen tsarin lamba amma yana da haruffa marasa inganci daga baya, hanyar za ta jefa NumberFormatException.
Madadin Hanyoyin Watsawa
Bayan amfani da gwada-catch tare da Integer.parseInt, Java kuma yana ba da madadin hanyoyin tantance kirtani zuwa lamba, kamar hanyar `Integer.valueOf` da Java 8's `OptionalInt` tare da qarqashinsu.
// Using Integer.valueOf public static Integer tryParse(String value) { try { return Integer.valueOf(value); } catch (NumberFormatException e) { return null; } } // Using Java 8 OptionalInt and Stream public static OptionalInt tryParseJava8(String value) { return value.chars() .allMatch(Character::isDigit) ? OptionalInt.of(Integer.parseInt(value)) : OptionalInt.empty(); }
A ฦarshe, kulawa da keษancewa da samar da mafita masu dacewa ga matsalolin yana da mahimmanci a cikin shirye-shirye. Hanyar kamawa don sarrafa NumberFormatException wanda Integer.parseInt ya jefa ita ce irin wannan mafita, tabbatar da cewa lambar mu ta kasance mai ฦarfi da aminci ta fuskar ฦimar shigarwar da ba zato ba tsammani.