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

    Default [SOLVED] problem with do..while loop

    Hello everyone.
    i m trying to take value from user continuesly using while loop but its not working the loop exits only on first turn its not asking for continues

    im using this codes

    char gender;
    char urbanRural;
    char c;
    int age;
    boolean check=true;
    Scanner sc = new Scanner(System.in);
    try{
    do{
    System.out.println("Enter gender M/F");
    gender =(char) System.in.read();
    System.out.println("You live in urban or rural U/R");
    urbanRural=(char) System.in.read();
    System.out.println("Enter Age ");
    age = sc.nextInt();
    System.out.println("Continue...y/n");
    c = (char) System.in.read();
    if(c =='y'){
    check=true;}
    else{check=false;}
    } while(check);
    }catch(IOException e){System.out.println(e.getMessage());}

    this loop taking values only once its not asking for continues...y/n
    I've tried both while and do..while its not working in any of them
    can anyone help me with this please :confused:

  2. #2
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    6

    Default

    try if(c=='y' || c=='Y')

    You're currently only checking for one case. If the user inputs a capital letter it will show up as wrong. You also might want to use a while loop instead of a do while loop.
    Liberty has never come from the government.
    Liberty has always come from the subjects of government.
    The history of liberty is the history of resistance.
    The history of liberty is a history of the limitation of governmental power, not the increase of it.

  3. #3
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    I'm not familiar with scanner, but...

    You say that you don't get the "Continue..." prompt. That would indicate something is happening when you try to read in the age. Look at the last messages; I suspect you will see some sort of exception message. That would cause you to break out of the loop.

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

    Default

    sorry about that. basically i'm conducting the survey of different products and here my complete codes they are not working as I want them to work. All I want this loop to continue unless and until user doesn't want to continue while saying in the end ....n...but they executing only for once as I mention it before, the loop is terminating right after taking the value of rural/urban
    int age;
    char gender;
    char ruralUrban;
    int w=0;
    int x=0;
    int y=0;
    int z=0;
    boolean check=true;
    char c;
    Scanner sc = new Scanner(System.in);

    //if(locality = urban and gender=female then W)
    //if(gender = female and age<30 then X)
    //if(gender=male and locality=rural and age>30 && age<60 then Y)
    //if(if age>60 then not Z)

    do
    {
    System.out.println("Enter gender : M/F");
    gender = (char) System.in.read();
    System.out.println("enter Age : ");
    age= sc.nextInt();
    System.out.println("residents of rural or urban select R/U");
    ruralUrban = (char) System.in.read();

    if ((ruralUrban == 'u'|| ruralUrban == 'U') && (gender == 'f' || gender == 'F')){
    w += 1;
    z += 1;
    }

    if ((gender == 'f' || gender == 'F') && (age < 30)){
    x += 1;
    z += 1;
    }

    if ((gender == 'm' || gender == 'M') && (ruralUrban == 'r' || ruralUrban == 'R') && (age > 30 && age < 60)) {
    y += 1;
    z += 1;
    }

    if (age > 60) {
    z -= 1;
    }

    System.out.println("Continue...y/n");
    c = (char) System.in.read();
    if(c =='y'){
    check=true;}
    else{check=false;}
    } while(check);

    System.out.println("Survey Report....");
    System.out.println(w + " Females who live in Urban prefer number of W");
    System.out.println(x + " Young female who live in urban and rural prefer X product");
    System.out.println(y + " Male of mid-age who live in rural prefer Y product ");
    System.out.println(z + " people prefer Z product as well");
    } catch(IOException e){System.out.println(e.getMessage());}

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

    Default

    no response....:(

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

    Default

    no response:(

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

    Default

    Just check the below condition .Might be it should be 'Y' or other than this and made check as false.

    if(c =='y'){
    check=true;}
    else{check=false;}
    }
    Ramya:cool:

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

    Default

    My doubt is u have not used Scanner properly right?
    Ramya:cool:

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

    Default

    To use scanner class I think the best way is create object and use like this
    Scanner sc = new Scanner(System.in);
    age=sc.nextInt();.
    and for c=='y' i'm entering small case y only but its not even asking for this its directly discontinuing right after taking three values (gender/urbanrural/age)

  10. #10
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    From what I see, there is no way not to print the "Continue..." prompt unless

    1. You did something I'm not seeing with the braces

    2. You are getting an IOException when you attempt to read the value of ruralUrban. (I don't see the try { statement anymore.)

    Check your output more closely. You should see either the "Continue..." prompt or an error message.

    Are you running this in such a way that the command window closes as soon as java ends? If so, open a command window and run java from the command line, so you can see the messages after if finishes running.

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

    Default

    I have made some small modification ion your code.Why are u not completly using Scanner for user inputs.I have highlighted the modification what i made.
    Please go thru.Some problems are there with System.in.read.Once if u confirm with the code below i will give u justification for these changes.Just i have wrapped ur code in my class and tested.It's fine.

    Java Code:
    import java.util.*;
    import java.io.*;
    public class Test
    {
    public static void main(String args[]) throws Exception
    {
    int age;
    char gender;
    char ruralUrban;
    int w=0;
    int x=0;
    int y=0;
    int z=0;
    boolean check=true;
    [B][COLOR="Green"]String c="";[/COLOR][/B]
    Scanner sc = new Scanner(System.in);
    
    //if(locality = urban and gender=female then W)
    //if(gender = female and age<30 then X)
    //if(gender=male and locality=rural and age>30 && age<60 then Y)
    //if(if age>60 then not Z)
    
    do
    {
    System.out.println("Enter gender : M/F");
    gender = (char) System.in.read();
    System.out.println("enter Age : ");
    age= sc.nextInt();
    System.out.println("residents of rural or urban select R/U");
    ruralUrban = (char) System.in.read();
    if ((ruralUrban == 'u'|| ruralUrban == 'U') && (gender == 'f' || gender == 'F')){
    w += 1;
    z += 1;
    }
    
    if ((gender == 'f' || gender == 'F') && (age < 30)){
    x += 1;
    z += 1;
    }
    
    if ((gender == 'm' || gender == 'M') && (ruralUrban == 'r' || ruralUrban == 'R') && (age > 30 && age < 60)) {
    y += 1;
    z += 1;
    }
    
    if (age > 60) {
    z -= 1;
    }
    
    System.out.println("Continue...y/n");
    
    [B][COLOR="DarkGreen"]c = sc.next();
    
    if(c.trim().equalsIgnoreCase("y")){[/COLOR][/B]
    check=true;
    System.out.println(check);
    }
    else{check=false;}
    } while(check);
    
    System.out.println("Survey Report....");
    System.out.println(w + " Females who live in Urban prefer number of W");
    System.out.println(x + " Young female who live in urban and rural prefer X product");
    System.out.println(y + " Male of mid-age who live in rural prefer Y product ");
    System.out.println(z + " people prefer Z product as well");
    }
    }
    Ramya:cool:

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

    Default

    thank you very much its working perfectly fine, but how ..

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

    Default

    sorry steve that ..try { ... was top of int age; which I forget to copy it sorry about that.

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

    Default

    It is RamyaSivakanth who has given the solution :-)
    If you read as a character the cursor is waiting to accept more.That is why i have replaced with String and used scanner object.next() method.If u think this has solved pleasde mark it as solved
    Ramya:cool:

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

Similar Threads

  1. if else loop problem
    By Ms.Ranjan in forum New To Java
    Replies: 12
    Last Post: 04-25-2009, 09:30 AM
  2. Some while loop problem need help
    By shaggyoo7 in forum New To Java
    Replies: 4
    Last Post: 01-14-2009, 07:16 PM
  3. Loop Problem
    By jralexander in forum New To Java
    Replies: 4
    Last Post: 12-02-2008, 07:08 AM
  4. Problem to use different for loop to add up
    By matt_well in forum New To Java
    Replies: 6
    Last Post: 08-03-2008, 10:24 PM
  5. For loop problem
    By mcal in forum New To Java
    Replies: 32
    Last Post: 01-25-2008, 03:51 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
  •