Wyszukiwanie liczby w tekście - ocena kodu.

0

Napisałem metodę, która ma znajdować pierwszą liczbę typu integer w otrzymanym tekście.
Proszę o ocenę. Czy kod wygląda i działa dobrze ?
Co powinienem zmienić, żeby wyglądało i działało dobrze....

public class MoreUtils {

/*
 * 	The method searches the first positive integer in the text.
 */
	
public static int findInt(String s) {
    	
	int[] arr = new int[10]; //The number 2,147,483,647 is the maximum positive value for integer, it has 10 digits.
	int index = 0;
	
	
	/*
	 * Extracts the first 10 found digits.
	 */
	
	int excludeZero = 1; //Indicates that the first digit greater than zero was found. 1 - exclude zero 0 - join zero.
	for(int i=0;i< s.length();i++) {
		
		
		if((int)s.charAt(i)>=(48+excludeZero)&&(int)s.charAt(i)<=57) {	
			arr[index]=(int)s.charAt(i)-48;
			index++;
			excludeZero=0;
		}
		
		if(index>9) break;
	}
    

	/*
	 * Converts the found digits to a number.
	 */
	
	long n = 0; //For case if the number is greater than 2,147,483,647 we change later to integer.
	int k =index;
	for(int i=0;i<index;i++) {
		int m = 1;
		
		k--;
		for(int j=k;j>0;j--) {
			m=m*10;

		}
		
		n = n+m*arr[i];

	}
	
	return (n>Integer.MAX_VALUE) ? Integer.MAX_VALUE : (int)n;
    	
    }


public static void main(String args[]) {
	
    System.out.println(findInt("a1b2c3m4567M8"));
    System.out.println(findInt("m2223M"));
    System.out.println(findInt("21474836470"));
    System.out.println(findInt(""));
    System.out.println(findInt(" 1"));
    System.out.println(findInt("\\2 \0\1\2 \\"));
    System.out.println(findInt("0000003456789"));
 }

}
2

Zmienne o nazwach n, m, k, arr itd. absolutnie nic nie mówią o swoim przeznaczeniu, a to podstawowy błąd. ;)

4

pare uwag:

  • ta ilosc komentarzy nie bylaby potrzebna gdyby kod byl samopisowy
  • magiczne stale powrzucane w kodzie
  • staraj sie uzywac jdk zamiast uprawiac magie na prymitywach
  • wyniki dzialania sa niespojne w zaleznosci od tego gdzie pojawia sie zero na wejsciu

obczaj:

    public static int findInt(String text) {
        int number = 0;
        for (char digit : text.toCharArray()) {
            if (Character.isDigit(digit)) {
                number *= 10;
                number += Character.getNumericValue(digit);
            } else if (number > 0) { // end of the digits sequence
                return number;
            }
            if (number < 0) { // overflow
                return Integer.MAX_VALUE;
            }
        }
        return number;
    }
0

Dzięki za odpowiedzi. Źle podałem co ma robić ten kod, ma wyszukiwać całą pierwszą liczbę Int, nawet jak jej cyfry są poprzedzielane innymi charami.
Twój kod przez to nie wykonuje poprawnie 1 testu.
System.out.println(findInt("a1b2c3m4567M8")); powinno dawać 12345678. Spróbuję dzisiaj poprawić kod według waszych wytycznych :)

1

skoro tak to mozesz nawet zgrabniej:

    public static int findInt(String text) {
        return text.chars().filter(Character::isDigit).map(Character::getNumericValue).reduce(0, (number, digit) -> number < 0 || number == Integer.MAX_VALUE ? Integer.MAX_VALUE : number * 10 + digit);
    }
0
katelx napisał(a):

skoro tak to mozesz nawet zgrabniej:

    public static int findInt(String text) {
        return text.chars().filter(Character::isDigit).map(Character::getNumericValue).reduce(0, (number, digit) -> number < 0 || number == Integer.MAX_VALUE ? Integer.MAX_VALUE : number * 10 + digit);
    }

Bardzo ładnie, ale dla mnie jakoś za bardzo skomplikowane D będę robił swój zrozumiały dla mnie kod według waszych wcześniejszych wytycznych.
Coś trzeba poprawić w tym kodzie bo ten test nie przechodzi:
System.out.println(findInt("a0b1c0d2e0304050607")); Wyświetla 2147483647, a powinno być 1020304050

0

Po sugestiach zmieniłem kod.
Czy jeszcze można coś zmienić na lepsze, albo czy są jakieś błędy?

