L0rDvAd3r_9
L0rDvAd3r_9

Reputation: 33

Method not printing/returning

I'm having a problem with one of my methods. I've checked it over and over again and I can't seem to find anything wrong. The problem that I have is that the method does not print within the console upon calling it. I think it might be related to the Scanner in the method.

The code for the method is:

public String cpuGuess() throws FileNotFoundException{
        Scanner fileRead = new Scanner(myGuesses);
        int line = rand.nextInt(99-1)+1;
        for(int i = 0; i <= line; i++){
            Guess = fileRead.next();
        }
        fileRead.close();
        CPUGuess = Guess;
        if(Arrays.asList(super.used).contains(CPUGuess)){
            System.out.println(cpu.cpuGuess());
        }
        else{
            StringBuffer hiddenBuff = new StringBuffer(hidden);
            for (int t = 0; t < phrase.length(); )
                if (CPUGuess == Character.valueOf(phrase.charAt(t)).toString()){
                    count++;
                }
            if (phrase.contains(CPUGuess)){
                ArrayList<Integer> indicies = new ArrayList<Integer>();
                for (int index = phrase.indexOf(CPUGuess.toString()); index >= 0; index = phrase.indexOf(CPUGuess.toString(), index + 1)){
                    indicies.add(index);
                }
                int[] index = new int[indicies.size()];
                for(int i = 0; i < indicies.size(); i++) {
                    index[i] = indicies.get(i);
                }
                for (int n = 0; n < index.length; n++){
                    hiddenBuff.setCharAt(index[n], CPUGuess.charAt(0));
                }
                hidden = hiddenBuff.toString();
                status = "Correct";
            }
            else if (!phrase.contains(CPUGuess)){
                System.out.println("Sorry, no " + Guess + "'s");
                System.out.println("Human, you're up!");
            }
            super.used.add(CPUGuess.charAt(0));
        }
        System.out.println(count + " " + CPUGuess + "s");
        count = 0;
        return hidden;
    }

I call the method in the main class under the main method:

public static void main(String[] args) throws FileNotFoundException {
    System.out.println("WHEEL... OF... FORTUNE!!!!");
    System.out.println(board.phraseGenerate());
    System.out.println(cpu.cpuGuess());
}

Please note that the cpu object has already been constructed and board.phraseGenerate creates both hidden and phrase.

Thank you for your help and please don't be condescending, I'm relatively new at this and I've seen a lot of post get discouraging feedback.

Upvotes: 0

Views: 70

Answers (2)

Ruchir Baronia
Ruchir Baronia

Reputation: 7561

You must increment t in this for loop:

for (int t = 0; t < phrase.length(); )
                if (CPUGuess == Character.valueOf(phrase.charAt(t)).toString()){
                    count++;
                }

Also, you are missing an open curly brace...It happens to the best of us. At the end, with the increment and the curly brace:

for (int t = 0; t < phrase.length(); t++ ) { //Need that curly brace!

                   if (CPUGuess == Character.valueOf(phrase.charAt(t)).toString()){
                        count++;

     } 
}

                //t++ Probably important...!

Upvotes: 1

Raf
Raf

Reputation: 7649

Looking at your code, you seem to be missing an opening curly braces after your for loop as shown below:

 for (int t = 0; t < phrase.length(); ) 

which should be

 for (int t = 0; t < phrase.length(); ) { } 

The above causes the print statement to be skipped. Don't forget that you are skipping the third part of the loop (the increment) as I mentioned in my comment. Not sure whether that is how your code is supposed to work or you have to add a t++ there as shown below:

//not sure if you forgot t++ or that's how it is supposed to work
for (int t = 0; t < phrase.length(); t++) { } 

Upvotes: 0

Related Questions