3 Replies - 1539 Views - Last Post: 16 April 2009 - 02:32 AM Rate Topic: -----

#1 javajinx  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 17-March 09

Loop end

Posted 16 April 2009 - 01:26 AM

Hi all!
I'm trying to write a program that the user puts in a word and it scans it for a "secret letter" (which is r) then tells them if it's in the word or not. This goes on till they get bored and type stop.
I've finished it basically but for some reason when you type stop it goes through the loop one more time instead of just ending. I assume i've missed closing something or i've put something in the wrong order but I just can't find where. Any help is greatly appreciated! :)

import java.util.Scanner;
public class secretletter {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);
String answer; 
final char SECRET_LETTER= 'r';
//this sets the values for the variables   

  

do
{
//initalises a do while loop		
System.out.println("Enter a word to see if it has the secret letter or type 'stop' to end: ");

answer=sc.next(); 
int findR = answer.indexOf(SECRET_LETTER);
//this searches for 'r' and will give a value of -1 if none are found
if(findR == -1)
//creates a if/else loop that replies to the user wheter their word contains the secret letter
{
System.out.println("Your guess does not contains the secret letter");
}
else 
{
System.out.println("Your guess contains the secret letter");
} 
}
while(!answer.equals ("stop"));
//exits the loop when they type stop
}}



This is what it's doing:
Enter a word to see if it has the secret letter or type 'stop' to end:
dog
Your guess does not contains the secret letter
Enter a word to see if it has the secret letter or type 'stop' to end:
stop
Your guess does not contains the secret letter

Process completed.

Is This A Good Question/Topic? 0
  • +

Replies To: Loop end

#2 DaneAU  Icon User is offline

  • Great::Southern::Land
  • member icon

Reputation: 284
  • View blog
  • Posts: 1,617
  • Joined: 15-May 08

Re: Loop end

Posted 16 April 2009 - 01:39 AM

Seemed to work fine for me - i just changed where you defined
int findR

import java.util.Scanner;
public class SecretLetter {

    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
        String answer;
        final char SECRET_LETTER= 'r';
        int findR;
        //this sets the values for the variables

        do
        {
            //initalises a do while loop
            System.out.println("Enter a word to see if it has the secret letter or type 'stop' to end: ");
            answer = sc.next();

            // function will return -1 if not found or >0 if found
            findR = answer.indexOf(SECRET_LETTER);
            //this searches for 'r' and will give a value of -1 if none are found
            if(findR == -1)
                System.out.println("Your guess does not contains the secret letter");
                 else
                    System.out.println("Your guess contains the secret letter");            
        }   while(!answer.equals("stop"));
        //exits the loop when they type stop
    }
}



EDIT

Just read waht the actual problem is - this is probably not the best way of doing it, however this will work and prevent it printing out that unwanted message when stop is entered.
import java.util.Scanner;
public class SecretLetter {

    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
        String answer = "";
        final char SECRET_LETTER= 'r';
        int findR;
        //this sets the values for the variables

        while( true )
        {
            //initalises a do while loop
            System.out.println("Enter a word to see if it has the secret letter or type 'stop' to end: ");
            answer = sc.next();
            if( answer.equals("stop"))
                break;
            // function will return -1 if not found or >0 if found
            findR = answer.indexOf(SECRET_LETTER);
            //this searches for 'r' and will give a value of -1 if none are found
            if(findR == -1)
                System.out.println("Your guess does not contains the secret letter");
                 else
                    System.out.println("Your guess contains the secret letter");            
        }
        //exits the loop when they type stop
    }
}

This post has been edited by bbq: 16 April 2009 - 01:44 AM

Was This Post Helpful? 1
  • +
  • -

#3 javajinx  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 17-March 09

Re: Loop end

Posted 16 April 2009 - 02:12 AM

Thanks very much for your speedy help! It works fine now.
Thanks again :D
Was This Post Helpful? 0
  • +
  • -

#4 DaneAU  Icon User is offline

  • Great::Southern::Land
  • member icon

Reputation: 284
  • View blog
  • Posts: 1,617
  • Joined: 15-May 08

Re: Loop end

Posted 16 April 2009 - 02:32 AM

No worries matey :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1