public class MoreUtils {

/*
 * 	The method searches the first positive integer in the text.
 *  The first number is found even if the digits are preceded by letters and other characters.
 */

public static int findInt(String text) {
    
	/*
	 * arr - table in which we store the found elements of the integer number
	 */
	
	int[] arr = new int[10]; //The number 2,147,483,647 is the maximum positive value for integer, it has 10 digits.
	int index = 0;
	int number = 0;
	
	/*
	 * Extracts the first 10 found digits.
	 * Converts the found digits to a number.
	 */
	
	int excludFirstZeros = 1; //Indicates that the first digit greater than zero was found. 1 - exclude zero 0 - join zero.
	for(char digit : text.toCharArray()) {
		
		if(Character.isDigit(digit)) {	
			if(Character.getNumericValue(digit)>=0+excludFirstZeros)
			{
				arr[index]=Character.getNumericValue(digit);
				index++;
				excludFirstZeros=0; 
				number *= 10;
	            number += Character.getNumericValue(digit);
			}
			
		}
		
		if(index>9) break;
	}
    
	
	return (number<0) ? Integer.MAX_VALUE : number;
    	
    }

public static void main(String args[]) {
	
	System.out.println(findInt("a1b2c3m4567M8"));
    System.out.println(findInt("m2223M"));
    System.out.println(findInt("21474836470"));
    System.out.println(findInt("31474836470"));
    System.out.println(findInt(""));
    System.out.println(findInt(" 1"));
    System.out.println(findInt("\\2 \0\1\2 \\"));
    System.out.println(findInt("0000003456789"));
    System.out.println(findInt("a0b1c0d2e0304050607"));
 }

}

1

findInt powinieneś podzielić na mniejsze fragmenty, bo wykonuje wszystko na kupę. Do tego w nieco dziwny sposób, bo całość sprowadza się do kilku czynności:

  • szukanie jakiegokolwiek ciągu znaków cyfr (nie ważne jakiej długości),
  • jeśli znaleziono:
    • wypakowanie podciągu ze znakami cyfr,
    • konwersja podciągu na liczbę,
    • jeśli konwersja powiodła się:
      • zwrócenie wyniku konwersji
    • jeśli nie powiodła się:
      • tu sobie wymyśl co robić – wyjątek, null, nie wiem, nie znam się na Javie.

W dalszym ciągu komentujesz co robi dana linijka, więc nadal kod nie jest samoopisowy. No i nadal została zmienna arr, która nic nie mówi o swoim przeznaczeniu.

0

Chyba ten arr, jest mi niepotrzebny D
Po tej zmianie:

public class MoreUtils {

/*
 * 	The method searches the first positive integer in the text.
 *  The first number is found even if the digits are preceded by letters and other characters.
 */

public static int findInt(String text) {
    

	int countDigits = 0; // The number 2,147,483,647 is the maximum positive value for integer, it has 10 digits.
	int number = 0;
	
	/*
	 * Extracts the first 10 found digits.
         * Converts the found digits to a number.
	 */
	
	int excludFirstZeros = 1; //Indicates that the first digit greater than zero was found. 1 - exclude zero 0 - join zero.
	for(char digit : text.toCharArray()) {
		
		if(Character.isDigit(digit)) {	
			if(Character.getNumericValue(digit)>=0+excludFirstZeros)
			{
				countDigits++;
				excludFirstZeros=0; 
				number *= 10;
	            number += Character.getNumericValue(digit);
			}
			
		}
		
		if(countDigits>9) break;
	}
    
	
	return (number<0) ? Integer.MAX_VALUE : number;
    	
    }


public static void main(String args[]) {
	
	System.out.println(findInt("a1b2c3m4567M8"));
    System.out.println(findInt("m2223M"));
    System.out.println(findInt("21474836470"));
    System.out.println(findInt("31474836470"));
    System.out.println(findInt("00"));
    System.out.println(findInt(""));
    System.out.println(findInt(" 1"));
    System.out.println(findInt("\\2 \0\1\2 \\"));
    System.out.println(findInt("0000003456789"));
    System.out.println(findInt("a0b1c0d2e0304050607"));
 }

}
0

Dzięki waszym wytycznym i po lekkich zmianach, mam nadzieję ostateczny kod:

public class MoreUtils {

/*
 * 	The method searches the first positive integer in the text.
 *  The first number is found even if the digits are preceded by letters and other characters.
 */

public static int findInt(String text) {
    
	int countDigits = 0; // The number 2,147,483,647 is the maximum positive value for integer, it has 10 digits.
	int number = 0;
	int previousNumber = 0;
	
	/*
	 * Extracts the first 10 found digits.
	 * Converts the found digits to a number.
	 */
	
	int excludFirstZeros = 1; //Indicates that the first digit greater than zero was found. 1 - exclude zero 0 - join zero.
	for(char digit : text.toCharArray()) {
		
		previousNumber = number;
		if(Character.isDigit(digit)) {	
			if(Character.getNumericValue(digit)>=0+excludFirstZeros)
			{
				countDigits++;
				excludFirstZeros=0; 
				number *= 10;
	            number += Character.getNumericValue(digit);
	            if (number < 0) { // overflow
	                return previousNumber;
	            }
	            
			}
			
		}
		
		if(countDigits>10) return previousNumber;
		
	}
    
	return number;   	
    }




public static void main(String args[]) {
	System.out.println(findInt("a1b2c3m4567M8n9"));
	System.out.println(findInt("a1b2c3m4567M8"));
    System.out.println(findInt("m2223M"));
    System.out.println(findInt("21474836470"));
    System.out.println(findInt("31474836470"));
    System.out.println(findInt("00"));
    System.out.println(findInt(""));
    System.out.println(findInt(" 1"));
    System.out.println(findInt("\\2 \0\1\2 \\"));
    System.out.println(findInt("0000003456789"));
    System.out.println(findInt("a0b1c0d2e0304050607"));
 }

}
1

