【问题标题】:Write a word in a text file on a specific position in java在java中特定位置的文本文件中写一个单词
【发布时间】:2019-08-19 15:45:59
【问题描述】:

我正在编写一个项目,其中我必须将 MySQL 数据库中的问题和标记写入文本文件(问题和标记位于数据库的不同列中,但在同一个表中)。在这里,我想在相同的位置写标记,即在每个问题之后垂直对齐。

我尝试使用 \t 但它无法获得所需的输出

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

期望的输出是:

(单个“.”表示实际输出中的空格,“Question1”、“Question2”、“Question3”不是实际问题,而是语句)

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

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

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

实际输出为:

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

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

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

【问题讨论】:

  • 您是否尝试过使用String.format
  • 作为上述的替代方案,您还可以在数据库端进行填充,这样当问题被引入 Java 时,它们的长度就已经相同了。
  • 您有多少问题?收集列表中的问题并计算最大长度,然后迭代并打印计算的点数
  • "Question1"、"Question2" 等是实际文本,还是您有问题陈述?
  • 这有帮助。谢谢@Avi

标签: java mysql text-files filewriter


【解决方案1】:

您只需要计算问题的大小并将剩余的空格添加到您喜欢的行大小。

请参阅add characters n-times 了解重复字符的替代方法。

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

【讨论】:

  • 这段代码一遍又一遍地打印同一个问题,然后继续下一个问题,然后做同样的事情。变量计数是我的代码中的 AtomicInteger,因为我正在使用流在此处未上传的代码中递增它
  • 不理解您的评论。 while 循环调用myRs.next() 和问号每次获取当前记录的问号和问号。我认为核心问题是空格的数量,我认为我的代码显示了该问题的解决方案。祝你好运。
【解决方案2】:

按照Avi 的建议,只需将所有要编写的问题存储在ArrayList 中。此外,将标记存储在另一个 ArrayList 中。然后,找到最长的问题的字符串长度,并使用String.format 写入文本文件。如下:

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

`

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-11-30
    • 1970-01-01
    • 1970-01-01
    • 2020-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多