I'm writing a program about words game.
The program is working fine but i'm having trouble in reduce the array to size 5.that mean the user only can input 5 times then the program shut down
Anyone have any ideas?

Recommended Answers

All 8 Replies

the user only can input 5 times

There are many ways to count what happens.
The most common one is to use a loop.

Not sure I understand the problem. Your user is entering input, and it's getting stored in an array - that much I've got. You're running out of space in the array, I think, and the program crashes with an ArrayIndexOutOfBounds - I'm guessing on that part. Why is the array an array of five? Where does that number come from, and would another number be better?
If you give a little more detail on the nature of the game, and how you're implementing it, it might be easier to answer this.

you simply need to apply If condition to shut the program if array index equals to 5.

Please provide little more details about the program and a piece of code so we can understand a little more what you wanted to do...!

i'm sorry.Here is the code i'm working on.

package com.project1.wordgame;

import javax.swing.JOptionPane;

public class Project1 {
    static String word;
    static String[] wordList;
    static int wordSize = 0;
    static int i = 0;

    public static void main(String[] args) {
        wordList = new String[10];
        inputWord();        
    }

    public static String inputWord() {
        while (true) {
            String word;
            word = (JOptionPane.showInputDialog(null, "Enter 10 letters: "));
            if (word == null || "".equals(word)) {
                if (isValidList(wordList)) {
                    printTotalScore();
                }
                return "";
            }
            else if (word.equals("stop") || (word.equals("STOP"))) {
                System.exit(0);// end program if stop is typed.
            } else if (isValidWord(word))
            {
                storeWord(word);
            } 
        }
    }

    public static boolean isValidWord(String w) {
        
        if (w.length() > 10) {
            JOptionPane.showMessageDialog(null, "Too much letters.");
            return false;
        }
        for (int i = 0; i < w.length(); i++)
            if (Character.isDigit(w.charAt(i))) {
                JOptionPane.showMessageDialog(null, "Enter only letters.");
                return false;
            }
        for (int i = 0; i < w.length(); i++) {
            if (w.charAt(i) == 'a' || w.charAt(i) == 'i' || w.charAt(i) == 'u'
                    || w.charAt(i) == 'e' || w.charAt(i) == 'o'
                    || w.charAt(i) == 'z' || w.charAt(i) == 'q'
                    || w.charAt(i) == 'k' || w.charAt(i) == 'v'
                    || w.charAt(i) == 'f' || w.charAt(i) == 'w') {                
            }            
            return (true);
        }
        return (false);
    }

    public static boolean isValidList(String[] list) {
        if (list == null || wordSize == 0) {
            System.out.println("Array is blank");
            System.exit(0);
        }
        return true;
    }

    public static void storeWord(String w) {
        
        String[] tmpArray = new String[wordSize];
        tmpArray = wordList;
        
        wordList = new String[wordSize + 1];
        
        for (int i = 0; i < wordSize; i++) {
            wordList[i] = tmpArray[i];
        }
        
        wordList[wordSize] = w.toLowerCase();
        
        wordSize++;
        
        JOptionPane.showMessageDialog(null, "Word added.");
    }

    public static int wordScore(String w) {
        int Cntr = 0;
        for (int i = 0; i < w.length(); i++) {
            if (w.charAt(i) == 'a' || w.charAt(i) == 'i' || w.charAt(i) == 'u'
                    || w.charAt(i) == 'e' || w.charAt(i) == 'o') {
                Cntr = Cntr + 0;
            } else if (w.charAt(i) == 'z' || w.charAt(i) == 'q') {
                Cntr = Cntr + 10;
            } else if (w.charAt(i) == 'k' || w.charAt(i) == 'v'
                    || w.charAt(i) == 'f' || w.charAt(i) == 'w') {
                Cntr = Cntr + 5;
            } else if (!(Character.isDigit(w.charAt(i))))
                Cntr = Cntr + 1;
        }
        return Cntr;
    }

    public static void printTotalScore() {
        int totalScore = 0;
        for (int k = 0; k < wordSize; k++) {
            int score = wordScore(wordList[k]);
            totalScore += score;
            System.out.println("Word : " +wordList[k] + "\t\t-\tScore : " +score);
        }
        System.out.println("---------------------------------------------------");
        System.out.println("Total Score : " + totalScore);
    }
}

Is the program working now? I don't see any comments in the code saying there is a *** PROBLEM HERE ***.

yes the problem is working and now it's unlimited input.but but i'm having trouble in reduce the array to size 5.that mean the user only can input 5 times then the program shut down.

i'm having trouble in reduce the array to size 5

Please explain.
How are you counting the number of times the user inputs something?
Can you detect when the limit has been reached and do something?

Again, why the number five? Why do you want the array size to be five?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.