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
15
  • 2
    Have you tried using String.format? Commented Aug 19, 2019 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. Commented Aug 19, 2019 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 Commented Aug 19, 2019 at 15:51
  • "Question1", "Question2" etc are the actual text or you have the question statement instead ? Commented Aug 19, 2019 at 15:52
  • 1
    This helped. Thanks @Avi Commented Aug 19, 2019 at 17:55

2 Answers 2

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);
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

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
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.
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);
            }

`

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.