Page 1 of 2 12 LastLast
Results 1 to 20 of 27
  1. #1
    Lund01's Avatar
    Lund01 is offline Senior Member
    Join Date
    Sep 2010
    Location
    The Netherlands
    Posts
    149
    Rep Power
    0

    Default arrayList with numbers in it.

    I have a program that has an arrayList.
    And in that arrayList there are numbers. (4,9762 - 36,973 - 7,0923 )
    How do I add all these values that are in the arrayList to one number.
    Like: 4,9762 + 36,973 + 7,0923 = answer.

    I am kinda stuck on that one.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default

    To get better help sooner, post a SSCCE (Short, Self Contained, Compilable and Executable) example that demonstrates where exactly you are stuck.

    db

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

    Default

    The file I am working with is very large.
    But this is the part of the code that I am stuck with:

    Java Code:
    		if (arrayList.size() >0) {
    			Float ALMValues = arrayList.get(arrayList.size()-1);
    			som = som + ALMValues;
    		}
    I know the fault is in here.
    But I don't see it.

    som should be the answer of: 4,9762 + 36,973 + 7,0923.

  4. #4
    Iron Lion is offline Senior Member
    Join Date
    Nov 2010
    Posts
    210
    Rep Power
    5

    Default

    All you're doing there is getting the last value in the ArrayList. What you want to do is iterate over every value. Here's some pseudocode:

    Java Code:
    for each element in list
        add that element to total
    print total

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

    Default

    I was thinking that this: som = som + ALMValues;
    Would add the numbers up, and that on the end sum would be all numbers combined.
    How should I add the first to the rest then?
    With ++ or something.

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,883
    Rep Power
    25

    Default

    som = som + ALMValues;
    That's close. Replace the last operand with code that gets the next element from the arraylist
    If it were an array you might code:
    som = som + ALMValues[i];

  7. #7
    Iron Lion is offline Senior Member
    Join Date
    Nov 2010
    Posts
    210
    Rep Power
    5

    Default

    Look at the section on "enhanced for" in this tutorial. You have the right idea, but you need to do it for every item in the list, not just the last one.

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

    Default

    I now have this:

    Java Code:
     
    					   if (arrayListMass.size() >0) {
    						   Float ALMValues = arrayListMass.get(arrayListMass.size()-1);
    						   for (float x = 0; x <= ALMValues; x++) {
    							   som = som + x;
    						   }
    					   }
    But the first calculation I get 0,0 back.

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

    Default

    Will look at those options too. :)

  10. #10
    Iron Lion is offline Senior Member
    Join Date
    Nov 2010
    Posts
    210
    Rep Power
    5

    Default

    Quote Originally Posted by Lund01 View Post
    I now have this:

    Java Code:
     
    					   if (arrayListMass.size() >0) {
    						   Float ALMValues = arrayListMass.get(arrayListMass.size()-1);
    						   for (float x = 0; x <= ALMValues; x++) {
    							   som = som + x;
    						   }
    					   }
    But the first calculation I get 0,0 back.
    You're not using the loop to do anything with the ArrayList. All you're adding to the sum each time is the loop counter.

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

    Default

    We have been searching a little more.
    And have tried making an array of it. But I don't think that would be the solution for the program.
    Although I now know how that works.

    But we still can't do it with an arrayList.
    We are looking at this problem with 2 of us, but we both don't see it.
    (Kinda stupid huh)

  12. #12
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,883
    Rep Power
    25

    Default

    Two things to consider:
    You need a loop
    You need to get the value of each item in the array

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

    Default

    I now know how to to this with an array.
    But we still have a problem.

    float fl[] = {2.5f, 4.5f, 8.9f, 5.0f, 8.9f};

    Here we have put the values in the array.
    But I would like to have done this atomaticly.
    As the program has a peptide and looks it up is a HashMap.
    Then those number like 2.5 and 8.9 should be stored in the array.
    And of all those sumbers I need the sum.

    But how do I put these numbers in an array?
    How do I add them?

  14. #14
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,883
    Rep Power
    25

    Default

    how do I put these numbers in an array?
    What is wrong with this:
    Java Code:
    float[] fl = {2.5f, 4.5f, 8.9f, 5.0f, 8.9f};
    How do I add them?
    Use a loop to add each element to the total

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

    Default

    I have now made this.
    I have an arrayList that contain the value.
    And that one I made to an array.
    I also got it that it is counting the numbers, so a big improvement.
    But when printing he still prints only the value of the first element and not the sum.

    Java Code:
     
    					// Calculate the neutral peptide mass.
    					for (int i = 0; i < peptide_zelfInfo.length(); i++) {
    					   String su = peptide_zelfInfo.substring(i, i +1);
    					   float mass = y.get(su);
    					   arrayListMass.add(mass);
    
    					   // Convert arrayList to array. 
    					   Object ia[] = arrayListMass.toArray();
    					   float sum1 = 0;
    					   // Count 
    					   for(int t=0;t<ia.length;t++) {
    						   sum1+=((Float)ia[t]).floatValue();
    						   //
    						   System.out.println("Sum is :" + sum1);
    						   // Convert sum1 to a String value.
    						   String sum2 = ""+sum1;
    						   // Add this value to the arrayList.
    						   arrayList.add(sum2);
    					   }
    					}
    					// Get the item that was stored in the arrayList.
    					String massaInfo = arrayList.get(0);
    In arrayListMass we got the values like 2.973, 63.27362.

    So massaInfo schould be the sum of all elements but that is not the case, it is the first value of arrayListMass. What is going wrong here?

  16. #16
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,883
    Rep Power
    25

    Default

    Remove ALL the code not needed for summing the arraylist and just post that.
    Also post the full output from your code. It looks like it prints the sum as it is computed.
    Java Code:
    					String massaInfo = arrayList.get(0);
    What is that line of code supposed to do?
    How many elements are in the arraylist?
    What is in the first element?
    What is in the second element?
    What is in the last element?

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

    Default

    What i think is the problem:
    What if I have an arrayList and in there is only one number.
    How do I get that out?
    Java Code:
     String massaInfo = arrayList.get(0);
    Would this be wrong then?

  18. #18
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,883
    Rep Power
    25

    Default

    Look at the length of the list and you will be able to get out single element.

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

    Default

    I have changed the last sentence to:
    Java Code:
    String massaInfo = arrayList.get(arrayList.size()-1);
    When I print sum1 I have the values that I need.
    With the arrayList I try to get that value out of the loop and printed in the xml output file.
    But when it is printed in the xml file all values for calc_neutral_pep_mass are all given as 1413.

    If it help I can post the entire code here.
    It is a program that I made earlyer so I think you have seen it before.
    But we needed made some changes in it.

    Java Code:
     
    /**
    * Autheur: Oddens, Ilse X.M; Leenheer, Dennis.
    * 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 an .pep.xml file.
    * Programmed on: Windows XP
    */
    
    import java.io.*;
    import java.util.*;
    import java.lang.*;
    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();
    
    		// Create an arrayList.
    		ArrayList<String> arrayList = new ArrayList<String>();
    		ArrayList<Float> arrayListMass = new ArrayList<Float>();
    
    
    		// Define som.
    		//Double som = 0.00;
    		float som = 0;
    		//float sum = 0.0f;
    
    		// Create and add a root element.
    		Element elt = doc.createElement("msms_pipeline_analysis");
    		doc.appendChild(elt);
    
    		/**
    		// Comment for in the pep.XML file.
    		Comment comment = doc.createComment("?xml version=\"1.0\"?");
    		elt.appendChild(comment);
    		*/
    
    		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 neutral peptide mass.
    					for (int i = 0; i < peptide_zelfInfo.length(); i++) {
    					   String su = peptide_zelfInfo.substring(i, i +1);
    					   float mass = y.get(su);
    					   arrayListMass.add(mass);
    
    					   //
    					   Object ia[] = arrayListMass.toArray();
    					   float sum1 = 0;
    					   //
    					   for(int t = 0; t < ia.length; t++) {
    						   sum1+=((Float)ia[t]).floatValue();
    						   //
    						   System.out.println("Sum is :" + sum1);
    						   // Convert sum1 to a String value.
    						   String sum2 = ""+sum1;
    						   // Add this value to the arrayList.
    						   arrayList.add(sum2);
    
    						   //System.out.println(arrayList.size());
    
    					   }
    					}
    
    					// Get the item that was stored in the arrayList.
    					String massaInfo = arrayList.get(arrayList.size()-1);
    					// Convert the massaInfo value to a Float value.
    					float massaInfo1 = Float.parseFloat(massaInfo);
    
    					// Calculate the massDiff.
    					// Transfor the String to an float.
    					float a=Float.parseFloat(precursorInfo);
    					float massDiffInfo = a/massaInfo1;
    					// Transform the float to an String for in the XML file.
    					String massDiffInfo1 = ""+massDiffInfo;
    
    					// 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);
    					child2.setAttribute("calc_neutral_pep_mass", massaInfo);
    					child2.setAttribute("massdiff", massDiffInfo1);
    
    					// 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);
    
    					// Empty the arrayLists.
    					arrayList.clear();
    					arrayListMass.clear();
    				}
    
    				// ++ 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();
    		}
    	}
    }

  20. #20
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,883
    Rep Power
    25

    Default

    Can you explain your problem in a simpler way? For example:
    The program outputs 1234 but it should output 2134

    Show the program's output and explain what is wrong with it line by line where there is an error.

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 12
    Last Post: 07-07-2011, 09:49 PM
  2. Replies: 11
    Last Post: 01-14-2011, 07:36 PM
  3. Replies: 7
    Last Post: 11-22-2010, 02:26 PM
  4. printing two smallest numbers from a series of numbers
    By trofyscarz in forum New To Java
    Replies: 2
    Last Post: 10-15-2008, 12:46 AM
  5. Replies: 2
    Last Post: 04-21-2008, 12:43 PM

Posting Permissions

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