So I am writing a program for school that handles usernames and passwords. Its supposed to take prompt for user names and passwords for 3 users. Then display the username and asterisk for the length of the password. I have pretty much everything I need including how to print the asterisk for the length of the password on the same line:
//int asterix =password[x].length();
* for (int y=0; y<asterix ;y++){
* System.out.print("*");
* }
*/
My issue is I need to format the output like this:
USER ID PASSWORD
howdyDoodie ***********
batMan ************
barneyRubble ************
So far my code looks like this:
public class test{
/**
*
* @param args
*/
public static void main(String[] args){
String[] user = new String[3];
String[] password = new String[3];
// Prompt for Username and Password and loop 3 times adding to next value in array
for(int x=0; x<=2;x++){
user[x] = JOptionPane.showInputDialog(null,"Enter Username: ");
password[x] = JOptionPane.showInputDialog(null,"Enter Password: ");
// Test number of loops
//System.out.println(x);
}
//Field Names Print
System.out.printf("\n%s\t%10s","Username","Password");
for(int x=0; x<=2;x++){
System.out.printf("\n%s\t%15s",user[x],password[x]);
}
System.exit(0);
}
/*
* //int asterix =password[x].length();
* for (int y=0; y<asterix ;y++){
* System.out.print("*");
* }
*/
} // End of Class
I can’t figure out how to get the asterisk to print out and use the formatting.
You need a nested loop. Move the for loop printing the
asterisk (*)inside the for loop printing username and password for all users.Your loop should look like this. It is not tested, but you can work it around to get desired output.
%-20s\tmeansusernametakes 20 spaces, left-aligned, and add a tab after it.