Reputation: 39
this program is a magic date program (day times month="year as a two digits number") like 06\10\60 ... I want to make a condition ... if the day or the month or the year entered are more or less than 2 digits should display an error message ... I tried this
System.out.println("Please enter the day as a two digit Number");
day=key.nextInt();
day1=day.length();
but it did not work ... so I tried this :
System.out.println("Please enter the month as a two digit Number");
month1=key.next().length();
month=key.nextInt();
but when i run the program ... it requires two inputs not one So help me out
Upvotes: 0
Views: 135
Reputation: 2690
i don't understand that why you are using key but i think that Buffer reader can solve your problem here is the example!
int month=0,length=0;
String len=null;
BufferedReader bf=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Please enter the month as a two digit Number");
len=bf.readLine();
length=len.length();
month=Integer.parseInt(len);
if(length==2)
{
System.out.println("Here you go month is two digit!");
}
else
{
//inform user that month is not 2 digits!
}
Upvotes: 0
Reputation: 9331
The problem is that you are using nextInt()
but you would like two-digit numbers like 06
which is not a valid integer.
It will be better to:
Scan the input as a String. String day = scan.nextLine();
Validate for correct input by using day.matches("\\d{2}")
(This will only match any pairing of [0-9] and don't have to worry about other characters)
If it Validates then you can get your Integer: Integer realDay = Integer.valueOf(day);
Perform further Validation to test the range (1-31 for Days) (1-12 for Months) (1-28 or 1-29 for February depending on the Year) etc.
Upvotes: 1
Reputation: 1
int day=key.nextInt();
String dayString = "";
if(day> 30 || day <0){ System.out.println("Error1");}
else if(day<10 ){ dayString = "0" + String.valueOf(day);}
else{dayString = String.valueOf(day);}
here u have ur day in 2 digits as a string,more easy to manipulate. u can also try Date class (java.util.Date)
Upvotes: 0
Reputation: 734
You can do this simply by converting int to string and then by checking the length of the string.
String s = Integer.toString(day);
if(s.length() <= 2 && !(s.length() < 1)){
System.out.println("Good");
}else
System.out.println("Bad");
Upvotes: 1
Reputation: 53819
Actually, it seems that you want to enforce the length of the string so that the user has to type 06
instead of 6
.
If so:
String dayString = key.next();
if(dayString.length() != 2) {
// throw error
}
int day = Integer.parseInt(dayString);
Upvotes: 0
Reputation: 796
Except using int use string then check the length of the input. This code is only a base. It is stored in result. Then after the check use Integer.parseInt(result);
System.out.println("Please enter two digits:");
Scanner scan = new Scanner(System.in);
String result = scan.next();
if(result.length()>2 || result.length()<2)
System.out.println("ERROR");
else
System.out.println("Success");
Upvotes: 0
Reputation: 670
to test for the number of digits in an int primitive try this
int number = ...;
// or however you get the number
int length = (int)(Math.log10(number)+1);
if(length!=2) {
// no!
} else {
// yuss!
}
Upvotes: 0
Reputation: 53819
Simply:
day = key.nextInt();
int dayAbs = Math.abs(day); // to handle negatives
if(dayAbs >= 10 && dayAbs <= 99) {
// ...
}
Upvotes: 1