Forum Stats

  • 3,826,849 Users
  • 2,260,713 Discussions
  • 7,897,100 Comments

Discussions

how to remove charecter  from string?

3513491
3513491 Member Posts: 4
edited Jul 29, 2017 2:06PM in Java Programming

i try to replace  from my string with this code

mystring=mystring.replaceAll("","");

but it did not work?

Answers

  • mNem
    mNem Member Posts: 1,380 Gold Trophy
    edited Jul 28, 2017 6:22AM

    Works fine for me.

    public static void main(String[] args)

      { 

        String str = "asffass".replaceAll("", "");

        System.out.print(str);

      }

  • Unknown
    edited Jul 28, 2017 11:34AM
    but it did not work?

    If you want help with code you have to POST THE CODE.

    Standard Java exceptions NEVER say 'did not work' - they ALWAYS provide an exception number and message.

  • 3513491
    3513491 Member Posts: 4
    edited Jul 29, 2017 5:28AM

    Exception :unmmaped charachter

    private List<Card> ankiConvert(File ankiLesson) throws UnsupportedEncodingException{

            int i=1;

            List<Card> notes=new ArrayList();

             Connection conn = null;

             Statement query=null;

            try {

                // db parameters

                String url = "jdbc:sqlite:"+ankiLesson.getAbsolutePath();

                // create a connection to the database

                try{

                conn = DriverManager.getConnection(url);

                }catch(Exception e){

                System.out.println("Connection to SQLite has been established.");

                }

                query=conn.createStatement();

                String sql="SELECT flds,sfld FROM 'notes'";

                ResultSet rs=query.executeQuery(sql);

                while(rs.next()){

                   

                    org.jsoup.nodes.Document doc =  Jsoup.parse(rs.getString("flds"));

                    org.jsoup.nodes.Document doc1 =  Jsoup.parse(rs.getString("sfld"));

                    String front=doc1.text();

                    String back=doc.text().replaceAll("&#31;","").replace(front,"");

                    String image=doc.getElementsByTag("img").attr("src");

                    Card card=new Card();

                    card.setId(String.valueOf(i));

                    card.setFrontside(front.trim().replaceAll("(\\[)(.*)|(\\()(.*)|(\\{)(.*)",""));

                    card.setBackside(back.trim().replaceFirst("(/.*/)",""));

                    card.setLevel("0");

                    card.setTesthit("0");

                    card.setChapter("All");

                    if(image!=null){

                    card.setImg(image);

                    }

                    notes.add(card);

                    i++;

                }

                rs.close();

            } catch (SQLException e) {

                System.out.println(e.getMessage());

            } finally {

                try {

                    if (conn != null) {

                        conn.close();

                    }

                } catch (SQLException ex) {

                    System.out.println(ex.getMessage());

                }

            }

            return notes;

        }

  • mNem
    mNem Member Posts: 1,380 Gold Trophy
    edited Jul 29, 2017 6:03AM

    It is difficult to identify which line is throwing exception.... when we do not have access to your 3rd party libraries.

    Exception :unmmaped charachter

    May be it is useful to identify the issue, if you could paste the full exception trace.

    One more thing, IMHO, it is always pays to write readable code when it comes to maintainable code.

    Normally, I avoid chaining method calls.

    String back=doc.text().replaceAll("&#31;","").replace(front,"");

    instead,

    String back = doc.text();

    back = back.replaceAll(.....); // side note: this could throw NPE if back is null.

    back = back.replace(....);

    This allows me to step over line by line using the debugger and identify what is being returned when troubleshooting.

  • Unknown
    edited Jul 29, 2017 2:06PM

    I think you missed reading this part of what I said:

    Standard Java exceptions NEVER say 'did not work' - they ALWAYS provide an exception number and message.

    The code you posted pretty much says you do NOT care about what exceptions may occur - and that processing should just continue no matter what happens:

    try{            conn = DriverManager.getConnection(url);            }catch(Exception e){            System.out.println("Connection to SQLite has been established.");            }            query=conn.createStatement();

    Really?

    Even if 'getConnection' raises an exception you just want to go ahead and try to create a statement and continue processing the rest of the code?

    You should ONLY capture exceptions if you plan to handle them.

    You code has multiple exception blocks and NONE OF THEM actually handle an exception.

      } catch (SQLException e) {            System.out.println(e.getMessage());        } finally {            try {                if (conn != null) {                    conn.close();                }            } catch (SQLException ex) {                System.out.println(ex.getMessage());            }        }

    Get rid of ALL of those exception handlers so you can see the actual exceptions that occur and get the information Java provides about exactly WHERE they occur.

    Post the actual stack trace that Java provides - it will show the FULL list of events that are happening.

    Then start troubleshooting your problem by examining EVERY STEP of the code so you can see what it does

    Don't underestimate the importance of what mNem said:

    String back=doc.text().replaceAll("&#31;","").replace(front,"");

    That contains THREE different statements jammed together on one line. That makes it difficult to know which of them, if any, are causing an exception.

    If that one line causes a problem it could be the '.replace', the 'replaceAll' or even the 'doc.text()'. Either the 'doc' or the 'front' could be null or problematic.

    Write SIMPLE code so you know what the code does. Then you can worry about taking shortcuts and trying to write complex, single-line statements.

    Then if you still have trouble identifying the actual line with a problem add a try .. except block around every line until you find the one throwing the exception.

    Then examine EACH AND EVERY variable using a debugger (e.g. NetBeans) to make sure they have the value you expect.

    Until you remove those garbage exception handlers and post the actual exception stack trace (printStackTrace) we can't really help.

This discussion has been closed.