Results 1 to 11 of 11
  1. #1
    SmokeyWoody is offline Member
    Join Date
    Oct 2014
    Posts
    36
    Rep Power
    0

    Default Do-while input validation

    I need to write an input validation while using the do-while statement. I feel like most of it is good except that it gets stuck inside the brackets of the do statement. After I enter an input, it just keeps asking me over and over for an input. Any help?

    Then I have to make it s if you enter an input that is out of range, you have to keep entering an input until it is in range.

    Java Code:
             do
             {
                System.out.print("Please enter the amount of spaces the letters will shift... ");
                shift = uInput.nextInt();
             }
    Java Code:
    public class ShiftEncoderDecoderDriver
    {
       public static void main(String[] args)
       {
          int shift;
          Scanner uInput = new Scanner(System.in);
          System.out.print("Please enter the code that you wish to encode...");
          String plainText = uInput.nextLine();
          {
             do
             {
                System.out.print("Please enter the amount of spaces the letters will shift... ");
                shift = uInput.nextInt();
             }
             while(shift > 0 || shift < 25);
                {
                System.out.print("Your shift value is set at... "+shift);
                } 
             while(shift > 0 || shift > -25);
                {
                System.out.print("Your shift value is set at... "+shift);
                }
             while(shift > 25 && shift < -25) 
                {
                System.out.print("Out of range shift amount, please enter your shift amount again.");
                shift = uInput.nextInt();
                }
           }
          
          
       }
    Last edited by SmokeyWoody; 10-24-2014 at 02:10 AM. Reason: Additional information

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

    Default Re: Do-while input validation

    it just keeps asking me over
    The code needs to set or change some values so the while() condition is false and the program's execution can exit the loop.
    If the values of the variables used in the while() condition are not changed, the loop will run forever.

    Most of the while() statements should be if statements since they should only be executed once.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    SmokeyWoody is offline Member
    Join Date
    Oct 2014
    Posts
    36
    Rep Power
    0

    Default Re: Do-while input validation

    Quote Originally Posted by Norm View Post
    The code needs to set or change some values so the while() condition is false and the program's execution can exit the loop.
    If the values of the variables used in the while() condition are not changed, the loop will run forever.

    Most of the while() statements should be if statements since they should only be executed once.
    I don't fully understand since it is what inside of the do brackets that loops. :/

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

    Default Re: Do-while input validation

    Let me give those to you one at a time:
    a while() loop won't exit if the variables in the condition are not changed in the loop.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    SmokeyWoody is offline Member
    Join Date
    Oct 2014
    Posts
    36
    Rep Power
    0

    Default Re: Do-while input validation

    Quote Originally Posted by Norm View Post
    Let me give those to you one at a time:
    a while() loop won't exit if the variables in the condition are not changed in the loop.
    This is what I have now. So now I have to somehow figure out how to have the loops exit? Adding the if statements just has it so after exiting the statement it prints the statements after it. Not entirely sure what to do here. My textbook doesn't give any information on how to do so :/

    Java Code:
    public class ShiftEncoderDecoderDriver
    {
       public static void main(String[] args)
       {
          Scanner uInput = new Scanner(System.in);
          System.out.print("Please enter the code that you wish to encode...");
          String plainText = uInput.nextLine();
          
          int shift;
          do
          {
             System.out.print("Please enter the amount of spaces the letters will shift... ");
             shift = uInput.nextInt();
    
          }
          while(shift > 25 && shift < -25); 
          {
             System.out.print("Out of range shift amount, please enter your shift amount again.");
          }
          if(shift > 0 || shift < 25);
          {              
             System.out.print("Your positive shift value is set at... "+shift);
          } 
          if(shift < 0 || shift > -25)
          {
             System.out.print("Your negative shift value is set at... "+shift);
          }
          
        
    
          
       }
    }

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

    Default Re: Do-while input validation

    The do{}while loop should exit when the use has entered good data,
    and it should continue looping if the data is bad.
    The if statements and println() statements when the data is bad helps the user correct the data.
    They should be inside the loop.

    The basic idea:
    Begin loop
    get data
    if data bad print message
    if data good exit loop
    end loop
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    SmokeyWoody is offline Member
    Join Date
    Oct 2014
    Posts
    36
    Rep Power
    0

    Default Re: Do-while input validation

    Quote Originally Posted by Norm View Post
    The do{}while loop should exit when the use has entered good data,
    and it should continue looping if the data is bad.
    The if statements and println() statements when the data is bad helps the user correct the data.
    They should be inside the loop.

    The basic idea:
    Begin loop
    get data
    if data bad print message
    if data good exit loop
    end loop
    So if I am understanding you correctly, my format is wrong and I should have something inside of the do{} so it will stop the invalid input and ask for an correct input?

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

    Default Re: Do-while input validation

    I should have something inside of the do{}
    Yes.
    The loop should continue looping until there is good data. It helps the user to enter good data if the program tells the user what is wrong with the current data and what good data looks like.
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    SmokeyWoody is offline Member
    Join Date
    Oct 2014
    Posts
    36
    Rep Power
    0

    Default Re: Do-while input validation

    Quote Originally Posted by Norm View Post
    Yes.
    The loop should continue looping until there is good data. It helps the user to enter good data if the program tells the user what is wrong with the current data and what good data looks like.
    I am not sure of what to add with what I have now. It just won't cycle.

    Java Code:
          do
          {
             System.out.print("Please enter the amount of spaces the letters will shift... ");
             shift = uInput.nextInt();
             if( shift > 25 || shift < -25 );
                System.out.print("The number you have entered is out of range, please enter a valid number. (-25 - 25)");
                
          }

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

    Default Re: Do-while input validation

    You have left off the while() part of the statement that shows the condition that is used to control the looping. The condition that controls the looping must be true if the data is bad and false if the data is good.
    If you don't understand my response, don't ignore it, ask a question.

  11. #11
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Do-while input validation

    You may also want to recheck the placement of semicolons ";" in your code.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Input validation help
    By AdamG in forum New To Java
    Replies: 3
    Last Post: 10-17-2011, 10:29 PM
  2. Input Validation Help
    By Spyderpig in forum New To Java
    Replies: 3
    Last Post: 02-18-2011, 12:58 PM
  3. JTable input validation
    By Manfizy in forum New To Java
    Replies: 10
    Last Post: 06-10-2009, 10:04 AM
  4. Input Validation
    By kickflipper1087 in forum New To Java
    Replies: 5
    Last Post: 11-03-2008, 06:47 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
  •