Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sequential count in between 0000-9999

Tags:

java

I want to get the count in 4 digits i.e.0 as 0000,1 as 0001,2 as 0002 till 9999.

I am trying the below code but its not working . if anyone can help it will be nice.

    public void actionPerformed(ActionEvent ae) {
    String str = ae.getActionCommand();
    if (str.equals("GENERATE PART NO. :")) {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection("jdbc:mysql://localhost/d03", "root", "");
            st = con.createStatement();

            String s = "select value from part where mix='" + jc.getSelectedItem() + "'";

            rs = st.executeQuery(s);
            t1.getText();
            if (rs.next()) {

                String add1 = rs.getString("value");
                t1.setEditable(false);

                String str9 = "EP" + add1;

                for (j = 0; j <= 9999; j++) {

                    String.format("%04d", j);
                    String str10 = str9 + j;
                    String query = "select MC from final";
                    ResultSet rs1 = st.executeQuery(query);
                    while (rs1.next()) {
                        while (str10.equals(rs1.getString("MC"))) {
                            j++;

                            str10 = str9 + j;
                        }
                        t1.setText(str10);
                    }
                }
            }
like image 654
user6443988 Avatar asked Dec 01 '25 09:12

user6443988


2 Answers

String.format("%04d", j); does what you want, but you need to use the result of the format() method. Currently you are simply discarding the return value.

...
for (int j = 0; j <= 9999; j++) {
    String str = String.format("%04d", j);
    System.err.println(str);
}
...

Output:

0000
0001
0002
0003
...
like image 151
Andreas Fester Avatar answered Dec 05 '25 15:12

Andreas Fester


What you want to do is rather this:

for (;str10.equals(rs1.getString("MC"));j++) {
    str10 = String.format("%s%04d", str9, j);
}
like image 31
Nicolas Filotto Avatar answered Dec 05 '25 14:12

Nicolas Filotto



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!