java - Check if the number entered is in array, otherwise add to the array -
the user enter size of array , values. if entered value exists, user must enter different number. can't seem construct proper code check if inserted value exists.
public static void main(string[] args) { string holder="", s; int size; s=joptionpane.showinputdialog("enter size of array"); size= integer.parseint(s); string array1[]= new string[size]; //declared , instantiated array1 (int x=0; x<=array1.length-1;x++) { array1[x]=joptionpane.showinputdialog("enter value array[" +x +"]"); int a=0; if (array1[x].equals(array1[x])){ a=1; joptionpane.showmessagedialog(null, "exists"); } else joptionpane.showmessagedialog(null, "continue"); } (int x=0; x<=array1.length-1;x++) { holder=holder+ "\n"+ array1[x]; } joptionpane.showmessagedialog(null,holder);
how this? checks if value exists, otherwise user needs re-enter number.
public static void main(string[] args) { string holder = "", s; int size; s = joptionpane.showinputdialog("enter size of array"); size = integer.parseint(s); string array1[] = new string[size]; //declared , instantiated array1 (int x = 0; x <= array1.length - 1; x++) { string num = joptionpane.showinputdialog("enter value array[" + x + "]"); if (arrays.aslist(array1).contains(num)) { x = x - 1; joptionpane.showmessagedialog(null, "exists"); } else { array1[x] = num; joptionpane.showmessagedialog(null, "continue"); } } (int x = 0; x <= array1.length - 1; x++) { holder = holder + "\n" + array1[x]; } joptionpane.showmessagedialog(null, holder); }
Comments
Post a Comment