mardi 10 novembre 2015

Using if-else statements with JOptionPane inside of for loops

I'm trying to write an English to Italian array translator JApplet for my Intro to Programming course. I am having trouble with getting the JOptionPane's error message to execute within the loop nested with the if-else statements when the input doesn't match any of the strings and indices in the array. The input is converted to lowercase strings to make the input case insensitive.

I've tried just an else statement, but that causes the error message to pop up ten times because of the loop. I've tried else if (i != EnglishWords.length), but that makes the applet skip right to the error message even if the equals() statement was true. It does work if you type in if-else if-else manually, as I have done in the code in case I couldn't get the loop to work, but it is required on our assignment that we use loops for arrays.

I could really use some help. Thank you for reading.

Here is the for loop:

 for (int i=0; i<=EnglishWords.length; i++)
        {
           if (finalInput.equals(EnglishWords[i])){
                 JOptionPane.showMessageDialog(null, ItalianWords[i], "Italian Word", JOptionPane.PLAIN_MESSAGE, icons[i]);
                 translatedWord = ItalianWords[i];
                 break;
                }
           else if (i == EnglishWords.length){
                 JOptionPane.showMessageDialog(null, "Sorry, word is not found", "ERROR", JOptionPane.ERROR_MESSAGE);
                 break;
                }

And here is my entire source code:

import java.awt.*;
import javax.swing.*;
import java.text.*;



/**
 * Class ArrayAssignment - write a description of the class here
 * 
 * @author 
 * @version JDK 8
 * @course 
 * @date 10-30-15
 * @URL: 
 */
public class ArrayAssignment extends JApplet 
{
    String[ ] EnglishWords = //declares initializes the text for the English words
        {"House", "Lamp", "Table", "Sofa", "Computer", "Bathroom", "Bedroom",
        "Kitchen", "Door", "Chair"};


    String[ ] ItalianWords = //declares and initializes the text for the Italian words
        {"Casa", "Lampada", "Tabella", "Divano", "Computer", "Bagno", 
            "Camera da letto", "Cucina", "Porta", "Sedia"};

    String[ ] imageFileNames = //declares and initializes the images' file na
        {"house.png", "lamp.jpg", "table.jpg", "sofa.jpg", 
        "computer.png", "bathroom.jpg", "bedroom.jpg", "kitchen.jpg", "door.jpg", 
        "chair.jpg"};

    /* 
     * Declares adn initializes the gallery size, which is the number of images in the file to be
     * added to the applet
     * 
     * The gallerySize will be used as the termination number in the
     * getImage and icon loop.
     */
    int gallerySize = 10; 

    Image img; //declaration of an alias for the Image variable
    ImageIcon[ ] icons; //declaration for the actual future images 

    Image img2; //declaratin for another image 
    ImageIcon icon; //declaration for a second icon 

    Image[ ] imgSlide; //declaration to make all the images into an array for the loops

    String EnglishInput; //declares the string to be used to define the JOptionPane input dialog box

    String switchImgOutput; //delcaration for the image switch statement 
    Image imgOutput; //declaration to later execute the if statement inside of the imgSlide loop

    String translatedWord; //variable to define the translated English word 
    /**
     * Called by the browser or applet viewer to inform this JApplet that it
     * has been loaded into the system. It is always called before the first 
     * time that the start method is called.
     */

    public void init()
    {
        setLayout(new BorderLayout()); //changes the layout to border 

        icons = new ImageIcon[gallerySize]; // load the images into memory

        //loop that retrieves all of the household images and converts them to icons 
        for (int i=0; i<gallerySize; i++)
        {
            img = getImage( getDocumentBase( ), imageFileNames[i]);
            icons[i] = new ImageIcon( img );
        }          


        img2 = getImage(getDocumentBase( ),"flags.png"); //retrieves the flag image 
        icon = new ImageIcon(img2); //converts the flag image to an icon 

        //shows a list of the available words to translate
        JOptionPane.showMessageDialog(null, EnglishWords, "Available Words to Translate Into Italian", JOptionPane.INFORMATION_MESSAGE);


        //creates a dialog box for where you will type in the English word you wish transalted to Italian 
        EnglishInput = (String)JOptionPane.showInputDialog(this, "English to Italian Translator", 
                                                     "English to Italian", JOptionPane.PLAIN_MESSAGE, 
                                                      icon, null, "enter your selected word from list here");

        //converts the input to lowercase, thereby making it case insensitive                                               
        String lowerCase = EnglishInput.toLowerCase();

        String finalInput = lowerCase; //declares that the finalInput variable is the lowercase-converted input string



        /*
         * determines the entered English word by analyzing the string
         * and connecting it to the correct
         * array index
         */
        switch (finalInput){
            case "house":
                finalInput = EnglishWords[0]; break;
            case "lamp": 
                finalInput = EnglishWords[1]; break;
            case "table":
                finalInput = EnglishWords[2]; break;
            case "sofa":
                finalInput = EnglishWords[3]; break;
            case "computer":
                finalInput = EnglishWords[4]; break;
            case "bathroom":
                finalInput = EnglishWords[5]; break;
            case "bedroom":
                finalInput = EnglishWords[6]; break;
            case "kitchen": 
                finalInput = EnglishWords[7]; break;
            case "door": 
                finalInput = EnglishWords[8]; break;
            case "chair":
                finalInput = EnglishWords[9]; break;
            }

     /*
     * Displays an output message box that 
     * has the correct translation and icon.
     * 
     * If the word doesn't exist in the translator,
     * it will display an error message. 
     */
    /*
        if (finalInput == EnglishWords[0]){
            JOptionPane.showMessageDialog(null, ItalianWords[0], "Italian Word", JOptionPane.PLAIN_MESSAGE, icons[0]);
            translatedWord = ItalianWords[0];
        }
        else if (finalInput == EnglishWords[1]){
            JOptionPane.showMessageDialog(null, ItalianWords[1], "Italian Word", JOptionPane.PLAIN_MESSAGE, icons[1]);
            translatedWord = ItalianWords[1];
        }
         else if (finalInput == EnglishWords[2]){
            JOptionPane.showMessageDialog(null, ItalianWords[2], "Italian Word", JOptionPane.PLAIN_MESSAGE, icons[2]);
            translatedWord = ItalianWords[2];
        }
         else if (finalInput == EnglishWords[3]){
            JOptionPane.showMessageDialog(null, ItalianWords[3], "Italian Word", JOptionPane.PLAIN_MESSAGE, icons[3]);
            translatedWord = ItalianWords[3];
        }
         else if (finalInput == EnglishWords[4]){
            JOptionPane.showMessageDialog(null, ItalianWords[4], "Italian Word", JOptionPane.PLAIN_MESSAGE, icons[4]);
            translatedWord = ItalianWords[4];
        }
         else if (finalInput == EnglishWords[5]){
            JOptionPane.showMessageDialog(null, ItalianWords[5], "Italian Word", JOptionPane.PLAIN_MESSAGE, icons[5]);
            translatedWord = ItalianWords[5];
        }
         else if (finalInput == EnglishWords[6]){
            JOptionPane.showMessageDialog(null, ItalianWords[6], "Italian Word", JOptionPane.PLAIN_MESSAGE, icons[6]);
            translatedWord = ItalianWords[6];
        }
         else if (finalInput == EnglishWords[7]){
            JOptionPane.showMessageDialog(null, ItalianWords[7], "Italian Word", JOptionPane.PLAIN_MESSAGE, icons[7]);
            translatedWord = ItalianWords[7];
        }
         else if (finalInput == EnglishWords[8]){
            JOptionPane.showMessageDialog(null, ItalianWords[8], "Italian Word", JOptionPane.PLAIN_MESSAGE, icons[8]);
            translatedWord = ItalianWords[8];
        }
         else if (finalInput == EnglishWords[9]){
            JOptionPane.showMessageDialog(null, ItalianWords[9], "Italian Word", JOptionPane.PLAIN_MESSAGE, icons[9]);
            translatedWord = ItalianWords[9];
        }
        else {
            JOptionPane.showMessageDialog(null, "Sorry, word is not found", "ERROR", JOptionPane.ERROR_MESSAGE);
        }
        */



        for (int i=0; i<=EnglishWords.length; i++)
        {
           if (finalInput.equals(EnglishWords[i])){
                 JOptionPane.showMessageDialog(null, ItalianWords[i], "Italian Word", JOptionPane.PLAIN_MESSAGE, icons[i]);
                 translatedWord = ItalianWords[i];
                 break;
                }
           else if (i == EnglishWords.length){
                 JOptionPane.showMessageDialog(null, "Sorry, word is not found", "ERROR", JOptionPane.ERROR_MESSAGE);
                 break;
                }
    }
}

public void paint(Graphics g){
     Image img3 = getImage( getDocumentBase( ), "dictionary.jpg" ); //retrieves the background image from file 
     g.drawImage( img3, 0,0,getWidth(), getHeight(), this ); //sets the image as a background 

     g.setColor(Color.black); //sets the color to black 
     g.setFont(new Font ("Cooper Black", Font.BOLD, 18)); //changes the font to "Times new Roman, style to BOLD, and the size to 30

     g.drawString("English Word:", 55, 175); //writes "English Word:" onto the applet window 
     g.drawString(EnglishInput, 105, 270); //draws the user input 

     g.drawString("Italian Translation:", 265, 175); //writes "Italian Translation:" onto the applet window 
     g.drawString(translatedWord, 320, 270);

          for (int i=0; i<gallerySize; i++)
        {
            img = getImage( getDocumentBase( ), imageFileNames[i]);
            imgSlide[i] = img;
        }          

             switch (switchImgOutput){
            case "house":
                imgOutput = imgSlide[0]; break;
            case "lamp": 
                imgOutput = imgSlide[1]; break;
            case "table":
                imgOutput = imgSlide[2]; break;
            case "sofa":
                imgOutput = imgSlide[3]; break;
            case "computer":
                imgOutput = imgSlide[4]; break;
            case "bathroom":
                imgOutput = imgSlide[5]; break;
            case "bedroom":
                imgOutput = imgSlide[6]; break;
            case "kitchen": 
                imgOutput = imgSlide[7]; break;
            case "door": 
                imgOutput = imgSlide[8]; break;
            case "chair":
                imgOutput = imgSlide[9]; break;
            }



     for (int i=0; i<=imageFileNames.length; i++){
         if (imgOutput == imgSlide[i]){
         g.drawImage(imgSlide[i], 200, 200, this); break;
        } 
    } 








    }
}

Aucun commentaire:

Enregistrer un commentaire