简体   繁体   English

如何在 While 循环中递增 Integer 并将其转换为字符串?

[英]How do you Increment an Integer inside a While loop and convert it to String?

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 .该程序的目标是 append 并将数据库的内容显示到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.我想通过在 while 循环内声明一个 integer 来用递增编号列出它们,然后将其转换为字符串,然后在循环的下一次迭代中递增它。

the output goes like this: output 是这样的:

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有人可以帮忙吗

You are recreating the variable and assign it to 1 at each iteration int num = 1;您正在重新创建变量并在每次迭代时将其分配给1 int num = 1; . . You have to create it outside of the while loop scope您必须在 while 循环之外创建它 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++;
}

Move num outside the while loop.num while 循环。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 你如何转换地图 <String, Person> 到地图 <String, Integer> 在java流里面收集? - How do you convert Map<String, Person> to Map<String, Integer> in java streams inside collect? 如何通过do while循环将用户输入转换为星号? - How do you convert the users input into asterisks with a do while loop? 不正确的整数值:如何将我的数据从String转换为Int以避免Auto_Increment? - Incorrect Integer Value: How do i convert my Data from String to Int to avoid Auto_Increment? 如何在while循环内初始化数组并在外部定义它? - How do you initialize an array inside a while loop and define it outside? Java do/while 循环问题? 如何计算循环内的字符串 - Java do/while loop problem? How to count the string inside the loop 如何使用for循环将字符串arraylist转换为double? - How do you convert string arraylist to double using for loop to calculate? 如何在Java中将整数转换为字符? (5-&gt;&#39;5&#39;) - How do you convert integer to character in Java? (5 --> '5') 如何转换数字并将其存储在char数组中,然后将char数组转换为String,以便输出整数? - How do you convert a number and store it in a char array and then convert the char array to String so that it prints the integer? 如何将字符串转换为整数以在 Velocity 的循环中使用? - How to convert a String to Integer for use in a loop in Velocity? Java do while 循环增量 - Java do while loop increment
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM