Johnson Johnson - 1 month ago 9
Java Question

Getting the index position from an array of strings

Prerequisites: has to be single dimension arrays and it can't be HashMaps or ArrayLists

Preface:

I am currently stuck and have been stuck for quite some time now. I really need some assistance or different thought process right now. So I have two arrays that both contain strings. The function of the program is to take user input, check to see if it exists in one array and output the same exact index position of second array in respect to the first array. For example if the user types a word that exists as array1[5] it should output the word located at array2[5]. I am currently to the point where my program can accurately determine whether or not the user input matches a string in my first array.

Problem:

So the part i'm stuck on is getting the index integer value from the first array and getting it to use the same integer value to draw the same position in the second array. In general terms I need to know how to store the value of x in array1[x] and store it as a variable and print out array2[x] and basically have them match.

public void actionPerformed( ActionEvent ae ){
Object obj = ae.getSource();

if ( obj == b1)
{
input = tf1.getText();

for (e=0; e<english.length; e++)
{
if (!input.equals(english[e]))
{
translate = false;

}

else if (input.equals(english[e]))
{
output = swedish[index];

translate = true;
repaint();
break;


}

}


}

Answer

You are accessing the second array via swedish[index]however there is no index variable anywhere in your code. I think it should be swedish[e].

This way you are using the exact same index to access the second array, when you have a match in the first array.

Comments