java - how to make a nested loop break if string.contains() is correct -
i'm having issues understanding nested loops , behavior. on first loop script asks 10 digit number otherwise keep looping, works fine. on second loop, i'm trying program keep running until users enters "999" anywhere in phone number. have idea i'm not able put together. if user enters 10 digit number doesnt contain 999, keep asking reenter phone number.
import javax.swing.joptionpane; import java.lang.*; public class formatphonenumber { public static void main(string[] args) { final int numlength=10; string phonenum = null; string nines="999"; phonenum=joptionpane.showinputdialog(null, "enter telephone number"); while (phonenum.length()!=numlength) {phonenum=joptionpane.showinputdialog(null, "you must re-enter 10 digits telephone number."); } stringbuffer str1 = new stringbuffer (phonenum); str1.insert(0, '('); str1.insert(4, ')'); str1.insert(8, '-'); joptionpane.showmessagedialog(null, "your telephone number " +str1.tostring()); while (phonenum.contains(nines))// issue { } } }
nesting when 1 loop inside loop. code have provided not have one.
typical example of nesting:
while( some_condition ) { do_something.. while( more_condition ) { do_something_more.. } }
if understand correctly, want keep on entering numbers , them. once user enters number has '999' in it, control has come out of loops.
as pointed out, not need nested loop achieve @ all.
while( phonenumber has 10 digits ) { do_something.. if( phonenumber has '999' anywhere ) { break; } }
Comments
Post a Comment