java - if statement for first two characters of string -


this question has answer here:

i need if statement. if ticket doesn't begin particular string prompts them "invalid, try again", example of ticket ba200

public class game {    public static void main(string[] args)    {       scanner keyboard = new scanner(system.in);       system.out.println("enter ticket");       string ticket = keyboard.nextline();       if(ticket=("ab"|| "ab" || "ba" || "ba" || "cb" || "cb"))       {          system.out.println("correct");       }       else       {          system.out.println("invalid, try again");       }    } } 

use startswith method:

import java.util.scanner;  public class game { public static void main(string[] args) {     scanner keyboard = new scanner(system.in);     system.out.println("enter ticket");     string ticket = keyboard.nextline();     if(ticket.touppercase().trim().startswith("ab") ||              ticket.touppercase().trim().startswith("ba") ||              ticket.touppercase().trim().startswith("cb") )     {         system.out.println("correct");     }     else     {         system.out.println("invalid, try again");     } } } 

Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -