Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

JAVA YES-NO REGULAR EXPRESSION

$
0
0
Hi, I tried to change part of my code in the CLI that I was writing.

I had this code for the option yes/no to "reset" the CLI and make another operation.

}
			answer = userInput.nextLine();
			}
			catch(InputMismatchException e){
				System.out.println(e.toString());
			}
			catch(NullPointerException e){
				System.out.println(e.toString());
			}
			catch(Exception e){
				System.out.println(e.toString());
			}
			answer = userInput.nextLine();
			}
			while(answer.equalsIgnoreCase("y") && (!(acceptableuserInput)));
			


The thing is that I wanted to change this code to a REGULAR Expression so It's a shorter code and also if I type Y or N with a space, Lower or Upper case will be detected as correct.

This is the code of the whole CLI as you can see very basic. At the end of the CLI you can see my try to Regular expression. What is wrong there? Why it doesn't work? I'm not even receiving an error it's somewhere in the writing of the code. Thanks again guys.

	import java.util.InputMismatchException;
	import java.util.Scanner;
	import java.util.regex.*; //This is for the regular expression.;
	import java.util.regex.Pattern;
	import java.util.regex.PatternSyntaxException;
	
	
	
	public class menuatmwregularexpression {
		private static final boolean acceptableuserInput = false;
		static Scanner userInput = new Scanner(System.in);
		static int userChoice;
		static String answer = null;	
		boolean acceptableUserInput = false; 
		private Pattern pattern;
	    private Matcher matcher;
		
		
		
		public static void main(String[] args) {
			
				int accountbalance = 1000; //Money in you account
				
				do
				{
				System.out.println("             --**ATM MACHINE**-- \n" ); 
				System.out.println(" -Select your desired operation from the Menu- \n" ); 
				System.out.println("------------------------------------------------- \n" ); 
				System.out.println("[1]:- View account balance.\n" ); 
				System.out.println("[2]:- Withdraw cash.\n" ); 
				System.out.println("[3]:- Make a deposit.\n" ) ; 
				System.out.println("[4]:- Exit.\n" ) ; 
				
				
				try{
					userChoice = userInput.nextInt();
			
				switch(userChoice)
				{
				case 1:
					System.out.println("Your account balance is:");
					System.out.println(accountbalance + "$" );
					System.out.println("Would you like to do another transaction? [Y/N]");
					break;
					
				case 2:
					System.out.println("Please type the amount that you want to withdraw: ");
					int withdrawalAmount = userInput.nextInt(); 
					System.out.println(withdrawalAmount + " $, are being withdrawed.");
					System.out.println("Your balance now is: $ " +(accountbalance - withdrawalAmount) );
					System.out.println("Please take your cash");
					System.out.println("Would you like to do another transaction? [Y/N]");
					break;
					
				case 3:
					System.out.println("Please type the amount that you would like to deposit: ");
					int depositAmount = userInput.nextInt();
					System.out.println(depositAmount + " $, are going to be deposited in your account.");
					System.out.println("Please insert the cash");
					System.out.println("Your balance now is: $ " +(accountbalance + depositAmount) );
					System.out.println("Would you like to do another transaction? [Y/N]");
					break;
					
				case 4:
					System.out.println("Thanks for using ATM. Have a nice day. \n");
					System.exit(userChoice);
					break;
				
				default: 
					System.out.println("Invalid Choice. Please select an option from the menu.");
					return;
				}
				Scanner sc = new Scanner(System.in);
				int YesOrNo;
				do {
			        while (!sc.hasNextInt()) {
			            System.out.println("Please press Y / N");
			            sc.next(); // this is important!
			        }
			        YesOrNo = sc.nextInt();
			    } while (public boolean isYes(String s)){
			        return s.matches("[yY]");
				}
			

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>