1

I am writing a project in which I have to write questions and marks from MySQL database in a text file (Questions and marks are in different columns in database but in same table). Here, I want to write marks in same position i.e vertically aligned after each questions.

I tried using \t but it can't get desired output

while(myRs.next()) {
        String question = myRs.getString("question");
        String marks = myRs.getString("questionMarks");
        try {
        file.write(question+"\t\t\t\t\t\t\t" + marks + "\n");//write to text file
        }
        catch(Exception exe) {
            System.out.println(exe);
        }
        System.out.println("Q" + count +". " + question);
    }

Desired output is:

(Single "." represents whitespaces in actual output and "Question1", "Question2" , "Question3" are not actual questions, rather, they are statements)

Q1. Question1.............................4

Q2. Question2.............................4

Q3. Question3.............................5

Actual output is:

Q1. Question1........................ 4

Q2. Question2................................4

Q3. Question3...........................5
Zaid khan
  • 41
  • 5
  • 2
    Have you tried using [String.format](https://stackoverflow.com/questions/388461/how-can-i-pad-a-string-in-java/391978#391978)? – Avi Aug 19 '19 at 15:48
  • As an alternative to the above, you could also do the padding on the database side, such that when the questions are brought into Java they are already the same length. – Tim Biegeleisen Aug 19 '19 at 15:49
  • How much questions do you have? Collect questions in List and calculate max length, after this iterate and print with calculated number of dots – Vitaly Aug 19 '19 at 15:51
  • "Question1", "Question2" etc are the actual text or you have the question statement instead ? – Zain Arshad Aug 19 '19 at 15:52
  • @Avi String.format isn't giving me desired output – Zaid khan Aug 19 '19 at 16:26
  • @Vitaly I have more than a thousand questions. I forgot to mention that dots are whitespaces and not dots in actual output – Zaid khan Aug 19 '19 at 16:28
  • @ZainArshad I have question statement and not "Question1" , "Question2". – Zaid khan Aug 19 '19 at 16:29
  • @Zaidkhan Do you have a maximum width to your "question" string? – Avi Aug 19 '19 at 16:30
  • @Avi No. I didn't keep track of that while making the database – Zaid khan Aug 19 '19 at 16:35
  • @Zaidkhan Are question strings ALWAYS going to have the format "Q#. Question#"? Is there a way you can find the number of questions you will have to print? – Avi Aug 19 '19 at 16:38
  • @Avi Format is "Q#.Question Statement" and not "Q#. Question#."(Question# is not my actual question statements"). I have to write 4 to 8 questions in the text file. – Zaid khan Aug 19 '19 at 16:44
  • @Zaidkhan So then, it should be relatively trivial to calculate the width of the question statement? – Avi Aug 19 '19 at 16:46
  • @Avi Width can be varying. As the user can add questions to the database which would change the maximum width of the question statement – Zaid khan Aug 19 '19 at 17:08
  • @Zaidkhan If you're only going to print 4-8 questions at once (and never use the file again), you can just generate the "question" part of the string for all the questions, then store them in a `ArrayList`/other list type. Additionally, store the "marks" part in another list. Then, once you compute the longest question String's length, store it in `maxLen`, an integer. Finally, for each question/answer pair, you can do something like the following: `System.out.println(String.format("%-"+(1+maxLen)+"s%s",questions.get(index),answers.get(index)));`, except writing to file instead of printing. – Avi Aug 19 '19 at 17:16
  • 1
    This helped. Thanks @Avi – Zaid khan Aug 19 '19 at 17:55

2 Answers2

0

You just need to calculate the size of the question and add remaining spaces up to your preferred linesize.

See add characters n-times for alternative ways to repeat characters.

int maxlinesize = 40;
int count=0;

while(myRs.next()) {
    String question = myRs.getString("question");
    String marks = myRs.getString("questionMarks");
    count++;
    String q="Q"+count+" "+question;
    StringBuffer buffer = new StringBuffer();
    buffer.append(String.join(q, java.util.Collections.nCopies(maxlinesize - q.length(), " ")))
    .append(marks);
    try {
        file.write(buffer.toString()+ "\n");//write to text file
    }
    catch(Exception exe) {
        System.out.println(exe);
    }
}
Conffusion
  • 4,092
  • 2
  • 13
  • 19
  • This code is printing the same question over and over again then moving on to next question and then does the same. Variable count is an AtomicInteger in my code as i am using stream to increment it in the code which isn't uploaded here – Zaid khan Aug 19 '19 at 16:58
  • Don't understand your comment. The while loop calls `myRs.next()` and question and marks are each time getting the question and questionMarks of the current record. I thought the core problem was the number of spaces and I think my code shows a solution for that problem. Good luck. – Conffusion Aug 20 '19 at 07:16
0

As suggested by Avi, just store all the questions to be written in an ArrayList. Also, store marks in another ArrayList. Then, find the longest question's string length and use String.format to write in the text file. As follows:

        ArrayList<String> question1 = new ArrayList<String>();
        ArrayList<Integer> marks1 = new ArrayList<Integer>();
        int maxLen = 0;
        while(myRs.next()) {
            String question = myRs.getString("question");
            Integer marks = myRs.getInt("questionMarks");
            question1.add(question);
            marks1.add(marks);
            for(int i = 0; i < question1.size(); i++) {
                if(question1.get(i).length() > maxLen) {
                    maxLen = question1.get(i).length();

                }
            }
            int index = 0;

            try {

                file.write("Q" + count + ". " + String.format("%-"+(1+maxLen)+"s%d\n", question1.get(index), marks1.get(index)));
            }
            catch(Exception exe) {
                System.out.println(exe);
            }

`

Zaid khan
  • 41
  • 5