• Post Reply Bookmark Topic Watch Topic
  • New Topic

Array Hasstles  RSS feed

 
James Constable
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ive been mucking about with a chemistry education tool that requires element[113] to be empty. The array is in 3 parts
int
double
String

ive never been good at Arrays and this is no exception
The array is below
public static double[] mass = {1.0079, 4.0026, 6.941, 9.0122, 10.811, 12.011, 14.007, 15.999, 18.998, 21.180,
22.990, 24.305, 26.982, 28.086, 30.974, 32.065, 35.453, 39.948, 39.098, 40.078,
44.956, 47.867, 50.942, 51.996, 54.938, 55.845, 58.933, 58.693, 63.546, 65.39,
69.723, 72.61, 74.922, 78.96, 79.904, 83.80, 85.468, 87.62,0 , 88.906,91.224,
92.906, 95.94, 98, 101.07, 102.91, 106.42, 107.87, 112.41, 114.82, 118.71,
121.76, 127.60, 126.90, 131.29, 132.91, 137.33, 138.91, 140.12, 144.24, 145,
150.36, 151.96, 157.25, 258.93, 162.50, 164.93, 167.26, 168.93, 173.04, 174.97,
178.49, 180.95, 183.84, 186.21, 190.21, 192.22, 195.08, 196.97, 200.59, 204.38,
207.2, 208.98, 209, 210, 222, 223, 226, 227, 232.04, 231,04, 238.03, 237, 244,
243, 247, 247, 251, 252, 257, 258, 259, 262, 261, 262, 266, 264, 269, 268, 271,
272, 277, , 289};
public static int[] place = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23,
24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44,
45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65,
66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86,
87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105,
106, 107, 108, 109, 110, 112, 113, 114};
public static String[] element = {hydrogen, helium, lithium, beryllium, boron, carbon, nitrogen, oxygen,
flourine, neon, sodium, magnesium, aluminium, silicon, phosphorus, sulphur,
chlorine, argon, potassium, calcium, scandium, titanium, vanadium, chromium,
iron, cobalt, nickel, copper, zinc, gallium, arsenic, selenium, bromine,
krypton, rubidium, strontium, yttrium, zirconium, niobium, molybdenum,
technetium, ruthenium, rhodium, palladium, silver, cadmium, indium, tin,
antimony, tellurium, iodine, xenon, caesium, barium, lanthanum, praseodymium,
neodymium, promethium, samarium, europium, gadolinium, terbium, dysprosium,
holmium, erbium, thulium, ytterbium, lutetium, hafnium, tantalum, tungsten,
rhenium, osmium, iridium, platinum, gold, mercury, thallium, lead, bismuth,
polonium, astatine, radon, francium, radium, actinium, thorium, protactinium,
uranium, neptunium, plutonium, americium, curium, berkeilum, californium,
einsteinium, fermium, mendelevium, nobelium, lawerncium, rutherfordium, dubnium,
seaborgium, bohrium, hassium, meitnerium, ununnillium, unununium, ununbium, ,
ununquadium };
ive tried seeding 113 with a 0 but i still cant get it to compile
please help
btw this is a personal project not homework (im a chemistry geek)
im just trying to put something out there as a learning tool
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all, you forgot to put double quotations on your strings.
About the blank, do you mean between ununbium and ununquadium ? Then you should write "ununbium", null, "ununquadium" instead.



You can then check if the element is null or not, like

[ December 14, 2007: Message edited by: Christophe Verre ]
 
James Constable
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok Thanks heaps ill try it asap
 
James Constable
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried it, it worked with the String part but i got an incompatible error with the double something like this

C:\Users\Roujin\Documents\JavaProgramming\Virtunet\Education\twentyElements.java:22: incompatible types
found : <nulltype>
required: double
272, 277,null , 289};
was worth a try though thanks anyway
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For the int and double arrays, you'd use 0, not null.

Using multiple arrays like this in parallel is almost always a bad idea. It's hard to work with, hard to get right. Why now create a small class named "Element", which has name, atomicNumber, and atomicWeight properties, and then have one single array (or better, a java.util.ArrayList) full of Elements? This keeps the related values together in one object.
 
Bill Shirley
Ranch Hand
Posts: 457
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
wow, that's a nightmare, good luck,

you should take the opportunity to redesign without arrays,
and read that data in from a resource file

1 hydrogen 1.0012

etc.
then the data is more easily maintained/confirmed,
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, it isn't a nightmare. It is really quite simple. There are 114 elements, so set up a 115-member array. You can then fit them all in missing out 0 and 113; these will remain null. Remember that arrays start with index 0 and all their members default to null for objects or 0 for primitive numbers or false for boolean primitives. That way you can avoid having elements[1] = helium, elements[2] = beryllium.

Then, as Ernest Friedmann-Hill has told you (I would have told you, only the Ranch website went down and lost my post earlier today), create an Element class with (probably final) fields for atomic number, name, symbol ("H", "He" etc) and atomic mass.
Then you can put them into the array in order. You will unfortunately have to write out or copy-and-paste all the names and numbers you write earlier.

Tell us how you get on.
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But Bill Shirley's idea about writing all the data into a resource file and reading them into your application is a very good idea.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!