السلام عليكم
قمت بصناعة trim method بس المشكلة عندي انه اريد المستخدم يدخل قيم صحيحةIntegers .
مثلا أذا كان المدخل = 00012334000 فان الناتج يصبح 12334 بدون الاصفار الموجودو في الطرفين اليمين واليسار
اليكم هذا الكود
package trimmethod;
/*
* posudoCode:
* we Have to create the trim method and make it works with integers
* if the integers that we entered has 0at the beggining or 0 at the end then our method will get ride off
* the 0s from the beginning and the end
* Test it and make it wokrs
*/
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
// int[] array= {0,0,0,0,2,1,3,4,5,0,0,0,0};
System.out.println("Please enter your integer: ");
int []array = input.nextInt();
System.out.println("Your integer after it was terminated is "+ trim(array));
}
public static int[] trim(int[] array) {
int start = 0;
while(array[start] == 0 )
start++;
int end =0;
while(array[end] == 0 )
end--;
int[] newArray = new int[end-start+1];
for (int i =start ; i<= end ;i++)
newArray[i-start]= array;
return newArray;
}
}
