Results 1 to 9 of 9
  1. #1
    qwe
    qwe is offline Member
    Join Date
    Nov 2014
    Posts
    13
    Rep Power
    0

    Default " do not continue, try again"

    Hello
    I have a small issue, hope someone will help :)

    If the user in my system writes the wrong number, the system keeps going on to the next process. Is their a code to make the system stop and "start over". If the user lets say puts a String instead of a Double my system continues. I would like for it to say "System.out.print("you typed the wrong number, try again"); " and that the system then does not go further before the user has put in the right word/number etc.

    I have tried with for and switch, but can't make it work.

    Thanks!

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,143
    Rep Power
    15

    Default Re: " do not continue, try again"

    Can you post an MCVE showing what you have so far?
    How to Ask Questions the Smart Way
    Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
    Static Void Games forum - Come say hello!

  3. #3
    qwe
    qwe is offline Member
    Join Date
    Nov 2014
    Posts
    13
    Rep Power
    0

    Default Re: " do not continue, try again"

    Hey! Thanks so much for replaying. I don't know how to make a MCVE so I just copied my code in, it is not so long.
    I made it work with a "for" but I can only set it to an X amount of times before it start displaying the rest of my codes.
    Do you have a smart trick?

    Java Code:
    
    final int Username = 1234;
         final String Password = "12aa";
         
    
          int EnteredUsername;
          String EnteredPassword;
         
         for(int s=0;s<=3;s++)
         {if (s<3)
         {System.out.println("Enter Username");
         EnteredUsername = input.nextInt();
         System.out.println("Username Entered is " + EnteredUsername);
         System.out.println("Enter Password");
         EnteredPassword = input.next();
         System.out.println("Password Entered is " + EnteredPassword);
        
         if (Username == EnteredUsername && (Password.equals(EnteredPassword)) {
             System.out.println("Welcome");
             break;
         }
         else {
             System.out.println("Wrong Username or Password. Please try again");
         }
         }
             else
         {
             System.out.println("Your 3 chances are over!!!!");
    Last edited by qwe; 11-05-2014 at 10:48 PM.

  4. #4
    qwe
    qwe is offline Member
    Join Date
    Nov 2014
    Posts
    13
    Rep Power
    0

    Default Re: " do not continue, try again"

    Also my code says that username and password are wrong even when I type it in correctly ? Any ideas why?

    This code should be enough to check if they are
    if (Username == EnteredUsername && Password == EnteredPassword)

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: " do not continue, try again"

    Please edit your post and wrap your code with highlight tags:
    [code]
    YOUR CODE GOES HERE
    [/code]
    to get highlighting and preserve formatting.

    code to make the system stop and "start over".
    That sounds like a loop that surrounds the main code with a continue statement at the "start over" point that sends the execution back to the top of the loop.

    Any ideas why?
    Use the equals() method for comparing String objects not the == operator.
    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    qwe
    qwe is offline Member
    Join Date
    Nov 2014
    Posts
    13
    Rep Power
    0

    Default Re: " do not continue, try again"

    Thanks! Appreciate it :)

    I still don't get how to make the 3 in this code become infinite.
    Java Code:
    for(int s=0;s<=3;s++)
         {if (s<3)
    I could just change 3 to 10000 but their must be a better way?
    Thanks again !

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: " do not continue, try again"

    how to make the 3 in this code become infinite.
    There are two 3s?
    What do you want the code to do?

    The normal forever loop is made by using a while(true) statement

    BTW. Putting a statement after a { makes the code harder to read and understand. Its better to not put anything after a {
    Also nested {s and }s should be indented to show logic nesting.
    Last edited by Norm; 11-06-2014 at 12:19 AM.
    If you don't understand my response, don't ignore it, ask a question.

  8. #8
    qwe
    qwe is offline Member
    Join Date
    Nov 2014
    Posts
    13
    Rep Power
    0

    Default Re: " do not continue, try again"

    Okay thanks. I will try with a while statement instead.
    I just want the code not to continue to the next process unless the username and password are right. In this code, if I type it wrong 3 times it just continues the the menu witch it shouldn't be able to.

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: " do not continue, try again"

    If you describe your problem as pseudo code you get something like:
    while invalid login
    get login details.

    That should give you enough to get started.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. "Continue Yes or No" Problem
    By LetsG0Blue in forum New To Java
    Replies: 10
    Last Post: 02-17-2013, 01:27 AM
  2. Replies: 0
    Last Post: 12-07-2012, 09:29 AM
  3. Replies: 3
    Last Post: 10-30-2012, 04:06 PM
  4. Why do we need "continue" in this piece of code?
    By tariqm in forum New To Java
    Replies: 4
    Last Post: 04-08-2012, 06:47 PM
  5. Replies: 1
    Last Post: 10-20-2008, 08:35 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
  •