Search code examples
javaswingjtextarea

How to remove first line of jTextArea


I am quite new to programming and java. I want to know how to remove first line on jtextarea. Though I can post the coding of my application but I don't think it's needed and plus I am on phone. Please tell me how to remove first line in jtextfield java.

Ok... heres the code for what I am trying to do->

String x = (String) jComboBox1.getSelectedItem();
String z = JOptionPane.showInputDialog("Please enter new name for Database");
        try{
        Class.forName("java.sql.DriverManager");
        Connection con = (Connection)
        DriverManager.getConnection("jdbc:mysql://localhost:"+GlobalParams.portvar+"/",""+k,""+j);
        Statement stmnt = (Statement) con.createStatement();
        String query3 = "Create database "+z;
        stmnt.executeUpdate(query3);
        String query = "use "+x;
        stmnt.executeQuery(query);
        String query2 = "show tables";
        Statement stmnt2 = (Statement) con.createStatement();
        ResultSet rs = stmnt2.executeQuery(query2);
        while (rs.next()){ 
        String dname = rs.getString("Tables_in_"+x);
        if(jTextArea1.getText().equals("")){
        jTextArea1.setText(jTextArea1.getText()+dname);
        }
        else{
            jTextArea1.setText(jTextArea1.getText()+"\n"+dname);
        }
        String y = jTextArea1.getText();
        Scanner scanner = new Scanner(y);
while (scanner.hasNextLine()) {
        String line = scanner.nextLine();
        String query4 =  "RENAME TABLE "+x+"."+line+" TO "+z+"."+line;
        //I will add remove first line code here.--------------------------------------
        stmnt.executeUpdate(query4);
        String query5 = "drop database "+x;
        stmnt.executeUpdate(query5);
}}}

    catch(Exception e){
        JOptionPane.showMessageDialog(this,e.getMessage());
    }

As you can see, I am trying to create a program which renames databases in mysql. It works fine but it moves just one table and then it gives error that "table already exists (same table name that it moved)" in new database. Since it gets the table names from lines present in textarea, i suspect its reading same line again and again. So my idea is to remove the first line everytime the code is completed so that it wont take the same table name twice. To be clear what I want to do-> Suppose my text area consists of following lines->

line 1
line 2
line 3
line 4
so on...

I want that after execution of code, line 1 should be removed and the new text present in textarea should be->

line 2
line 3
line 4
line 5
so on...

Solution

  • Just call scanner.nextLine() after creating the Scanner:

    Scanner scanner = new Scanner(y);
    if (scanner.hasNextLine()) // Skips first line
        scanner.nextLine();
    while (scanner.hasNextLine()) {
        String line = scanner.nextLine();
        ...
    }