public class Translation {
public static void main(String[] args)
{
String[] eng = {"hello", "goodbye", "cat", "dog"};
String[] fre = {"bonjour", "au revoir", "le chat", "le chien"};
String word;
word = JOptionPane.showInputDialog("Enter word");
int result = sequentialSearch(eng, word);
if(result == -1)
{
JOptionPane.showMessageDialog(null,"S... Word not found");
}
else
{
JOptionPane.showMessageDialog(null,"F... Translation: "+fre[result]);
}
}
public static int sequentialSearch(String [] words, String target)
{
int index=0; boolean found = false;
for(int i=0;i
{
if(target.equals(words[i]))
{
found = true;
index =i;
break;
}
}
if(found)
return index;
else
return -1;
}
}
I'm currently going through an old CS class learning assignment that I found online. I need to use arrays to convert English words to French and vice versa using 2 string arrays and a linear search. I set up the linear search but I'm having difficulty setting up the process to draw the output from the 2nd array. Below is what I put together so far but as I said, I know that I need to reference the first index and pull the corresponding item in the other, but I can't seem to piece the code together.
I'm stumped on drawing the output from the input. Any guidance is appreciated!
import java.text.*; // general package for formatting
import javax.swing.*; // for GUI
public class Translation
{
public static void main(String[] args)
{
String[] eng = {"hello", "goodbye", "cat", "dog"};
String[] fre = {"bonjour", "au revoir", "le chat", "le chien"};
String word;
word = JOptionPane.showInputDialog("Enter word");
sequentialSearch(eng, word);
}
public static int sequentialSearch(new words[], int target)
{
int index;
int element;
boolean found;
index = 0;
element = -1;
found = false;
while (!found && index < words.length)
{
if (words[index] == target)
{
found = true;
element = index;
}
index++
}
return element;
}
}