java - input validation using loop -
a loop in class not returning expected value. should return true when value entered in method equal 1 of values in area. when compile , run main method: "please enter id number checked" enter 2 "you entered number id" "565" "456" "342" "234" "237" "192" "237" "109" "278" "num u entered 2 true" "the value entered false" "press key continue . . ."
the "num u entered 2 true" line useless, , used testing. since 2 not equal of elements equal in array, im not sure why remain false.
here class.
public class account { int[] anum = {565, 456, 342, 234, 237, 192, 237, 109, 278}; boolean flag = false; public boolean achecker(int num) { for(int = 0; < anum.length; i++) { if(num == anum[i]); { system.out.println(anum[i]); flag = true; } } system.out.println("num u entered " + num + " " + flag); return flag; } } here main method.
import java.math.*; import java.text.decimalformat; import java.io.*; import java.util.*; public class labbookfiftythree { public static void main(string[] args) { scanner myinput = new scanner(system.in); account myaccounts = new account(); int id = -1; while (id < 0) { system.out.println("please enter id number checked"); if (myinput.hasnextint()) { system.out.println("you entered number id"); id = myinput.nextint(); } else if (myinput.hasnext()) { system.out.println("please enter proper value"); myinput.next(); } else { system.err.println("no more input"); system.exit(1); } } system.out.println("the value entered " + myaccounts.achecker(id)); } }
if(num == anum[i]); { system.out.println(anum[i]); flag = true; } is equivalent to
if(num == anum[i]) /* nothing @ */; { system.out.println(anum[i]); flag = true; } because of semicolon after if, system.out.println , flag = true not part of if statement. therefore performed whether or not if condition true.
delete last semicolon if line.
Comments
Post a Comment