I have a long string and everything is right, but I am unable to get the same output as the prof. He has a more uniform output. So first I tried to do a system.out.println(), but that did not work.
This is my code:
System.out.println("Password = " + password + "\t\t" + "Newline = " + ctnline + "\t" + "Single = " + ctlength + "\t" + "Equal = " + ctequal + "\t" + "Lenght = " + ctlength2 + "\t" + "Upper = " + ctupper + "\t" + "Lower = " + ctlower + "\t" + "Special = " + (ctspec2));
This was the output:
Password = bank,Rabbither Newline = 0 Single = 2 Equal = 0 Lenght = 1 Upper = 2 Lower = 0 Special = 1
Password = acrossShemoment, Newline = 0 Single = 3 Equal = 0 Lenght = 2 Upper = 9 Lower = 0 Special = 0
Password = her.theyOWN Newline = 0 Single = 6 Equal = 0 Lenght = 3 Upper = 9 Lower = 0 Special = 1
Password = ALICEeyes.said Newline = 0 Single = 0 Equal = 0 Lenght = 0 Upper = 0 Lower = 0 Special = 0
Password = thepackKing. Newline = 0 Single = 1 Equal = 0 Lenght = 3 Upper = 4 Lower = 0 Special = 1
Password = thethatKing. Newline = 0 Single = 1 Equal = 0 Lenght = 0 Upper = 2 Lower = 0 Special = 0
Password = AfterWhichdear! Newline = 0 Single = 1 Equal = 0 Lenght = 1 Upper = 3 Lower = 0 Special = 0
Password = "Silencethecrown Newline = 0 Single = 1 Equal = 1 Lenght = 6 Upper = 8 Lower = 0 Special = 0
Password = "Silencemuchme Newline = 0 Single = 6 Equal = 0 Lenght = 1 Upper = 6 Lower = 0 Special = 2
Password = King.nopeeped Newline = 0 Single = 5 Equal = 0 Lenght = 2 Upper = 9 Lower = 0 Special = 2
Password = eitherKing.Soon Newline = 0 Single = 0 Equal = 0 Lenght = 0 Upper = 0 Lower = 0 Special = 0
That did not work, the teacher has an output where the "newline" is aligned together.
I need the output to be like this (how the teacher has it):
Password = bank,Rabbither Newline = 0 Single = 2 Equal = 0 Lenght = 1 Upper = 2 Lower = 0 Special = 1
Password = acrossShemoment, Newline = 0 Single = 3 Equal = 0 Lenght = 2 Upper = 9 Lower = 0 Special = 0
Password = her.theyOWN Newline = 0 Single = 6 Equal = 0 Lenght = 3 Upper = 9 Lower = 0 Special = 1
Password = ALICEeyes.said Newline = 0 Single = 0 Equal = 0 Lenght = 0 Upper = 0 Lower = 0 Special = 0
Password = thepackKing. Newline = 0 Single = 1 Equal = 0 Lenght = 3 Upper = 4 Lower = 0 Special = 1
Password = thethatKing. Newline = 0 Single = 1 Equal = 0 Lenght = 0 Upper = 2 Lower = 0 Special = 0
Password = AfterWhichdear! Newline = 0 Single = 1 Equal = 0 Lenght = 1 Upper = 3 Lower = 0 Special = 0
Password = "Silencethecrown Newline = 0 Single = 1 Equal = 1 Lenght = 6 Upper = 8 Lower = 0 Special = 0
Password = "Silencemuchme Newline = 0 Single = 6 Equal = 0 Lenght = 1 Upper = 6 Lower = 0 Special = 2
Password = King.nopeeped Newline = 0 Single = 5 Equal = 0 Lenght = 2 Upper = 9 Lower = 0 Special = 2
Password = eitherKing.Soon Newline = 0 Single = 0 Equal = 0 Lenght = 0 Upper = 0 Lower = 0 Special = 0
I've never learnt how to use printf, thats how they were showing it online when I was looking up a solution. Is there any other way to format is properly while printing. I'm not sure if we can use printf or not since the teacher never taught it.
Edit: I’m allowed to use the String.format() method.
You can use System.out.printf(). It works with the same codes as String.format().
System.out.printf("Password = %20s Newline = %2d Single = %d Equal = %d Lenght = %d Upper = %d Lower = %d Special = %d\n",
password, ctnline, ctlength, ctequal, ctlength2, ctupper, ctlower, ctspec2);
The format strings take any text you like plus codes beginning with %s. The two you'll use most often are %s -- a string -- and %d -- a decimal number. You can google for printf format strings for a longer discussion. Less often, but still important, is %f. I'll discuss that below.
The %s or %d can take an optional length. You'll see I did %20s -- that means space fill to 20 characters, even if the passed in string is empty. You can do the same thing with numbers. If you don't specify a length, it will use exactly as much as necessary. If you don't specify enough, it will run long.
You can zero-fill numbers like %03d. That will give you 004 instead of 4, for instance.
You can manipulate left-justification and right justification with -. %-20s, for instance.
At this point, I'd experiment.
For floating point numbers (1.5), you do %f.
System.out.printf("%3.1f\n", 1.5);
In this example, the 3 is the entire width and the 1 is the places after the decimal. So %10.2f will give you 10 characters wide and 2 points after the decimal.