3

I have to create an application where I will print the result of a student. In the assignment we have to use the \t, in order to tab everything in a good line.

   for(int i = 0; i< 7; i++) {
         System.out.println("Vak/Project :" + naamVak[i] + "\t\tCijfer: " +
      inputCijfer[i] + "\tBehaalde studiepunten: " + studiepuntenVAK[i]);
   }

It does print the name of the project (array called naamVak), the result (inputCijfer), and how many points they have got per project (studiepuntenVak).

When I print the results to the console, it shows as follows:

Vak/Project :Fasten Your Seatbelts      Cijfer: 1.0 Behaalde studiepunten: 0
Vak/Project :Programming        Cijfer: 2.0 Behaalde studiepunten: 0
Vak/Project :Databases      Cijfer: 3.0 Behaalde studiepunten: 0
Vak/Project :Personal Skills        Cijfer: 4.0 Behaalde studiepunten: 0
Vak/Project :Project Skills     Cijfer: 5.0 Behaalde studiepunten: 0
Vak/Project :Infrastructure     Cijfer: 6.0 Behaalde studiepunten: 3
Vak/Project :Network Engineering 1      Cijfer: 7.0 Behaalde studiepunten: 3

How can I create the output to be all in a correct line?

Markus Mitterauer
  • 1,560
  • 1
  • 14
  • 28
Boris
  • 41
  • 1
  • 3
  • 3
    you could "calculate" the lenght of the already printed part, and start from there. /t doesn't take the previous or next lines into account – Stultuske Sep 16 '16 at 10:48
  • 2
    Have a look at the [format](https://docs.oracle.com/javase/8/docs/api/java/io/PrintStream.html#format-java.lang.String-java.lang.Object...-) methods. – McDowell Sep 16 '16 at 10:51
  • [StringUtils#rightPad](https://commons.apache.org/proper/commons-lang/javadocs/api-3.1/org/apache/commons/lang3/StringUtils.html) might be helpful. – jp-jee Sep 16 '16 at 10:53
  • 1
    tabulator, even corrected in his counts, is not guaranted in all envinroments. Read about formatting, f.e. class java.util.Formatter and format in fixed length – Jacek Cz Sep 16 '16 at 10:55
  • tab usually is counted as every 8th character, but not always – Jacek Cz Sep 16 '16 at 10:56
  • Possible duplicate of [Printing with "\t" (tabs) does not result in aligned columns](http://stackoverflow.com/questions/6000810/printing-with-t-tabs-does-not-result-in-aligned-columns) – Markus Mitterauer Sep 16 '16 at 11:42

3 Answers3

2

You can use String.format method (with a printf like syntax), and provide a value for width. The syntax is here: https://docs.oracle.com/javase/7/docs/api/java/util/Formatter.html#syntax , You could use like:

System.out.println(String.format("%20s %20s %20s", naamVak[i], inputCijfer[i], studiepuntenVAK[i]));

this assumes a max input width of 20 chars for each column

maslan
  • 2,078
  • 16
  • 34
  • Thanks, really thanks! I will try this out when I get home. Will give you some feedback when Im done. – Boris Sep 16 '16 at 11:27
0

https://docs.oracle.com/javase/7/docs/api/java/util/Formatter.html

You can use String.format method

String s1 = String.format("test right alignment:  %13s   %50s  ",'test a short string', 'test a very very very long string' );
String s2 = String.format("test left  aligmnent:  %-13s   %-50s  ",'test a short string', 'test a very very very long string'  );

System.out.println(s1);
System.out.println(s2);
frhack
  • 4,862
  • 2
  • 28
  • 25
0
System.out.println(String.format("%20s %20s %20s %20s %20s", "Vak/Project: ", naamVak[i], "Cijfer: ", inputCijfer[i], "Behaalde studiepunten: ", studiepuntenVAK[i]));

Still gives a strange view for the user. This is the output in console:

   Vak/Project:  Fasten Your Seatbelts             Cijfer:                   8.6 Behaalde studiepunten: 
   Vak/Project:           Programming             Cijfer:                   7.5 Behaalde studiepunten: 
   Vak/Project:             Databases             Cijfer:                   2.1 Behaalde studiepunten: 
   Vak/Project:       Personal Skills             Cijfer:                   2.3 Behaalde studiepunten: 
   Vak/Project:        Project Skills             Cijfer:                   2.4 Behaalde studiepunten: 
   Vak/Project:        Infrastructure             Cijfer:                   2.5 Behaalde studiepunten: 
   Vak/Project:  Network Engineering 1             Cijfer:                   2.6 Behaalde studiepunten: 
Boris
  • 41
  • 1
  • 3
  • You should not use answers as a way to exchange posts, but anyway, that is because the first and the last rows contain entries that are longer that 20 characters: "Fasten Your Seatbelts" and "Network Engineering 1". You can for example adjust the "%20s %20s %20s %20s %20s" to like "%20s %30s %20s %20s %20s" or something – maslan Sep 20 '16 at 09:24