Results 1 to 4 of 4

Thread: loop question

  1. #1
    smcmahon is offline Member
    Join Date
    Nov 2012
    Posts
    2
    Rep Power
    0

    Default loop question

    Hello.

    Getting stuck in trying to implement the loop below, since I have two duplicate local variables, and its something I'm having a hard time finding a solution for.

    import java.util.*;
    import java.io.*;


    public class StringExample
    {

    public static void main( String[] args ) throws IOException
    {

    // TODO Auto-generated method stub
    Scanner input = new Scanner( System.in );

    System.out.println( "Please enter a password" );
    String pw = input.next();
    int result = pw.indexOf("@");


    while( result == -1 );
    {
    System.out.println( "Invalid, re-enter");
    String pw = input.next();
    }
    System.out.println( "Thanks for the password" );


    }

    }

    Its supposed to search a string for the @ symbol, and when a password is typed that contains it, then it terminates. Otherwise the user keeps getting prompted.

    Any insight is appreciated. Thanks.

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,429
    Rep Power
    7

    Default Re: loop question

    You redeclare pw inside the while loop instead of reseeting the value of the already declared pw. You never change result in your loop, so it cannot terminate.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default Re: loop question

    Java Code:
    while( result == -1 );
    Infinite loop of nothing or it will never enter.

  4. #4
    smcmahon is offline Member
    Join Date
    Nov 2012
    Posts
    2
    Rep Power
    0

    Default Re: loop question

    Quote Originally Posted by Junky View Post
    Java Code:
    while( result == -1 );
    Infinite loop of nothing or it will never enter.
    Thanks for the feedback. I was finally able to go with the following code, and it seems to accomplish what I set out for.

    import java.util.*;
    import java.io.*;


    public class SixTwo
    {

    public static void main( String[] args ) throws IOException
    {

    // TODO Auto-generated method stub
    Scanner input = new Scanner( System.in );

    boolean result;

    do
    {

    System.out.println( "Please enter a password" );
    String pw = input.next();
    result = pw.contains("@");
    }while( result == false);

    System.out.println( result );
    System.out.println( "Thanks for the password");

    }

    }

Similar Threads

  1. Another Loop Question
    By 462345 in forum New To Java
    Replies: 1
    Last Post: 03-28-2013, 09:27 PM
  2. Question With a loop
    By zniightmare in forum New To Java
    Replies: 14
    Last Post: 02-08-2012, 07:46 PM
  3. Loop question
    By rich123 in forum New To Java
    Replies: 6
    Last Post: 02-08-2011, 03:26 AM
  4. loop question
    By ccie007 in forum New To Java
    Replies: 22
    Last Post: 08-15-2010, 09:29 PM
  5. for Loop with Yes/No Question! help..please!
    By mastercrimson in forum New To Java
    Replies: 8
    Last Post: 06-02-2010, 06:08 PM

Posting Permissions

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