I am using PrintWriter to write the input from the standard input to a file, here I am trying to create an ascii table in the text file, but there is no proper alignment of the table in the text file. Here is my code guys.
package code.practise.MyIdeas;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;
public class WriteTextFile {
public static void main(String[] args) throws FileNotFoundException {
Scanner input = new Scanner(System.in);
String name, email;
final String EMAIL_PATTERN =
"^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@"
+ "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
PrintWriter printwriter = new PrintWriter("User Details.txt");
printwriter.println("+-----\t+-----\t+-----\t+");
printwriter.println("|sno\t|Name\t|email\t|");
printwriter.println("+-----\t+-----\t+-----\t+");
try {
A:for(int i = 1;;i++){
System.out.println("Please enter the name:");
System.out.println("You can quit by pressing enter key...");
name = input.nextLine();
System.out.println("Please enter the email:");
email = input.nextLine();
if(!email.matches(EMAIL_PATTERN)){
System.out.println("Please enter a valid email.\nThe details are not saved please start from the beginning.");
i--;
continue;
}
else
{
printwriter.println("|"+i+")\t|"+name+"\t|"+email+"\t|");
printwriter.println("+-----\t+-----\t+-----\t+");
}
for(;;){
if(i>=1){
System.out.println("Do you wish to continue ?(y/n)");
String quit = input.nextLine();
if(quit.equalsIgnoreCase("n"))break A;
else if (quit.equalsIgnoreCase("y")){
continue A;
}
else
{
System.out.println("Please enter either 'y' or 'n'");
continue;
}
}
}
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
finally {
input.close();
printwriter.close();
}
}
}
And I am also attaching the created text file.
Thanks
Varun Krishna. P