Results 1 to 12 of 12
  1. #1
    hedwards09 is offline Member
    Join Date
    Nov 2009
    Posts
    5
    Rep Power
    0

    Default Help with Sentinel Loops

    Hello,

    I am in an intro to Java programming class and I'm having a little trouble constructing a Sentinel loop that prompts the user for the name of a file until the user inputs "stop" to quit.

    Can anyone help me. All help will be much appreciated.

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    What have you done so far?

  3. #3
    hedwards09 is offline Member
    Join Date
    Nov 2009
    Posts
    5
    Rep Power
    0

    Default

    public static void main(String[] args)
    {
    Scanner input = new Scanner(System.in);

    String fName;

    System.out.print("Enter file name or stop to quit");
    fName = input.nextLine();

    while (fName != "stop")
    {
    //Methods entered here
    }
    }

  4. #4
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Never compare strings using == or !=. Use the equals method instead.
    Since you need the user to keep entering values, there needs to be some input.nextLines inside your loop as well.

  5. #5
    hedwards09 is offline Member
    Join Date
    Nov 2009
    Posts
    5
    Rep Power
    0

    Default

    So would the sentinel loop look like this?

    while(!fName.equals("stop");
    {
    input.nextLine();
    // methods entered here
    }

  6. #6
    Addez is offline Senior Member
    Join Date
    Aug 2009
    Posts
    294
    Rep Power
    0

    Default

    however == and != works fine when your comparing chars

  7. #7
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    It's a start. You also have the compiler and run time with you to test things out, right?

  8. #8
    hedwards09 is offline Member
    Join Date
    Nov 2009
    Posts
    5
    Rep Power
    0

    Default

    Yes i do. In the sentinel loop can I add methods to be executed if the statement returned is false?

  9. #9
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Quote Originally Posted by Addez View Post
    however == and != works fine when your comparing chars
    The more general thing to say is that they compare equality of references.
    All primitive references hold the actual value they represent so operators work as expected on primitives (of which char is one of them).

    Strings are not primitives so their references do not hold the string value but instead point to an object in memory. To compare between those objects you would need to use the overridden equals method.

  10. #10
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Quote Originally Posted by hedwards09 View Post
    Yes i do. In the sentinel loop can I add methods to be executed if the statement returned is false?
    You could test that out on the compiler since you say that you have it.





    ... methods are not added into loops though, they are added into classes.

  11. #11
    hedwards09 is offline Member
    Join Date
    Nov 2009
    Posts
    5
    Rep Power
    0

    Default

    Sorry I phrased that wrong. I can call methods in the sentinel loop. Am I correct?

  12. #12
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Quote Originally Posted by hedwards09 View Post
    Sorry I phrased that wrong. I can call methods in the sentinel loop. Am I correct?
    Only one way to find out ...

Similar Threads

  1. Help with loops!
    By jrelvi23 in forum New To Java
    Replies: 9
    Last Post: 09-04-2012, 01:36 PM
  2. when should we use loops
    By shahemaan in forum New To Java
    Replies: 1
    Last Post: 10-31-2009, 01:38 AM
  3. these loops...
    By Blaedel in forum New To Java
    Replies: 0
    Last Post: 10-01-2009, 06:59 PM
  4. how to use do while loops
    By mikeitalydz in forum New To Java
    Replies: 32
    Last Post: 09-26-2009, 08:30 PM
  5. Loops (while do etc)
    By manupr in forum New To Java
    Replies: 1
    Last Post: 01-15-2008, 03:59 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
  •