有没有办法确保某个字符在字符串中?

时间:2019-11-07 19:58:53

标签: java

我的计算机科学课上有一个BYOL(建立自己的实验室)。该实验将于3天之内完成,我需要为其中一部分提供一些帮助。我想知道如何确定用户是否输入了特殊字符(@,#,!,*,$,c,%等),并且我想知道如何在不包含该字符的情况下关闭字符串字符串。当用户输入的格式不正确时,我使用if ... then循环关闭字符串。 (例如:我要求他们输入他们的SSN,所以它的格式必须为XXX-XX-XXXX,否则它将关闭字符串。)我需要询问他们的电子邮件地址,并希望答案中包含“ @”。感谢所有帮助。

这是我的代码,因为我不知道如何单独发布它。

public static void main (String[]args)
   {
   String answer;
   do {
      Scanner word=new Scanner(System.in);
      Scanner input=new Scanner(System.in);
      System.out.print("Oh No! It appears that Dwayne \"The Rock\" Johnson has stolen your Credit Card and SSN Info!");
      System.out.print("\nPlease input some information for us to secure your personal Info.");
      System.out.print("\n\nPlease enter your Social Security Number: (XXX-XX-XXXX) ");
      System.out.print("\nPlease be aware that you must input the information in the correct format (Without the parentheses at the begining and end) ");
      String ssn=input.nextLine();
      if (ssn.length()!=11)
         System.out.println("\nYou have entered the incorrect format! (XXX-XX-XXXX)" );
      if (ssn.length()!=11)
         System.exit(0);

      System.out.print("\nPlease enter your Credit Card Number:(XXXX-XXXX-XXXX-XXXX) ");
      String ccn=input.nextLine();
      if (ccn.length()!=19)   
         System.out.println("\nYou have entered the incorrect format! (XXXX-XXXX-XXXX-XXXX)");
      if (ccn.length()!=19)
         System.exit(0);

      System.out.print("\nPlease enter your Credit Card Expiration Date: (MM/YY) ");
      String exp=word.nextLine();    

      System.out.print("\nPlease enter your CVV (Card Verification Value): (XXX) ");
      String back=word.nextLine();
      if (back.length()!=3)
         System.out.println("\nYou have entered the incorrect format! (XXX)");
      if (back.length()!=3)
         System.exit(0);    

      System.out.print("\nPlease enter your E-Mail Address: ");
      String mail=word.nextLine();

      System.out.print("\nPlease enter your American Phone Number: (+1(XXX) XXX-XXXX) ");
      String phone=word.nextLine();
      if (phone.length()!=16)
         System.out.println("\nYou have entered the incorrect format! (+1(XXX) XXX-XXXX)");
      if (phone.length()!=16)
         System.exit(0);    

      System.out.print("\nPlease enter your home address: ");
      String home=word.nextLine();

      System.out.print("\nPlease enter your First Name: ");
      String name=word.nextLine();

      System.out.print("\nPlease enter your Middle Initial (X): ");
      String mid=word.nextLine();
      if (mid.length()!=1)
         System.out.println("\nYou have entered the incorrect format! (X)");
      if (mid.length()!=1)
         System.exit(0);

      System.out.print("\nPlease enter your Last Name: ");
      String last=word.nextLine();

      System.out.print("\nPlease enter your Date of Birth: (MM/DD/YYYY) ");
      String dob=word.nextLine();
      if (dob.length()!=10)
         System.out.println("\nYou have entered the incorrect format! (MM/DD/YYYY)");
      if (dob.length()!=10)
         System.exit(0);    

      System.out.println("\n\n\t\t\t\tThank you, you have secured your information");
      System.out.println("\nIf you have any comments or concerns, please contact us at these locations:");
      System.out.println("\n\n\t+1(555) 555-5555");
      System.out.println("\n\tgoogleuser@gmail.com");
      System.out.println("\n\n\t\t\t\t\t\tThank you, and have a wonderful day!");
      System.out.println("\n\t\t\t\t\t\t\tThe Scammer Preventer™ Team");
      System.out.println("\n\n\n\t\t\t\t\tDo you have more information to input? Yes or No: ");
      answer=input.next();
      }
      while (answer.equalsIgnoreCase("\n\nYes"));

   }
}

2 个答案:

答案 0 :(得分:1)

if (!mail.contains("@")) {
  System.exit(0);
}

此答案可能也有帮助:https://stackoverflow.com/a/1795436/1317559

答案 1 :(得分:1)

使用regex检查字符。

您可以找到一种检查特殊字符here

的解决方案

您可以找到如何操作字符串here