1

I have a little issue with linking File and String arrays together..... I want to match each element of the File array to its corresponding number in the String array, E.g (Match word[1] to answer[1]) I don't have an idea about that.... thanks for the help!

private static File[] word = 

    { new File ("C:\\Users\\HP\\Downloads\\Word Pronunciation\\audio.mp3"), 
        new File ("C:\\Users\\HP\\Downloads\\Word Pronunciation\\baby.mp3"),
    new File ("C:\\Users\\HP\\Downloads\\Word Pronunciation\\board.mp3"),
    new File ("C:\\Users\\HP\\Downloads\\Word Pronunciation\\bomb.mp3"),
    new File ("C:\\Users\\HP\\Downloads\\Word Pronunciation\\gym.mp3"),
    new File ("C:\\Users\\HP\\Downloads\\Word Pronunciation\\football.mp3"),
    new File ("C:\\Users\\HP\\Downloads\\Word Pronunciation\\school.mp3"),
    new File ("C:\\Users\\HP\\Downloads\\Word Pronunciation\\keyboard.mp3"),
    new File ("C:\\Users\\HP\\Downloads\\Word Pronunciation\\computer.mp3"),
    new File ("C:\\Users\\HP\\Downloads\\Word Pronunciation\\name.mp3"), 
    new File ("C:\\Users\\HP\\Downloads\\Word Pronunciation\\lady.mp3"),
    new File ("C:\\Users\\HP\\Downloads\\Word Pronunciation\\church.mp3"), 
    new File ("C:\\Users\\HP\\Downloads\\Word Pronunciation\\sport.mp3"), 
    new File ("C:\\Users\\HP\\Downloads\\Word Pronunciation\\beauty.mp3"), 
    new File ("C:\\Users\\HP\\Downloads\\Word Pronunciation\\radio.mp3"),
    new File ("C:\\Users\\HP\\Downloads\\Word Pronunciation\\prince.mp3"),
    new File ("C:\\Users\\HP\\Downloads\\Word Pronunciation\\hearing.mp3"),
    new File ("C:\\Users\\HP\\Downloads\\Word Pronunciation\\worship.mp3"),
    new File ("C:\\Users\\HP\\Downloads\\Word Pronunciation\\song.mp3"),
    new File ("C:\\Users\\HP\\Downloads\\Word Pronunciation\\flower.mp3"),
    new File ("C:\\Users\\HP\\Downloads\\Word Pronunciation\\water.mp3"),
    new File ("C:\\Users\\HP\\Downloads\\Word Pronunciation\\nature.mp3"),
    new File ("C:\\Users\\HP\\Downloads\\Word Pronunciation\\goal.mp3"),
    new File ("C:\\Users\\HP\\Downloads\\Word Pronunciation\\manifest.mp3"),
    new File ("C:\\Users\\HP\\Downloads\\Word Pronunciation\\election.mp3"),
    new File ("C:\\Users\\HP\\Downloads\\Word Pronunciation\\number.mp3"),
    new File ("C:\\Users\\HP\\Downloads\\Word Pronunciation\\sentence.mp3"),
    new File ("C:\\Users\\HP\\Downloads\\Word Pronunciation\\movie.mp3"),
    new File ("C:\\Users\\HP\\Downloads\\Word Pronunciation\\sound.mp3"),
    new File ("C:\\Users\\HP\\Downloads\\Word Pronunciation\\teacher.mp3"),
    new File ("C:\\Users\\HP\\Downloads\\Word Pronunciation\\speed.mp3"),
    new File ("C:\\Users\\HP\\Downloads\\Word Pronunciation\\time.mp3"),
    new File ("C:\\Users\\HP\\Downloads\\Word Pronunciation\\debate.mp3"),
    new File ("C:\\Users\\HP\\Downloads\\Word Pronunciation\\video.mp3"),
    new File ("C:\\Users\\HP\\Downloads\\Word Pronunciation\\music.mp3"),
    new File ("C:\\Users\\HP\\Downloads\\Word Pronunciation\\phone.mp3"),
    new File ("C:\\Users\\HP\\Downloads\\Word Pronunciation\\mountain.mp3"),
    new File ("C:\\Users\\HP\\Downloads\\Word Pronunciation\\drink.mp3"),
    new File ("C:\\Users\\HP\\Downloads\\Word Pronunciation\\market.mp3"),
    new File ("C:\\Users\\HP\\Downloads\\Word Pronunciation\\broom.mp3"),
    new File ("C:\\Users\\HP\\Downloads\\Word Pronunciation\\help.mp3"),
    new File ("C:\\Users\\HP\\Downloads\\Word Pronunciation\\picture.mp3"),
    new File ("C:\\Users\\HP\\Downloads\\Word Pronunciation\\princess.mp3"),
    new File ("C:\\Users\\HP\\Downloads\\Word Pronunciation\\cake.mp3"),
    new File ("C:\\Users\\HP\\Downloads\\Word Pronunciation\\river.mp3"),
    new File ("C:\\Users\\HP\\Downloads\\Word Pronunciation\\dance.mp3"),
    new File ("C:\\Users\\HP\\Downloads\\Word Pronunciation\\rely.mp3"),
    new File ("C:\\Users\\HP\\Downloads\\Word Pronunciation\\level.mp3"),
    new File ("C:\\Users\\HP\\Downloads\\Word Pronunciation\\wealth.mp3"),
    new File ("C:\\Users\\HP\\Downloads\\Word Pronunciation\\surname.mp3") };

    private String []answer = {"audio", "baby", "board", "bomb", "gym", "football", "school", "keyboard", "computer", "name" ,
            "lady", "church", "sport", "beauty", "radio", "prince", "hearing", "worship", "song", "flower", "water", "nature", "goal" ,
            "manifest", "election", "number", "sentence", "movie", "sound", "teacher", "speed", "time", "debate", "video", "music", 
            "phone", "drink", "market", "broom", "help", "picture", "princess", "cake", "river", "dance", "rely", "level", "wealth", "surname"};

JButton click = new JButton();

public class Beginner(){

click = new JButton("1");

click.addActionListener(new ActionListener(){
 public void actionPerformed (ActionEvent x){

    }
});
}
}
1
  • If you want to match word[1] to answer[1] you .... ehhhh .... well, you just did. They have the same index value. That's how they are matched. What was your problem again? Commented Sep 1, 2016 at 23:36

2 Answers 2

1

You're better of using a Map.

Map<String, File> mapping = new HashMap<>();
mapping.put("audio", new File("C:\\Users\\HP\\Downloads\\Word Pronunciation\\audio.mp3");
// etc...

and then get the value by using:

File file = mapping.get("audio");
Sign up to request clarification or add additional context in comments.

Comments

1

Java 8 way would be

Map<String, File> mapping = IntStream.range(0, word.length)
.collect(Collectors.toMap(i -> answer[i], i -> word[i]));

to link your arrays.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.