Results 1 to 4 of 4
  1. #1
    mrblippy is offline Member
    Join Date
    Apr 2009
    Posts
    2
    Rep Power
    0

    Default Using string to terminate loop

    hi guys and gals :)

    just looking for a bit of help with a program is writing. i am trying to make it accept a word from the user and tell them if it contains the letter 'n'. it then asks the user to enter another word. if the user enters the word 'end' the program ends. however the loop always runs once and 'end' has no effect. any suggesting on what i'm doing wrong?



    import java.util.Scanner;


    public class letterTest
    {
    public static void main(String[] args)
    {

    Scanner sc;
    String word;

    sc = new Scanner(System.in);

    System.out.println("enter your word, end to finish");
    word = sc.nextLine();
    sc.nextLine();


    while (!word.equals("end"));
    {


    if(word.indexOf('n') > -1)
    {
    System.out.println("Contains secret letter");
    }

    else
    {
    System.out.println("Does not contain secret letter");
    }

    System.out.println("enter your next word, end to finish");
    word = sc.nextLine();
    sc.nextLine();




    }



    }
    }


    thanks and sorry if i explained the problem in a confusing way :(

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Java Code:
    while (!word.equals("end"));
    that semicolon at the end of this statement is killing you here.
    Get rid of it and then see what happens.

  3. #3
    mrblippy is offline Member
    Join Date
    Apr 2009
    Posts
    2
    Rep Power
    0

    Default

    thanks heaps for your help, cant believe i missed such a small thing.
    also found a few other problems and fixed them

    thanks again :)

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. Entering string in a loop with method
    By Konex in forum New To Java
    Replies: 3
    Last Post: 03-01-2009, 08:33 AM
  2. [SOLVED] Ending for loop with a String
    By jdiesel in forum New To Java
    Replies: 3
    Last Post: 01-08-2009, 03:11 AM
  3. Entering a while loop with a not equal to string
    By bri1547 in forum New To Java
    Replies: 9
    Last Post: 07-09-2008, 08:10 AM
  4. 3 errors and then terminate program
    By hezfast2 in forum New To Java
    Replies: 2
    Last Post: 05-20-2008, 02:57 AM
  5. terminating a while loop with a string
    By tkdvipers in forum New To Java
    Replies: 3
    Last Post: 07-10-2007, 12:23 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •