as I am learning java I am also having troubles doing the things I want my program to do. I am watching thenewboston to learn, however, he teach me everything, i.e. how to format user input.
This is my code so far.
import java.util.Scanner;
public class FitnessExtreme {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("Welcome to FitnessExtreme Centre");
System.out.println(" XX XX");
System.out.println(" XX XX");
System.out.println(" XXXX");
System.out.println(" XX XX");
System.out.println(" XX XX");
int membership = 299;
double tax = 1.25;
double total = membership*tax;
// Scans for user input
Scanner scan = new Scanner(System.in);
// Retrieves customer name
System.out.println("First name: ");
String fname = scan.next();
System.out.println("Last name: ");
String lname = scan.next();
// Retrieves CPR number
System.out.println("CPR number: ");
String cpr = scan.next();
// Validate CPR format
if(cpr.length() != 11) {
System.out.println("Format was not met, please try again");
System.out.println("CPR number: ");
cpr = scan.next();
}
// Retrieves Telephone number
System.out.println("Telephone number: ");
String phone = scan.next();
// Retrieves Address
System.out.println("House number: ");
String hnumber = scan.next();
System.out.println("Street name: ");
String sname = scan.next();
System.out.println("Post code: ");
String pcode = scan.next();
// Customer print out
System.out.println("Thank you " + fname + " " + lname);
System.out.println("I have registered following information on you: ");
System.out.println("CPR number: " + cpr);
System.out.println("Telephone number: " + phone);
System.out.println("House number: " + hnumber);
System.out.println("Street name: " + sname);
System.out.println("Post code " + pcode);
// Continue?
System.out.println("");
System.out.println("If this information is correct and you want to continue, please enter 'yes'");
String verify = scan.next();
if(verify.equals("yes")) {
System.out.println("Alright " + fname + " your price is gonna be:");
System.out.println("Membership subscription - subtotal: " + membership);
System.out.println("VAT " + tax);
System.out.println("Total: " + total);
}
}
}
So i need to format the user input "cpr", i need to to be in a ######-#### format. I got the length part, however, if the user doesn't fulfill the requirement regarding the format, I want it to say please try again, like I did with cpr. I found multiple guides on the format on Google but I couldn't find one that does the formatting the way I want it.
Who better to ask than you guys! Also if you have tips on how i can improve my program please share.
To a beginner, I would suggest not the "shortest" solution, but one that is easy to understand.
You say you want this format ######-####; in other words: 6 digits, dash, 4 digits.
You can build a check for that like this:
To check if a char is a digit, you can use Character.isDigit().
When that works, you can pull that checking into a single regular expression; and experiment with that.
But as said; a beginner should start by doing such things manually. Understand what is going on; and only then; move on and look into more powerful, but also more complex solutions.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With