Page 1 of 2 12 LastLast
Results 1 to 20 of 22
  1. #1
    mfaizan24 is offline Member
    Join Date
    Apr 2009
    Posts
    27
    Rep Power
    0

    Default [SOLVED] how to run loop unless and until user enter correct value

    Hello everyone,
    I'm trying write such which ask user again to enter correct value if user enter wrong one.

    like I'm using this code..

    import java.io.*;
    import java.util.*;
    public class test {

    public static void main(String[] args) {

    boolean t = true;
    String g;
    int age;
    Scanner s = new Scanner(System.in);
    do{
    System.out.println("Enter gender: m/f");
    g = s.next();
    System.out.println("Enter age");
    age = s.nextInt();

    System.out.println("Continue....true/false");
    t=s.nextBoolean();

    }while(t);
    }
    }


    if user instead of enter m or f or male or female he enter something else like
    'k' or anything else the loop goes back and ask user to enter correct value
    how can I do this...
    please any suggestion

  2. #2
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    7

    Default

    Java Code:
    do{
    do{
    System.out.println("Enter gender: m/f");
    g = s.next();
    while (!g.equalsIgnoreCase("m") && !g.equalsIgnoreCase("f"));
    System.out.println("Enter age");
    age = s.nextInt();
    
    System.out.println("Continue....true/false");
    t=s.nextBoolean();
    
    }while(t);
    I assume you now can, using this example, do the loop for the age aswell (e.g. negative or absurd low/high age)
    I die a little on the inside...
    Every time I get shot.

  3. #3
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    843
    Rep Power
    6

    Default

    Hi
    go thru this below link

    Java Programming Notes
    Ramya:cool:

  4. #4
    mfaizan24 is offline Member
    Join Date
    Apr 2009
    Posts
    27
    Rep Power
    0

    Default

    Hi Supamagier,
    This code is not working, I've change && to || but nothing happening.
    Hi Ramyasivakanth,
    Thanks for such informative notes but I didn't any suitable information for me.
    please help me out in this I've completed almost eveything just stuck in this section.

  5. #5
    mfaizan24 is offline Member
    Join Date
    Apr 2009
    Posts
    27
    Rep Power
    0

    Default

    Hi Supamagier,
    This code is not working, I've change && to || but nothing happening.
    Hi Ramyasivakanth,
    Thanks for such informative notes but I didn't any suitable information for me but they are really informative thanks again for such amazing notes.

    please help me out in this I've completed almost eveything just stuck in this section.

  6. #6
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default Please explain

    mfaizan24: just stating that the code isn't working, doesn't help in understanding the problem you're having. Please post your code (with Supamagier's suggested changes) and explain what the problem you're having with this code.

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  7. #7
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    using a helper method would make this more clear.

    Java Code:
    // pseudo code:
    ask for input
    
    while input != m or f
      print error, ask for input.
      repeat this loop.
    
    return input
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  8. #8
    mfaizan24 is offline Member
    Join Date
    Apr 2009
    Posts
    27
    Rep Power
    0

    Default

    import java.io.*;
    import java.util.*;
    public class test {

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

    boolean t = true;
    String g="";
    int age;

    Scanner s = new Scanner(System.in);

    do{

    System.out.println("Enter gender: m/f");
    g = s.next();
    while (!g.equalsIgnoreCase("m") || !g.equalsIgnoreCase("f"));

    System.out.println("Enter age");
    age = s.nextInt();

    System.out.println("Continue....true/false");
    t=s.nextBoolean();

    }while(t);
    }
    }

    here's my complete codes

  9. #9
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    does it do what you wanted?

    also this is shorter:
    Java Code:
    while (!g.matches("^[mMfF]$"))
              continue;
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  10. #10
    mfaizan24 is offline Member
    Join Date
    Apr 2009
    Posts
    27
    Rep Power
    0

    Default

    No it doesn't do what I want, All I want this loop should continue unless and until user enter the correct value
    eg.
    Enter gender m/f
    k
    // if user here press something like k or l instead of m/f it should go back and ask user again to enter correct value.

    but above posted codes are working but screen goes stuck.

  11. #11
    mfaizan24 is offline Member
    Join Date
    Apr 2009
    Posts
    27
    Rep Power
    0

    Default

    Hi,
    I've change while to if statement and its working perfectly fine

    public static void main(String[] args) {
    String g;
    int age;
    boolean t=true;
    Scanner sc = new Scanner(System.in);

    do{
    System.out.println("enter gender");
    g = sc.next();
    if(!g.matches("^[mMfF]$")){
    continue;
    }

    System.out.println("Enter age");
    age = sc.nextInt();
    if(age < 0){
    continue;
    }

    System.out.println("continue!!....true/false");
    t = sc.nextBoolean();
    }while(t);

    but stuck in age

    }

  12. #12
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    7

    Default

    Java Code:
    if(!g.matches("^[mMfF]$"))
    You shouldn't use code you don't understand (I don't think you do tbh)
    I die a little on the inside...
    Every time I get shot.

  13. #13
    mfaizan24 is offline Member
    Join Date
    Apr 2009
    Posts
    27
    Rep Power
    0

    Default

    How can I apply this codes for age..

    System.out.println("Enter age");
    age = sc.nextInt();
    if(age < 0){
    continue;
    }

    it doesn't work..

  14. #14
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    what's not working?
    Java Code:
    S:\>java Test
    enter gender
    m
    Enter age
    0
    continue!!....true/false
    false
    END
    
    S:\>java Test
    enter gender
    m
    Enter age
    -2
    enter gender
    m
    Enter age
    1000
    continue!!....true/false
    false
    END
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  15. #15
    mfaizan24 is offline Member
    Join Date
    Apr 2009
    Posts
    27
    Rep Power
    0

    Default

    m
    Enter age
    -2 //Here I'm stuck
    enter gender
    m
    Enter age
    1000
    continue!!....true/false
    false
    END

    =============

    It shouldn't go back to gender it should stay at age only
    like if user enter 0 or -2
    it should ask user to enter age
    not enter gender

    but this loop is going back to gender which is already checked.
    Please help me out...

  16. #16
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    that cause you changed while to an if
    Java Code:
         while(!g.matches("^[mMfF]$")){
       [B]     g = sc.next(); // Dop! update user input[/B]
            //continue; 
          }
    You want:
    While Not M or F, loop
    While Age < 0, loop
    If True, Then continue
    Else End.
    Last edited by angryboy; 06-09-2009 at 07:27 PM.
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  17. #17
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default hhhmmm...

    A couple of comments/suggestions...
    Maybe two "while" loops are needed for this cases (I'm pretty sure that one can be used, but it would probably confuse the OP)

    Java Code:
    [LIST][*]genderOK = false [*]start while loop (!genderOK)[*]ask for gender[*]compare if gender is correct[*]if yes, genderOK = true[*]end while loop[/LIST]
    And then do the same (another loop) for the age verification.

    Now to do it all in one loop...

    Java Code:
    [LIST][*]genderOK = false[*]ageOK = false[*]start while loop (!genderOK && !ageOK) [*]if genderOK == false[*]{[*]ask for gender[*]compare if gender is correct[*]if yes, genderOK = true[*]}[*]ask for age[*]compare if age is correct[*]if yes, ageOK = true[*]end while loop[/LIST]
    .... or something close to that (check the "||" logic... it might be "&&").

    Luck,
    CJSL
    Last edited by CJSLMAN; 06-10-2009 at 02:49 AM. Reason: Corrected the logic
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  18. #18
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    for the one loop pseudo code, put in a "continue;" inside the if statement. and use &&.
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  19. #19
    mfaizan24 is offline Member
    Join Date
    Apr 2009
    Posts
    27
    Rep Power
    0

    Default

    I am sorry I really didn't understand anything..

  20. #20
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

Page 1 of 2 12 LastLast

Similar Threads

  1. what code for not hitting enter?
    By tornbacchus in forum New To Java
    Replies: 4
    Last Post: 04-11-2009, 05:59 AM
  2. enter key problem
    By masa in forum AWT / Swing
    Replies: 3
    Last Post: 03-19-2009, 08:30 AM
  3. [SOLVED] User Input - loop
    By new person in forum New To Java
    Replies: 4
    Last Post: 02-22-2009, 11:02 PM
  4. loop when there is no user-input
    By becky in forum New To Java
    Replies: 12
    Last Post: 02-02-2009, 11:02 PM
  5. Replies: 2
    Last Post: 04-16-2008, 01:18 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
  •