Reputation: 149
I am new to Java and am trying to write some simple code that finds the object (solvent) selected on a combo box, and then displays the number (shift value) associated with it when the button called shift is pressed on the form.
I have set it up so that I have an array with the solvents and their corresponding shift values. I convert the combo box object to a String, store it as the string 'chosen' and then try to check where this 'chosen' string is found in the array. To do this, I converted an array to an ArrayList
and used indexOf
.
This code throws an ArrayIndexOutOfBoundsException
because the int position always comes out as -1
, so when I try to set the display text as solvents[position][1]
it comes out as solvents[-1][1]
which doesn't work.
My question is why is -1 coming up? I know this means that the String is not found within the list, but it should be. (i.e. the list should look like: CDCl3, H2O ..., etc. so the String
'chosen' which is "CDCl3"
is within that list.)
Also, even if this did work and the position = -1
didn't come up, I am aware my next line textDisplay.setText(solvents[position][1]);
would have to be modified as now the position refers to the position in an ArrayList
, not in an array. Does anyone have any ideas? Thanks.
String[][] solvents = new String[4][2];
String chosen;
public void populate_array() {
solvents[0][0] = "CDCl3";
solvents[1][0] = "H2O";
solvents[2][0] = "CD2Cl2";
solvents[3][0] = "DMSO";
solvents[0][1] = "7.26";
solvents[1][1] = "4.79";
solvents[2][1] = "5.32";
solvents[3][1] = "2.50";
}
public int solvent_position() { // finding where in the array the solvent is
int col_length = solvents[0].length;
int row_length = solvents.length;
int i = 0;
int position = -1;
int j = 0;
populate_array();
for (i = 0; i < row_length; i++) { // go through each row to find the solvent
if (solvents[i][0].contains(chosen)) {
position = Arrays.asList(solvents).indexOf(chosen);
textDisplay.setText(solvents[position][1]); // trying to the display the number associated with the solvent
}
}
return position;
}
private void shift_buttonActionPerformed(java.awt.event.ActionEvent evt) {
chosen = (String) combo_solvent.getSelectedItem();
solvent_position();
}
Upvotes: 2
Views: 1689
Reputation: 167
I would suggest you rewrite the code to use a map, that will greatly simplify your code and you won't need to deal with all the indexes or manually look for the chosen solvent.
It seems you are interested in the shift value, not in the specific position, to obtain the shift value just do it like in the example below.
I'm using strings for both values but you could be using a float for the shift value.
//Create the map with the values
HashMap<String, String> solvents = new HashMap<String, String>();
//Populate the data in your map
solvents.put("CDCl3", "7.26");
solvents.put("H2O", "4.79");
//Obtain the shift value, null if the element wasn't found
String shiftvalue = solvents.get(chosen);
Upvotes: 3
Reputation: 1
You have used two dimensional string array but you put only one String for each position. That's why your array list can not catch the value of the array and returning -1 as an else execution.
Upvotes: 0
Reputation: 16379
You have a problem at the line Arrays.asList(solvents).indexOf(chosen);
You are trying to convert a two dimensional array into a List
. Here you created a list of one dimensional array. And apparently, chosen
is a string not an array. So, since the string can never be inside a List of arrays, you are getting -1 as the index.
To solve your problem, I suggest you to use a different approach by creating a class as below:
public class Solvent{
public String name;
public float solubility; // or whatever your number is..
public Solvent(String name, float solubility){
this.name = name;
this.solubility = solubility;
}
}
And in your main code:
List<Solvent> solvents = new ArrayList<Solvent> solvents;
solvents.add(new Solvent("H20", 4.79));
//add others.
Now to get the position:
public int getPosition(String name){
for(Solvent solvent: solvents){
if(name.equals(solvent.name)){
return solvents.indexOf(solvent);
}
}
return -1;
}
Upvotes: 0
Reputation: 57114
Your current logic makes no sense. solvents[i][0].contains(chosen)
will check if the first element of the array in solvents[i]
string-wise contains chosen
, "ABC".contains("B")
-> true
, almost sure that is not what you want.
Arrays.asList(solvents)
will be a list of String[]
, not a list of String
. That is why indexOf
will never find a String
in it. (a proper IDE will even tell you that)
That being said: I do not know what you are trying to do, probably you should iterate using 2 for-loops, drop contains
and Arrays.asList
but use equals
to compare the Strings
. But still the return type is unclear, an int
is not enough to hold the entire index information.
Upvotes: 0