An warware: java gwada kama integer.parseint

gwada kama lamba.parseint 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.

Shafi posts:

Leave a Comment