Results 1 to 20 of 27
Thread: arrayList with numbers in it.
- 08-27-2011, 04:28 PM #1
- 08-27-2011, 04:35 PM #2
To get better help sooner, post a SSCCE (Short, Self Contained, Compilable and Executable) example that demonstrates where exactly you are stuck.
db
- 08-27-2011, 04:50 PM #3
The file I am working with is very large.
But this is the part of the code that I am stuck with:
I know the fault is in here.Java Code:if (arrayList.size() >0) { Float ALMValues = arrayList.get(arrayList.size()-1); som = som + ALMValues; }
But I don't see it.
som should be the answer of: 4,9762 + 36,973 + 7,0923.
- 08-27-2011, 05:28 PM #4
Senior Member
- Join Date
- Nov 2010
- Posts
- 210
- Rep Power
- 3
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
- 08-27-2011, 06:20 PM #5
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.
- 08-27-2011, 06:40 PM #6
That's close. Replace the last operand with code that gets the next element from the arraylistsom = som + ALMValues;
If it were an array you might code:
som = som + ALMValues[i];
- 08-27-2011, 06:41 PM #7
Senior Member
- Join Date
- Nov 2010
- Posts
- 210
- Rep Power
- 3
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.
- 08-27-2011, 06:44 PM #8
I now have this:
But the first calculation I get 0,0 back.Java Code:if (arrayListMass.size() >0) { Float ALMValues = arrayListMass.get(arrayListMass.size()-1); for (float x = 0; x <= ALMValues; x++) { som = som + x; } }
- 08-27-2011, 06:45 PM #9
Will look at those options too. :)
- 08-27-2011, 06:55 PM #10
Senior Member
- Join Date
- Nov 2010
- Posts
- 210
- Rep Power
- 3
- 08-27-2011, 09:46 PM #11
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)
- 08-27-2011, 10:32 PM #12
Two things to consider:
You need a loop
You need to get the value of each item in the array
- 08-28-2011, 04:21 PM #13
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?
- 08-28-2011, 04:33 PM #14
What is wrong with this:how do I put these numbers in an array?
Java Code:float[] fl = {2.5f, 4.5f, 8.9f, 5.0f, 8.9f};Use a loop to add each element to the totalHow do I add them?
- 08-28-2011, 04:47 PM #15
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.
In arrayListMass we got the values like 2.973, 63.27362.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);
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?
- 08-28-2011, 04:53 PM #16
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.
What is that line of code supposed to do?Java Code:String massaInfo = arrayList.get(0);
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?
- 08-28-2011, 04:59 PM #17
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?
Would this be wrong then?Java Code:String massaInfo = arrayList.get(0);
- 08-28-2011, 05:01 PM #18
Look at the length of the list and you will be able to get out single element.
- 08-28-2011, 05:48 PM #19
I have changed the last sentence to:
When I print sum1 I have the values that I need.Java Code:String massaInfo = arrayList.get(arrayList.size()-1);
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(); } } }
- 08-28-2011, 05:55 PM #20
Similar Threads
-
ArrayList copy some of the element from one arraylist tnto another arraylist
By ralf in forum New To JavaReplies: 12Last Post: 07-07-2011, 08:49 PM -
Problem getting numbers from user and finding smallest two numbers
By radhi16 in forum New To JavaReplies: 11Last Post: 01-14-2011, 06:36 PM -
how to add Arraylist filter for a jsp page showing results from a servlet-Arraylist
By alok_sharma in forum Java ServletReplies: 7Last Post: 11-22-2010, 01:26 PM -
printing two smallest numbers from a series of numbers
By trofyscarz in forum New To JavaReplies: 2Last Post: 10-14-2008, 11:46 PM -
Java Project Trouble: Searching one ArrayList with another ArrayList
By BC2210 in forum New To JavaReplies: 2Last Post: 04-21-2008, 11:43 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks