java - How to check if each element in a string array is an integer? -
i'm attempting error check determines whether the elements of array integers, i've been stuck long time though. ideas on how started helpful.
scanner scan = new scanner(system.in); system.out.print("please list @ least 1 , 10 integers: "); string integers = scan.nextline(); string[] newarray = integers.split("");
using string made scanner, loop on space-delimited list , check each element integer. if pass, return true; otherwise return false.
credit isinteger check goes determine if string integer in java
public static boolean containsallintegers(string integers){ string[] newarray = integers.split(" "); //loop on array; if value isnt integer return false. (string integer : newarray){ if (!isinteger(integer)){ return false; } } return true; } public static boolean isinteger(string s) { try { integer.parseint(s); } catch(numberformatexception e) { return false; } // got here if didn't return false return true; }
Comments
Post a Comment