Results 1 to 14 of 14
  1. #1
    Lund01's Avatar
    Lund01 is offline Senior Member
    Join Date
    Sep 2010
    Location
    The Netherlands
    Posts
    149
    Rep Power
    0

    Default I don't understand this error....

    Building the last parts on the program, I have one error I don't understand.

    Error:
    Java Code:
     
    C:\Documents and Settings\.....\Converter.java:145: setAttribute(java.lang.String,java.lang.String) in org.w3c.dom.Element cannot be applied to (java.lang.String,java.lang.Float)
    					child2.setAttribute("calc_neutral_pep_mass", massaInfo2);
    					      ^
    1 error
    And this is my program:
    Java Code:
    /**
    * Autheur: Oddens, Ilse X.M.
    * Creation Date: 9 June 2011.
    * Location: Leiden, the Netherlands.
    * Student at the Hogeschool Leiden.
    * Goal of this program: Read in a .msgfdb file and transform this to am .xml file.
    * Programmed on: Windows XP
    */
    
    import java.io.*;
    import java.util.*;
    import org.w3c.dom.*;
    import javax.xml.parsers.*;
    import javax.xml.transform.*;
    import javax.xml.transform.dom.*;
    import javax.xml.transform.stream.*;
    
    public class pepXMLConverter {
    	public static void main(String[] args) throws Exception {
    
    		// Make new Document
    		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    		DocumentBuilder docBuilder = dbf.newDocumentBuilder();
    		Document doc = docBuilder.newDocument();
    
    		//
    		ArrayList<Float> arrayList = new ArrayList<Float>();
    
    		// Create and add a root element.
    		Element elt = doc.createElement("root");
    		doc.appendChild(elt);
    
    		try {
    			// Create an instance for the file that needs to be loaded in the script.
    			Scanner in = new Scanner(System.in);
    			System.out.print("Enter the file name: ");
    			File file1  = new File(in.next());
    			Scanner s = new Scanner(file1);
    
    			// Keeps track of the count.
    			int getal = 0;
    
    			// Create a Dictionary (Hashmap).
    			// In here we have all the monoisotopic amino acid masses.
    			Map<String, Float> y = new HashMap<String, Float>();
    			y.put("A", 71.0371f);
    			y.put("R", 156.1011f);
    			y.put("N", 114.0429f);
    			y.put("D", 115.0269f);
    			y.put("C", 103.0091f);
    			y.put("E", 129.0425f);
    			y.put("Q", 128.0585f);
    			y.put("G", 57.0214f);
    			y.put("H", 137.0589f);
    			y.put("I", 113.0840f);
    			y.put("L", 113.0840f);
    			y.put("K", 128.0949f);
    			y.put("M", 131.0404f);
    			y.put("F", 147.0684f);
    			y.put("P", 97.0527f);
    			y.put("S", 87.0320f);
    			y.put("T", 101.0476f);
    			y.put("W", 186.0793f);
    			y.put("Y", 163.0633f);
    			y.put("V", 99.0684f);
    
    			// We read the lines from file1 one by one.
    			while (s.hasNextLine()) {
    				String line = s.nextLine();
    
    				// Skip the first line, and start with the second line.
    				if (getal >= 1) {
    
    					// Create the main tags:
    					// Create Child0.
    					Element child0 = doc.createElement("spectrum_query");
    					elt.appendChild(child0);
    					// Create Child1.
    					Element child1 = doc.createElement("search_result");
    					child0.appendChild(child1);
    					// Create Child2.
    					Element child2 = doc.createElement("search_hit");
    					child1.appendChild(child2);
    					// Create Child3.
    					Element child3 = doc.createElement("search_score");
    					child2.appendChild(child3);
    					// Create Child4.
    					Element child4 = doc.createElement("search_score");
    					child2.appendChild(child4);
    					// Create Child5.
    					Element child5 = doc.createElement("search_score");
    					child2.appendChild(child5);
    
    					// Read each line and seperate the words.
    					// After a 'tab space' the word gets seperated.
    					// -1 makes sure 'blank' field are alo taken with it, and not skipped.
    					String[] items = line.split("\t", -1);
    					String specFileInfo = items[0];
    					String scanInfo = items[1];
    					String actMethodInfo = items[2];
    					String precursorInfo = items[3];
    					String chargeInfo = items[4];
    					String peptideInfo = items[5];
    					String proteinInfo = items[6];
    					String deNovoScoreInfo = items[7];
    					String msgfScoreInfo = items[8];
    					String specProbInfo = items[9];
    					// Add some other values to specFileInfo2.
    					String specFileInfo2 = specFileInfo + "." + scanInfo + "." + scanInfo;
    
    					// Sperate the peptideInfo input in three sections.
    					// Ater a '.' the word gets seperated.
    					// -1 makes sure 'blank' field are alo taken with it.
    					String[] pept = peptideInfo.split("\\.",-1);
    					String peptide_prev_aaInfo = pept[0];
    					String peptide_zelfInfo = pept[1];
    					String peptide_next_aaInfo = pept[2];
    
    					// Calculate the peptide mass.
    					for (int i = 0; i < peptide_zelfInfo.length(); i++) {
    					   String ss = peptide_zelfInfo.substring(i, i +1);
    					   float mass = y.get(ss);
    					   float massaInfo = mass + y.get(ss);
    					   // Add these values to the arrayList.
    					   arrayList.add(massaInfo);
    					}
    					// Get the last item that was stored in the arrayList.
    					Float massaInfo2 = arrayList.get(arrayList.size()-1);
    
    					// Add Attributes to the child0 tag.
    					child0.setAttribute("spectrum", specFileInfo2);
    					child0.setAttribute("start_scan", scanInfo);
    					child0.setAttribute("end_scan", scanInfo);
    					child0.setAttribute("precursor_neutral_mass", precursorInfo);
    					child0.setAttribute("assumed_charge", chargeInfo);
    					child0.setAttribute("index", "1");
    					child0.setAttribute("retention_time_sec", "0.0");
    
    					// Add Attributes to the child2 tag.
    					child2.setAttribute("hit_rank", "1");
    					child2.setAttribute("peptide", peptide_zelfInfo);
    					child2.setAttribute("peptide_prev_aa", peptide_prev_aaInfo);
    					child2.setAttribute("peptide_next_aa", peptide_next_aaInfo);
    					child2.setAttribute("protein", proteinInfo);
    					[COLOR="blue"]child2.setAttribute("calc_neutral_pep_mass", massaInfo2);[/COLOR]
    					child2.setAttribute("massdiff", "NOG DOEN!");
    
    					// Add Attributes to the child3, child4 and child5 tags.
    					child3.setAttribute("name", "DeNovoScore");
    					child3.setAttribute("value", deNovoScoreInfo);
    					child4.setAttribute("name", "MSGFScore");
    					child4.setAttribute("value", msgfScoreInfo);
    					child5.setAttribute("name", "SpecProb");
    					child5.setAttribute("value", specProbInfo);
    				}
    
    				// ++ adds +1 every line.
    				getal++;
    			}
    
    			// Set up a transformer.
    			TransformerFactory transFac = TransformerFactory.newInstance();
    			Transformer trans = transFac.newTransformer();
    			trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    			trans.setOutputProperty(OutputKeys.INDENT, "yes");
    
    			StringWriter sw = new StringWriter();
    			// Save to file.
    			StreamResult result = new StreamResult(new File("Converter_Output.pep.xml"));
    			DOMSource source = new DOMSource(doc);
    			trans.transform(source, result);
    
    		} catch (FileNotFoundException e) {
    			e.printStackTrace();
    		}
    	}
    }

  2. #2
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    7

    Default

    massaInfo2 is a Float, and you're trying to pass it to a method that is expecting a String for that argument.

  3. #3
    Lund01's Avatar
    Lund01 is offline Senior Member
    Join Date
    Sep 2010
    Location
    The Netherlands
    Posts
    149
    Rep Power
    0

    Default

    But where did I then say I expect a string?

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,449
    Rep Power
    25

    Default

    Read the API doc for the setAttribute() method.
    What are the data types of its arguments.
    Look at your code where the error is.
    Does that call to setAttribute() use the correct data types?
    The compiler does NOT think it does.

  5. #5
    Lund01's Avatar
    Lund01 is offline Senior Member
    Join Date
    Sep 2010
    Location
    The Netherlands
    Posts
    149
    Rep Power
    0

    Default

    I have looked at the setAttribute.
    It appears that there is a getAttributeAsFloat(), but no setAttributeAsFloat().

    I also looked at the info on setAttribute:
    Java Code:
    setAttribute
    
    public void setAttribute(java.lang.String name,
                             float value)
    
        Sets the specified float attribute.
    
        Parameters:
            name - the name of the attribute.
            value - the float value for the specified attribute.
        See Also:
            getAttribute(String, float)
    I also tried putting an f behind the
    Java Code:
     
    child2.setAttribute("calc_neutral_pep_mass", massaInfo2[COLOR="blue"]f[/COLOR]);
    But no luck eather...

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,449
    Rep Power
    25

    Default

    What class and package is the class for the setAttribute() API doc that you looked up and displayed in post #5?
    Is it the class that you are using?

  7. #7
    Lund01's Avatar
    Lund01 is offline Senior Member
    Join Date
    Sep 2010
    Location
    The Netherlands
    Posts
    149
    Rep Power
    0

    Default

    I got it from this website:
    SimpleContent

    I also added
    import java.lang.*;

    But I still get that same error.

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,449
    Rep Power
    25

    Default

    Is child2 an instance of that SimpleContent class? Where is the import statement for that class?

    I thought child2 was an instance of the org.w3c.dom.Element class.

    Java Code:
    Element child2 = doc.createElement("search_hit");

  9. #9
    Lund01's Avatar
    Lund01 is offline Senior Member
    Join Date
    Sep 2010
    Location
    The Netherlands
    Posts
    149
    Rep Power
    0

    Default

    I don't know if I understand your last message...
    Did I forget an import?

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,449
    Rep Power
    25

    Default

    Do you understand the purpose of the import statement?
    It is used by the compiler to find classes that are defined in packages.

    What package is the child2 object a member of? It appears to be an instance of the Element class/interface

    You have looked up the API doc for another class, not the one you are using. You need to use the API doc for the classes you are using. Here is a link: Java Platform SE 6

  11. #11
    Lund01's Avatar
    Lund01 is offline Senior Member
    Join Date
    Sep 2010
    Location
    The Netherlands
    Posts
    149
    Rep Power
    0

    Default

    I have found an answer!
    I added
    Java Code:
     
    String massaInfo3 = ""+massaInfo2;
    Then the output is a string again, and het prints in in my XML output.

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,449
    Rep Power
    25

    Default

    Crude but effective.

  13. #13
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    7

    Default

    Quote Originally Posted by Lund01 View Post
    I have found an answer!
    I added
    Java Code:
     
    String massaInfo3 = ""+massaInfo2;
    Then the output is a string again, and het prints in in my XML output.
    That's fine if you understand why what you were doing before was causing an error.

  14. #14
    Lund01's Avatar
    Lund01 is offline Senior Member
    Join Date
    Sep 2010
    Location
    The Netherlands
    Posts
    149
    Rep Power
    0

Similar Threads

  1. Replies: 3
    Last Post: 04-18-2011, 11:33 PM
  2. I do not understand this error with charAt()
    By AcousticBruce in forum New To Java
    Replies: 20
    Last Post: 12-25-2010, 06:19 PM
  3. HELP ...DONT UNDERSTAND Error
    By ask4soteria in forum New To Java
    Replies: 6
    Last Post: 11-26-2010, 09:43 AM
  4. I don't understand the error ... (help!)
    By XmisterIS in forum New To Java
    Replies: 3
    Last Post: 09-02-2010, 09:33 AM
  5. i don understand this error
    By Deon in forum New To Java
    Replies: 4
    Last Post: 01-12-2008, 10:03 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •