2
package validation;
import java.util.*;

public class Validation {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        String regex = "^[a-zA-Z ]+$";
       String regex1 = "\\d[0-9]|[1-9]";
       String regex2 = "^[a-zA-Z0-9 ]+$";
        String Char;
         String num;
        String chars;
         System.out.println("WELCOME TO Mixed Martial Art's CLUB REGISTRATION");
        do{
            System.out.print("\nWhat Is Your Name:");
            Char = input.nextLine();

            if (Char.matches(regex))
                System.out.println("\nWelcome:"+" "+Char); 
            else if (Char.isEmpty())
                System.out.println("String field should not be Empty.");
            else if(!Char.matches(regex))
                System.out.println("Please Enter A Valid String!");
        } while(!Char.matches(regex));

        do {    
             System.out.print("\nHow Old Are you:");
             num = input.nextLine();
             if (num.isEmpty())
                 System.out.println("Number field should not be Empty.");       
             else if (!num.matches(regex1)){
                System.out.println("Please Enter A Valid Number!");
             } else{
                if(Integer.parseInt(num)<=18){
                   System.out.println("Sorry But You Are Underage.");                

                } else {
                    if (Integer.parseInt(num)<=39) {
                        System.out.println("You Are Qualified");
                    } else {
                        System.out.println("Opps! You Are OVER AGE!");
                    }
                }                     
            }
        }while(!num.matches(regex1));           
    }   
}

The above codes are working as what I expected, but I need to change something regarding the final print output.What I want is to print outputs all at once after I input all the details.

xenteros
  • 15,586
  • 12
  • 56
  • 91
Haider Abdullah
  • 111
  • 2
  • 13
  • then instead of printing everything right away, save them in an array or something then print – RisingSun Nov 29 '16 at 09:34
  • 2
    So you want to concate your messages and print them at the end. You could concatenate Strings (but not perfect) or use `StringBuilder` – AxelH Nov 29 '16 at 09:35
  • exactly I want to print all the result of the input at the end of the end. can you show me how the most efficient way to achieve that? I will really appreciate it. – Haider Abdullah Nov 29 '16 at 09:40
  • See Markus answers. This is exactly what need to be done. You append the instance of `StringBuilder` when needed, then you print the result. – AxelH Nov 29 '16 at 09:43
  • 1
    Take a lot at [this thread](http://stackoverflow.com/questions/1825781/when-to-use-stringbuilder), this is C# but the idea are the same (might not be the same perfs but that is not the point here) and the [comment section here](http://stackoverflow.com/a/4645155/4391450) is interesting – AxelH Nov 29 '16 at 09:47
  • thank you guys I'll update you soon. – Haider Abdullah Nov 29 '16 at 09:52

3 Answers3

3

Use StringBuilder to build your output:

StringBuilder sb = new StringBuilder();
sb.append("Thank you for registering.\n");
sb.append("Please verify your data:\n");
//...

System.out.println(sb.toString());
Markus Mitterauer
  • 1,560
  • 1
  • 14
  • 28
  • Hi, markus thank you for giving your time in sharing your thought about this topic. So how do I add this codes? I know it's a silly question but please try to understand me I'm just a beginner. – Haider Abdullah Nov 29 '16 at 09:51
  • @HaiderAbdullah Sorry, but what do you mean by *"how do I add this codes?"* - You create a new `StringBuilder` instance, `append` the stuff you want to have printed, and when finished print the whole content provided by `sb.toString()`. – Markus Mitterauer Nov 29 '16 at 09:59
1

You can use following code to get output as single string according to your code. Here I have used StringBuilder and append relevant output to it. Then at the end I have print the content of it.

import java.util.Scanner;

public class Validation {
public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    String regex = "^[a-zA-Z ]+$";
    String regex1 = "\\d[0-9]|[1-9]";
    String regex2 = "^[a-zA-Z0-9 ]+$";
    String Char;
    String num;
    String chars;
    System.out.println("WELCOME TO Mixed Martial Art's CLUB REGISTRATION");

    StringBuilder output = new StringBuilder();

    do {

        System.out.print("\nWhat Is Your Name:");
        Char = input.nextLine();

        if (Char.matches(regex))
            // System.out.println("\nWelcome:"+" "+Char);
            output.append("\nWelcome:" + " " + Char);
        else if (Char.isEmpty())
            System.out.println("String field should not be Empty.");
        else if (!Char.matches(regex))
            System.out.println("Please Enter A Valid String!");
    } while (!Char.matches(regex));

    do {

        System.out.print("\nHow Old Are you:");
        num = input.nextLine();
        if (num.isEmpty())
            System.out.println("Number field should not be Empty.");
        else if (!num.matches(regex1)) {
            System.out.println("Please Enter A Valid Number!");
        } else {
            if (Integer.parseInt(num) <= 18) {
                // System.out.println("Sorry But You Are Underage.");
                output.append("\nSorry But You Are Underage.");

            } else {
                if (Integer.parseInt(num) <= 39) {
                    // System.out.println("You Are Qualified");
                    output.append("\nYou Are Qualified");
                } else {
                    // System.out.println("Opps! You Are OVER AGE!");
                    output.append("\nOpps! You Are OVER AGE!");
                }
            }
        }

    } while (!num.matches(regex1));

    System.out.println(output);

}
}
Coder
  • 1,917
  • 3
  • 17
  • 33
0
import java.util.*;

public class Validation {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        String regex = "^[a-zA-Z ]+$";
        String regex1 = "\\d[0-9]|[1-9]";
        String nameChar;
        String num;
        String chars;
        String name = "";
        int age = 0;
        System.out.println("WELCOME TO Mixed Martial Art's CLUB REGISTRATION");

        do {
            System.out.print("\nWhat Is Your Name:");
            nameChar = input.nextLine();
            if (nameChar.isEmpty())
                System.out.println("String field should not be Empty.");
            else if (!nameChar.matches(regex))
                System.out.println("Please Enter A Valid String!");
            else if (nameChar.matches(regex))
                name = nameChar;
        } while (!nameChar.matches(regex));

        do {
            System.out.print("\nHow Old Are you:");
            num = input.nextLine();
            if (num.isEmpty())
                System.out.println("Number field should not be Empty.");
            else if (!num.matches(regex1)) {
                System.out.println("Please Enter A Valid Number!");
            } else {
                try {
                    int ageInput = Integer.parseInt(num);
                    if (ageInput <= 18) {
                        System.out.println("Sorry But You Are Underage.");
                    } else {
                        if (ageInput <= 39) {
                            age = ageInput;
                            break;
                        } else {
                            System.out.println("Opps! You Are OVER AGE!");
                        }
                    }
                } catch (Exception e) {
                }
            }

        } while (!num.matches(regex1));
        if(!name.isEmpty() && age>0){
            System.out.println("Thank you for registering");
            System.out.println("Name is "+name);
            System.out.println("Age is"+age);
            System.out.println("You Are Qualified");
        }

    }
}
Musaddique S
  • 1,539
  • 2
  • 15
  • 36
  • This is very useful but the problem is the it doesn't' show the result of the conditional statement for the age. It doesn't give the output whether it's over age, under age or qualified. – Haider Abdullah Nov 29 '16 at 12:12
  • check once again. previously you had wrong sysout for Qualified – Musaddique S Nov 29 '16 at 12:19