lundi 23 novembre 2020

how to get a random String from a String array

So basically I'm doing a Movie plan maker and I have to set in a text box a couple movies and then press a button to add the movie to the array, then I have to do the same thing with a snack, at the end I have to tipe the name of the movie and when I press a button for show me the movie it has to show me the movie that I type in and get a random snack, my issue is I don't know how to get a random snack, also my both arrays (movie and snack) has to have 50 spaces in the array and I does not matter if the snack repeat.

package com.example.movienightplanneracastillo;

import java.util.Random;

public class MovieTheater {

    Random random = new Random();

    private String[] Movies = new String[50];
    private String[] Snack = new String[50];
    private int nextEmptyIndex = 0;

    public boolean addMovie(String movie) {
        if (nextEmptyIndex < Movies.length) {
            Movies[nextEmptyIndex] = movie;
            nextEmptyIndex++;
            return true;
        } else {
            return false;
        }
    }

    public boolean addSnack(String snack) {
        if (nextEmptyIndex < Snack.length) {
            Snack[nextEmptyIndex] = snack;
            return true;
        } else {
            return false;
        }
    }

    public String findAllMatches(String name) {
        String namesFound = "";

        for (int i = 0; i < nextEmptyIndex; i++) {
            if (Movies[i].toLowerCase().contains(name.toLowerCase())) {
                namesFound = namesFound + Movies;
            }
        }

        return namesFound;
    }

    public String getMovie(String movieName) {
        int Index = -1;
        for(int i=0;i<Movies.length;i++) {
            if (Movies[i].equals(movieName));
            Index = i;
            break;
        }
        if(Index == -1) {
            return "";
        }
        else {
            return Movies[Index];
        }
    }
}

this is the code for my MovieTheater class if it helps in something.




Aucun commentaire:

Enregistrer un commentaire