Parsing data with java is killing me

i created some data in notepad
which contains :: embedded in the data
my loop gets to the :: and does not see the ::
while
(Strpos < (line.length() ) ) {
index = Strpos +2;
colons = (line.substring(Strpos, index)) ;
System.out.println(colons);// i can see the value i'm looking at here
if (colons != "::")
displayline.append(line.substring(Strpos, Strpos + 1));
Strpos++;
the pgm always executes the displayline.append.
is this because i created the data in notepad
i'm really getting discouraged with java.

Essentiallly what you're trying to do here is compare Strings. By using the == operator (or != in your case), what you are actually comparing is the address location, which is always going to return true for you, since the address are going to be different (not equal for your code example). Try changing your if statment to the following:String colonMatch = "::"
if(!colons.equals(colonMatch)) // if you current search is not equal to a pair of colonsUsing the equals() method does a character by character comparison, which is what you are needing. I've made this mistake myself a few times, so no worries.
James

Similar Messages

  • Parsing Date with SimpleDateFormat

    Hi,
    In my application, i want to parse date which is in String format.
    The format of Date in String is "yyyyMMdd'T'HHmmss.SSS'Z'" (ex:- 20031201T100116.000Z).
    //Happy Case - Input Param "20031201T100116.000Z"
    //Output : 2003-12-01 10:01:16.0 is the expected output ------- works well.
    //Exception case Input param Value "00000000T000000.000Z"
    But the above case breaks as the expected output is "0000-00-00 00:00:00 ". But the output what i get is
    "0002-11-30 00:00:00.0"
    Greatly appreciate your inputs...,
    Here is my code for the above stated case....
    private static void printDateFormat(String str) throws Exception
    String format = "yyyyMMdd'T'HHmmss.SSS'Z'"; //Desired Format
    ParsePosition pos = new ParsePosition(0);
    SimpleDateFormat sdf = new SimpleDateFormat(format);
    Date date = sdf.parse(str, pos);
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    System.out.println("[After Formatting]" + date.toString());
    java.sql.Timestamp ts = new java.sql.Timestamp(cal.getTime().getTime());
    System.out.println(ts.toString());
    Thanks !
    Priya.Jlus
    IIMS-NZ.

    SimpleDateFormat is set to lenient date parsing by default, so it's trying it's best to come up with an actual date for 0000-00-00 (what would you say a 'zero' month should be?). You can prevent this "approximation" by using "setLenient(false)", but then you will get a null Date reference for non-parseable dates like the one you're using.
    The bottom line is that you're going to have to do some validation for the user input and handle exceptional conditions.
    Hope this helps! :o)

  • How to handle blob data with java and mysql and hibernate

    Dear all,
    I am using java 1.6 and mysql 5.5 and hibernate 3.0 . Some time i use blob data type . Earlier my project's data base was oracle 10g but now i am converting it to Mysql and now i am facing problem to save and fetch blob data to mysql database . Can anybody give me the source code for blob handling with java+Mysql+Hibernate
    now my code is :--
    ==================================================
    *.hbm.xml :--
    <property name="image" column="IMAGE" type="com.shrisure.server.usertype.BinaryBlobType" insert="true" update="true" lazy="false"/>
    ===================================================
    *.java :--
    package com.shrisure.server.usertype;
    import java.io.OutputStream;
    import java.io.Serializable;
    import java.sql.Blob;
    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Types;
    import javax.naming.InitialContext;
    import javax.sql.DataSource;
    import oracle.sql.BLOB;
    import org.hibernate.HibernateException;
    import org.hibernate.usertype.UserType;
    import org.jboss.resource.adapter.jdbc.WrappedConnection;
    import com.google.gwt.user.client.rpc.IsSerializable;
    public class BinaryBlobType implements UserType, java.io.Serializable, IsSerializable {
    private static final long serialVersionUID = 1111222233331231L;
    public int[] sqlTypes() {
    return new int[] { Types.BLOB };
    public Class returnedClass() {
    return byte[].class;
    public boolean equals(Object x, Object y) {
    return (x == y) || (x != null && y != null && java.util.Arrays.equals((byte[]) x, (byte[]) y));
    public void nullSafeSet(PreparedStatement st, Object value, int index) throws HibernateException, SQLException {
    BLOB tempBlob = null;
    WrappedConnection wc = null;
    try {
    if (value != null) {
    Connection oracleConnection = st.getConnection();
    if (oracleConnection instanceof oracle.jdbc.driver.OracleConnection) {
    tempBlob = BLOB.createTemporary(oracleConnection, true, BLOB.DURATION_SESSION);
    if (oracleConnection instanceof org.jboss.resource.adapter.jdbc.WrappedConnection) {
    InitialContext ctx = new InitialContext();
    DataSource dataSource = (DataSource) ctx.lookup("java:/DefaultDS");
    Connection dsConn = dataSource.getConnection();
    wc = (WrappedConnection) dsConn;
    // with getUnderlying connection method , cast it to Oracle
    // Connection
    oracleConnection = wc.getUnderlyingConnection();
    tempBlob = BLOB.createTemporary(oracleConnection, true, BLOB.DURATION_SESSION);
    tempBlob.open(BLOB.MODE_READWRITE);
    OutputStream tempBlobWriter = tempBlob.getBinaryOutputStream();// setBinaryStream(1);
    tempBlobWriter.write((byte[]) value);
    tempBlobWriter.flush();
    tempBlobWriter.close();
    tempBlob.close();
    st.setBlob(index, tempBlob);
    } else {
    st.setBlob(index, BLOB.empty_lob());
    } catch (Exception exp) {
    if (tempBlob != null) {
    tempBlob.freeTemporary();
    exp.printStackTrace();
    st.setBlob(index, BLOB.empty_lob());
    // throw new RuntimeException();
    } finally {
    if (wc != null) {
    wc.close();
    public Object nullSafeGet(ResultSet rs, String[] names, Object owner) throws HibernateException, SQLException {
    final Blob blob = rs.getBlob(names[0]);
    return blob != null ? blob.getBytes(1, (int) blob.length()) : null;
    public Object deepCopy(Object value) {
    if (value == null)
    return null;
    byte[] bytes = (byte[]) value;
    byte[] result = new byte[bytes.length];
    System.arraycopy(bytes, 0, result, 0, bytes.length);
    return result;
    public boolean isMutable() {
    return true;
    public Object assemble(Serializable arg0, Object arg1) throws HibernateException {
    return assemble(arg0, arg1);
    public Serializable disassemble(Object arg0) throws HibernateException {
    return disassemble(arg0);
    public int hashCode(Object arg0) throws HibernateException {
    return hashCode();
    public Object replace(Object arg0, Object arg1, Object arg2) throws HibernateException {
    return replace(arg0, arg1, arg2);
    =================================================================
    can anyone give me the source code for this BinaryBlobType.java according to mysql blob handling ..

    Moderator action: crosspost deleted.

  • Get html data with java

    Ok say I want to get some kind of data from a html page? How could it be done? Is this possible with java, or do I have to use some kind of scripting language?

    It looks like what you might be looking for is a method of getting HTTP, not HTML, data. HTML is simply text and can therefore be retrieved any way that a File could be retrieved (using a FileReader/FileWriter). HTTP, on the other hand, is the protocol that we use the most when viewing websites.
    If you're looking to make HTTP request, you may want to look into something like java.net.HttpURLConnection. Try searching these forums for HTTP, not html.

  • Parsing XML with Java - seeking advice for method to pursue

    Hi guys
    So here's the deal: I want to parse some XML from a server, get all the data I need from it, then shunt that data to the classes that need it. However, I'm really not sure what the best way of parsing the XML is.
    I've just written a class for obtaining the file, and one for building a DOM from the XML file. But looking at org.w3c.dom in the Java API documentation (see HERE) I'll have to implement shedloads of interfaces to be able to take advantage of the DOM, and I don't even know a lot about it.
    So am I best just writing a simple parser based on regular expressions to get the data I want? There's about 5 attributes in the XML file that I need to get - 5 for each instance of the containing element. So I don't think it'll be hard to do with regular expressions.. plus even if I did decide to implement all the DOM interfaces, the only way I'd know how to do half the stuff is with regular expressions anyway. I mean, how else would you do it? How else could you match up Nodes according to the Strings you're using for the elements, attributes etc?
    I worry that a parser using regular expressions might be too slow... I'm building this as an applet to visually display information from the server. I have nothing to support those fears, I'm just not experienced enough to know whether speed would be a problem if I chose this route... I don't think it would, but really need confirmation of that suspicion being unfounded.
    Any advice would be very, very welcome, as I'm tearing my hair out at the moment, unsure what to do.

    Komodo wrote:But JDOM is not in the core class libraries, is it? So if I want to create an applet embedded into a website, am I able to get people to download that as well?
    Sorry, I don't know anything about applets.
    Komodo wrote:Everyone's advice is appreciated, but my core question remains unanswered - would using regular expressions, considering how simple and unchanging the XML files are, be a viable option in terms of speed?
    Yes! I've done more than my fair share of XML processing with REs. It's not always easy. I often wish I could just use something XPath-like. But it's certainly easy to do and would probably mean that you'd have something up and running quicker than if you spent time investigating pure XML parsing approaches.

  • Parsing dates with dashes

    Hi everyone,
    I have a problem parsing a date that contains dashes, I searched all over the net for any word on this problem, but I couldn't find any.
    SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy");
    System.out.println(format.parse("24-09-1976"));
    java.text.ParseException: Unparseable date: "24-09-1976"
         at java.text.DateFormat.parse(DateFormat.java:319)
         at test.TestMisc.testDateParse(TestMisc.java:12)
         at test.TestMisc.main(TestMisc.java:21)
    Any help is appreciated.

    Woow. I must say I'm over-whelmed by the fast response. Thanks to everyone.
    In fact, my locale is an Arabic one, and I tried it while setting the locale to US and it worked, but that's completely rediculous. The locale specific stuff related to SimpleDateFormat should only be the textual parts, like month names and stuff like that, but what does locale have to do with parsing numberical dates?
    I think one fast solution that I can do now is get my date parsing method to use a US locale, but does any one know of a method to tell my locale to behave properly and parse the date as it should?
    My JDK is 1.3.1_11, do you guys think this might be a bug with the JDK?
    I also tried
    System.out.println(Calendar.getInstance() instanceof GregorianCalendar);
    The result was "true", which means that the calendar is a gregorian one, so that eliminates the possiblity that the problem is caused due to a lunar calendar.

  • Send JDBC Data with Java Proxy

    Hello,
    I Have generated a Java Proxy. And the Data Source is a JDBC Table like:
    VALUE01--VALUE02--VALUE03
    ROW1-Test1----Test1----Test1
    ROW2-Test2----Test2----Test2
    Now i can set the Datatypes in XI Proxy with
    TYPE.setVALUE01("Test1");
    TYPE.setVALUE02("Test1");
    TYPE.setVALUE03("Test1");
    so i can send a XML like
    <TYPE>
    <VALUE01>Test</VALUE01>
    <VALUE02>Test</VALUE02>
    <VALUE03>Test</VALUE03>
    </TYPE>
    My Problem is to send a XML Data like
    <TYPE>
    <VALUE01>Test1</VALUE01>
    <VALUE02>Test1</VALUE02>
    <VALUE03>Test1</VALUE03>
    </TYPE>
    <TYPE>
    <VALUE01>Test2</VALUE01>
    <VALUE02>Test2</VALUE02>
    <VALUE03>Test2</VALUE03>
    </TYPE>
    How can is send more than one item throw XI Proxi?
    Any Idea or Documentation?
    Regards,
    Robin

    Hi Robin,
    Hope these links provide good help
    https://www.sdn.sap.com/irj/servlet/prt/portal/prtroot/com.sap.km.cm.docs/library/xi/java proxies and sap xi - the inside story, part 1.pdf
    https://www.sdn.sap.com/irj/servlet/prt/portal/prtroot/com.sap.km.cm.docs/library/xi/java proxies and sap xi - the inside story, part ii.pdf
    Regards
    Vishnu

  • Export and import table data with Java

    I need a library for simple exporting and importing table data.
    The data should be exported to a SQL file with insert statements.
    I just want to tell the library the table name, the connection and where to store the file. The usage should be very simple.
    Are there any small libraries for this? Finished calsses and methods which I can just call?

    I need a library for simple exporting and importing
    table data.
    The data should be exported to a SQL file with insert
    statements.Every database has utilities to export/import data from tables. Take a look at your database manual.

  • What are my options to locally store data with Java ME?

    RMS use to be the most popular way! But you can also look for 3thrd part frameworks to make it even more easy!

    Since you're still under warranty, it wouldn't hurt to make an appointment at your local Apple Store or an AASP and have the techs take a look at it. If there's no 'physical' scratch then there may be a problem with your screen that can be fixed under warranty.
    And open windows should not 'shake' - another thing that the techs to evaluate.
    Best of luck,
    Clinton

  • Parsing SOAP with JAVA

    I need to be able to take the XML portion of a SOAP message and send it to another a program as a text String.
    So basically take the SOAPBody, extract the SOAPElements and then write the contents to and output stream...
    Can anyone tell me how to do this, sample code would be appreciated.
    thanks

    http://java.sun.com/webservices/docs/1.0/tutorial/doc/JAXM.html

  • Data management with Java & XQuery

    As we can manipulate DB XML data with Java, when and why java programmers should consider XQuery instead ?

    Thanks for the precision. So one advantage of Xquery is to use some kind of universal language allowing an exist programmer to come on board with DB XML or a DB XML programmer to do some stuff on an exist db (or any xml db supporting xquery)
    To conclude of this subject in the particulary case of Oracle DB XML, are there some things you can do with Xquery that you can't in Java ? What can we tell in terms of performance for doing the same thing in Xquery and Java ?

  • Parsing data from Serial Port one byte at a time

    Hi everyone,
            What I want to do is read in from the serial port byte by byte and parse each byte that I am reading in. The size of the data read in varies. How do I do that? I can do using .dll but I want to learn how to do it in labview. I searched through the forum for something similar but no luck. I have seen some that puts the VISA read in a while loop and others that puts the property node,  "bytes at Port" in a while loop. What is the difference in doing either way? I am using Labview 7.1 Can anyone point me in the right direction?
    Thanks

    Hi guys..
    I'm still a beginner in using labview tcp/ip function. for now, i have a project to read the labview data with java programming language so that i can monitor that data using any devices that using java, such as PC or cell phone. my task for now is to determine the format of data that being sent (its type, length, etc), coz as far as i done until now, the data that read in the java is still a raw material so that i dont know how to gain the information from that data.
    my question is :
    - how does the type cast work in changing the data format from one to another?
    - how does the bytes to read in tcp read work? coz when i'm changing the length of bytes to read constant from 4 bytes to another, the data is becoming mess up. As far as i know, the bytes to read only read the first "constant number" byte in the tcp read to determine the length and type of the data..
    thanks

  • Wrong XML parsing in Java  6.0 , the same code works fine with Java 5.0

    I have the following xml data in a file ( C:\_rf\jrebug.xml ) :
    <?xml version="1.0" encoding="UTF-8"?>
    <rs>
         <data
              input3='aa[1]'
              input4='bb[1]'
              input6='cc[2]'
              input8='dd[7]'
              output2='ee[7]'
              output4='ff[511]'
              output6='gg[15]'
              output7='hh[1]'
         />
    </rs>
    I have the following code to parse this XML data :
    import java.io.IOException;
    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;
    import javax.xml.parsers.ParserConfigurationException;
    import org.w3c.dom.Document;
    import org.w3c.dom.Element;
    import org.w3c.dom.NamedNodeMap;
    import org.w3c.dom.NodeList;
    import org.w3c.dom.Node;
    import org.xml.sax.SAXException;
    import org.xml.sax.ErrorHandler;
    import org.xml.sax.SAXParseException;
    public class DomParserBug {
         Document dom;
         public void runExample() {
    parseXmlFile("C:\\_rf\\jrebug.xml");
              parseDocument();
         private void parseXmlFile(String filename){
              //get the factory
              DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
              //dbf.setValidating(true);
    dbf.setValidating(false);
              try {
                   //Using factory get an instance of document builder
                   DocumentBuilder db = dbf.newDocumentBuilder();
                   db.setErrorHandler(new MyErrorHandler());
                   //parse using builder to get DOM representation of the XML file
                   dom = db.parse(filename);
              }catch(ParserConfigurationException pce) {
                   pce.printStackTrace();
              catch(SAXParseException spe) {
                   spe.printStackTrace();
              catch(IOException ioe) {
                   ioe.printStackTrace();
              catch(SAXException se) {
                   se.printStackTrace();
    private void parseDocument()
    Element docEle = dom.getDocumentElement();
    NodeList nl = docEle.getElementsByTagName("data");
    if(nl != null && nl.getLength() > 0)
    for(int i = 0 ; i < nl.getLength();i++)
    Element el = (Element)nl.item(i);
    NamedNodeMap attrsssss = el.getAttributes();
    for (int ii=0; ii<attrsssss.getLength(); ++ii)
    Node attr = attrsssss.item(ii);
    System.out.println("Attribute name is =" attr.getNodeName() " AND Attribute value is ="+attr.getNodeValue());
         public static void main(String[] args){
              DomParserBug dpe = new DomParserBug();
              dpe.runExample();
         class MyErrorHandler implements ErrorHandler {
              public void error(SAXParseException e) {
              System.out.println("error : " +e.toString());
         // This method is called in the event of a non-recoverable error
         public void fatalError(SAXParseException e) {
         System.out.println("fatalError : " +e.toString());
         // This method is called in the event of a warning
         public void warning(SAXParseException e) {
         System.out.println("warning : " +e.toString());
    The parsed output is :
    Attribute name is =input3 AND Attribute value is =aa[1]
    Attribute name is =input4 AND Attribute value is =bb[1]
    Attribute name is =input6 AND Attribute value is =cc[2]
    Attribute name is =input8 AND Attribute value is =dd[7]
    Attribute name is =output2 AND Attribute value is =ee[7]
    Attribute name is =output4 AND Attribute value is =ff[511]
    Attribute name   is  =output6 AND Attribute value  is =hh[1]]
    Attribute name   is  =output7 AND Attribute value  is =hh[1]
    THE LAST TWO LINES ARE SIMPLY WRONG.
    With java 5.0 the last two lines are parsed correctly :
    Attribute name   is  =output6 AND Attribute value  is =gg[15]
    Attribute name   is  =output7 AND Attribute value  is =hh[1]
    I have seen this issue only after I upgraded to java 6.0. I have searched the java 6.0 bug database but there is nothing there.
    I have also submitted a bug to the bugdatabase last month but have not heared anything.
    Anybody have any clue about this ???
    Thanks
    Edited by: skaushik on Jan 4, 2008 12:40 AM
    Edited by: skaushik on Jan 4, 2008 6:38 PM

    I have seen similar issue. I found that if you remove the square brackets from the first line in teh XML file, the last two lines are parsed correctly.
    Replace the follwing line : :
    Attribute name is =input3 AND Attribute value is =aa[1]
    with :
    Attribute name is =input3 AND Attribute value is =aa
    and the output is CORRECT :
    Attribute name is =input3 AND Attribute value is =aa
    Attribute name is =input4 AND Attribute value is =bb[1]
    Attribute name is =input6 AND Attribute value is =cc[2]
    Attribute name is =input8 AND Attribute value is =dd[7]
    Attribute name is =output2 AND Attribute value is =ee[7]
    Attribute name is =output4 AND Attribute value is =ff[511]
    Attribute name   is  =output6 AND Attribute value  is =gg[15]
    Attribute name   is  =output7 AND Attribute value  is =hh[1]

  • SAX Parser Validation with Schemas (C, C++ and JAVA)

    We are currently using the Oracle XML Parser for C to parse and validate XML data using the SAX parser interface with validation turned on. We currently define our XML with a DTD, but would like to switch to schemas. However, the Oracle XML Parser 9.2.0.3.0 (C) only validates against a DTD, not a schema when using the SAX interface. Are there plans to add schema validation as an option? If so, when would this be available? Also, the same limitation appears to be true for C++ and JAVA. When will any of these provide SAX parsing with schema validation?
    Thanks!
    John

    Will get back to you after checked with development team...

  • Parsing date/time with extra characters

    I have a date/time that I would like to parse to make more readable. The original format is like so:
    2009-04-26T19:39:00-04:00
    This is the date, then 24hr time which is separated by a T, then the time zone. In this case, the time zone is EDT, -4 hours. I have found that I can parse this with something like formatter = new SimpleDateFormat ("yyy-MM-ddTHH:mm:ssZ");This doesn't work, because of the T in the middle I assume. I have also tried
    formatter = new SimpleDateFormat ("yyy-MM-dd" + "T" + "HH:mm:ssZ");This doesn't work either. I get a compile error "Illegal patter character 'T'" for both of these. I am about to split the string into two sections, one with date before T, and one with time after T. Before I do this, I was just wondering if there is a cleaner way to do this, using some wildcard possibly?

    Try this:
    formatter = new SimpleDateFormat(""yyyy-MM-dd'T'HH:mm:ss.SSSZ");{code}
    Taken straight from the java docs.  Hopefully it'll work.
    Edit:
    Oops, someone already replied...had the window open too long i guess.
    Edited by: sublimeph03nix on Apr 26, 2009 7:46 PM                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       

Maybe you are looking for