Click here to Skip to main content
15,907,906 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How to equalsIgnoreCase in the code. I want user to input ticket is ignore case.And how to make valid ticket between 5-7. Such as AB1234 is valid, asdfds123 is invalid.





import java.util.*;
public class Ticket
{
public static void main (String[] args)
{
Scanner keyboard = new Scanner(System.in);
boolean valid=false;
while(!valid)
{
valid = true;
System.out.print("Enter ticket >> ");
String ticket =keyboard.nextLine();

if((ticket.charAt(0)!='A')&&( ticket.charAt(1)!='B'))
{
valid = false;
System.out.println("First character must be one of 'A', 'B' or'C'");
}
}
}
}

What I have tried:

use equalsIgnoreCase

ticket = ticket.equalsIgnore(); is not working
Posted
Updated 24-Aug-17 17:18pm
Comments
Richard MacCutchan 21-Aug-17 10:42am    
If the first character must be one of "ABC" then you should not be using equalsignorecase.
Mohibur Rashid 21-Aug-17 22:34pm    
Try

if (ticket.charAt(0)>='A' && ticket.charAt(0)<='C' || ticket.charAt(0)>='a' && ticket.charAt(0)<='c')

1 solution

Use String.matches(). Something like this:
boolean isValid = ticket.matches("^[ABC]{2}.*$");
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900