twoj nowy kod daje zle wyniki (tzn ucina za dlugie liczby).
nie wiem co chcesz osiagnac robiac to Character.getNumericValue(digit)>=0+excludFirstZeros :)
po co tyle tych zmiennych tworzysz i zmieniasz, zamiast po prostu zsumowac wszystkie cyfry mnozac przez 10 i sprawdzic czy masz przepelnienie, t.j:

    public static int findInt(String text) {
        int number = 0;
        for (char digit : text.toCharArray()) {
            if (Character.isDigit(digit)) {
                number = number * 10 + Character.getNumericValue(digit);
            }
            if (number < 0) {
                return Integer.MAX_VALUE;
            }
        }
        return number;
    }
0

Nie prościej wyciągnąć to wyrażeniami regularnymi? :-)

0

Chętnie się dowiem jakie wyrażenie regularne wyciągnie pierwszego int-a z tekstu...
Jak na razie mam taki kod zrobiłem jednego if-a wykrywającego przekroczenie maksymalnej wartości integer:

public class MoreUtils {

/*
 * 	The method searches the first positive integer in the text.
 *  The first number is found even if the digits are preceded by letters and other characters.
 */

	
public static int findInt(String text) {
    
	int countDigits = 0; // The number 2,147,483,647 is the maximum positive value for integer, it has 10 digits.
	int number = 0;
	int previousNumber = 0;
	
	/*
	 * Extracts the first 10 found digits.
	 * Converts the found digits to a number.
	 */
	
	int excludFirstZeros = 1; //Indicates that the first digit greater than zero was found. 1 - exclude zero 0 - join zero.
	for(char digit : text.toCharArray()) {
		
		previousNumber = number;
		if(Character.isDigit(digit)) {	
			if(Character.getNumericValue(digit)>=0+excludFirstZeros)
			{
				countDigits++;
				excludFirstZeros=0; 
				number *= 10;
	            number += Character.getNumericValue(digit);
	            if (number < 0 || countDigits > 10) { // overflow
	                return previousNumber;
	            }     
			}	
		}	
	}
    	
	return number;
    	
    }

//Test
public static void main(String args[]) {
	
	System.out.println(findInt("a1b2c3m4567M8n9"));
	System.out.println(findInt("a1b2c3m4567M8"));
    System.out.println(findInt("m2223M"));
    System.out.println(findInt("21474836470"));
    System.out.println(findInt("31474836470"));
    System.out.println(findInt("00"));
    System.out.println(findInt(""));
    System.out.println(findInt(" 1"));
    System.out.println(findInt("\\2 \0\1\2 \\"));
    System.out.println(findInt("0000003456789"));
    System.out.println(findInt("a0b1c0d2e0304050607"));
 }

}
1

nie wiem po co dalej cudujesz z customowym wycinaniem zer i zakragleniami (i wprowadzasz nowe bugi) skoro dostales dzialajace rozwiazania.

rozwiazanie z regexem (obsluge bledow sobie dodaj):

public static int findInt(String text) {
    return Integer.valueOf(text.replaceAll("\\D", ""));
}
0

Tak jak pisałem w komentarzach, tylko to rozwiazanie wykonuje poprawnie wszystkie testy. Spełnia założenia o wyszukaniu pierwszej liczby typu int , przy tym nie daje żadnych błędów.

0
Deltech napisał(a):

Nie prościej wyciągnąć to wyrażeniami regularnymi? :-)

Napisałem z wyrażeniami regularnymi, tak jak najlepiej potrafię na chwilę obecną.
Jeżeli ktoś może pomóc jak to skrócić i lepiej napisać tak żeby spełniało założenia i testy, to bardzo proszę...

import java.util.*;
import java.util.regex.*;

public class MoreUtilsWithRegex {
	
	    public static void main(String[] args) {
	       

	        Scanner sc = new Scanner(System.in); 
	        String text = sc.nextLine();   
	        sc.close();
	        
	        //replece all non digit characters
	        text = text.replaceAll("\\D", "");
	        
	        //eliminates all zeros that precede the right number
	        Pattern p = Pattern.compile("(?:[0]+)([0-9]+)");
	        Matcher m = p.matcher(text);
	        if(m.find())
	        {
	        	text = m.group(1);
	        }
	        
	        String sNumber = "";
	    	String previousNumber = "0";
	    	
	        p = Pattern.compile("([0-9])");
	        m = p.matcher(text);
	        while(m.find()&&sNumber.length()<10) {

	        	previousNumber = sNumber;
	        	sNumber = sNumber+(m.group(0));
	        }
	        
	        int  number=0;
	        
	        try {
	        	
	        	number = Integer.parseInt(sNumber);
	        	
	        } catch (NumberFormatException e) {

	        	number = Integer.parseInt(previousNumber);
	        }
	        
	        System.out.println(number);
	    }

}

1 użytkowników online, w tym zalogowanych: 0, gości: 1