java - Switch statement not acting correctly -
i'm trying switch statement work.
i ask user's birthday , put in format dd.mm.yyyy. if statement determines if use "." separate or 1 of other symbols such "/", " ". other symbols work fine, enter switch statement if user enters 15/07/1993 work fine , month becomes july. when enter 15.07.1993 goes default case not 7th case.
i assume has got escaped "."
("\\.") this might changing value of it. there way around it? input scanner.
any questions feel free ask.
if(input.contains("\\.")) { string[] tokens = input.split("\\."); day = integer.parseint(tokens[0]); intmonth = integer.parseint(tokens[1]); year = integer.parseint(tokens[2]); } switch(intmonth) { case 1: month = "january"; break; case 2: month = "febuary"; break; case 3: month = "march"; break; case 4: month = "april"; break; case 5: month = "may"; break; case 6: month = "june"; break; case 7: month = "july"; break; case 8: month = "august"; break; case 9: month = "september"; break; case 10: month = "october"; break; case 11: month = "november"; break; case 12: month = "december"; break; default: month = "not valid"; break; }
the problem use regular expression string.contains.
according method documentation, need supply charsequence string.contains, ie. have write if (input.contains(".")) instead of if (input.contains("\\.")).
Comments
Post a Comment