0
StyledDocument doc = txtpaneExamGeneration.getStyledDocument();

    Connection con = null;
    Statement stmt = null;
    try {
        Class.forName("org.sqlite.JDBC");
        con = DriverManager.getConnection("jdbc:sqlite:sql_items.sqlite");
        stmt = con.createStatement();
        ResultSet rs = stmt.executeQuery("SELECT item_desc FROM active_items ORDER BY random();");

        while(rs.next()){
            int num = 1;
            String itemNumbering = Integer.toString(num);
            String stringHandler = rs.getString("item_desc");
            doc.insertString(doc.getLength(), Integer.toString(num) + ". _______________ - " , null);
            doc.insertString(doc.getLength(), stringHandler + ".\n\n", null);
            num++;
        }
    } catch (ClassNotFoundException | SQLException e) {
        System.out.println(e);
    } catch (BadLocationException ex) {
        Logger.getLogger(generateExam.class.getName()).log(Level.SEVERE, null, ex);
    }

The objective of the program is to append and display the contents of a database to a JTextPane. I want to list them with incrementing numbering by declaring an integer inside the while loop and then convert that to string and then increment it for the next iteration of the loop.

the output goes like this:

1. ____________ - item 1

1. ____________ - item 2

1. ____________ - item 3

1. ____________ - item 4

I cannot understand why it is not incrementing the numbering. Can someone please help

1
  • 1
    The first statement in your loop declares and initializes num at 1. Also, I would prefer to build a single String with StringBuilder in the loop and then insert it into the document with one call (after the loop). I suspect you will see much better performance if you take advantage of buffering in general. Commented Apr 24, 2020 at 15:08

2 Answers 2

1

You are recreating the variable and assign it to 1 at each iteration int num = 1;. You have to create it outside of the while loop scope

int num = 1;
while(rs.next()){
    String itemNumbering = Integer.toString(num);
    String stringHandler = rs.getString("item_desc");
    doc.insertString(doc.getLength(), Integer.toString(num)+". _______________ - ",null);
    doc.insertString(doc.getLength(), stringHandler + ".\n\n", null);
    num++;
}
Sign up to request clarification or add additional context in comments.

Comments

1

Move num outside the while loop.

int num = 1;
while(rs.next()){
    String itemNumbering = Integer.toString(num);
    String stringHandler = rs.getString("item_desc");
    doc.insertString(doc.getLength(), Integer.toString(num) + ". _______________ - " , null);
    doc.insertString(doc.getLength(), stringHandler + ".\n\n", null);
    num++;
}

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.