4

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.

Sotirios Delimanolis
  • 252,278
  • 54
  • 635
  • 683
  • "I'm not sure if we can use printf or not since the teacher never taught it." - There's no use in us answering, then. You should ask your teacher which methods you're allowed to use, and then research how to use them. – Jacob G. Nov 01 '19 at 13:57
  • Maybe, your teacher is using one of the solutions provided to [this question](https://stackoverflow.com/questions/388461/how-can-i-pad-a-string-in-java). – deHaar Nov 01 '19 at 13:59
  • You can play around with the solution suggested [here](https://stackoverflow.com/a/39312614/356629) – kenny_k Nov 01 '19 at 14:04
  • "Is there any other way to format" - yes, the basic one is to add enough spaces so that the values in one column all have the same length. (e.g. while the password has a length less than 20, add a space to it) – user85421 Nov 01 '19 at 14:07

2 Answers2

2

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.

Joseph Larson
  • 4,851
  • 1
  • 15
  • 25
  • Thank you so much, that worked. In the end I used this as my formatting: ``` System.out.printf("Password = %-22s Newline = %-5d Single = %-5d Equal = %-5d Lenght = %-5d Upper = %-5d Lower = %-5d Special = %-5d\n", password, ctnline, ctlength, ctequal, ctlength2, ctupper, ctlower, ctspec2); ``` – Tushar Raval Nov 01 '19 at 16:39
1

You can use String.format to uniform your outputs. With String.format you can define your texts output length as a format.

As an example;

String firstValue = "Password = bank,Rabbither";
String secondValue = "Newline = 0";
String thirdValue = "Single = 2";
String str = String.format("%1$-20s | %2$-30s | %3$-30s",fristValue, secondValue, thirdValue);

For 1$-20s : 1$ means 1st variable, -20s means your strings fixed length.

kenny_k
  • 3,325
  • 4
  • 24
  • 36
Burak Akyıldız
  • 1,457
  • 15
  • 25