Results 1 to 6 of 6
  1. #1
    kararose820 is offline Member
    Join Date
    Apr 2013
    Posts
    1
    Rep Power
    0

    Question Help with IF statement

    I need to prompt the user for an age, and then print all people above that age (ID, name, and age). How do I do this??? Here is my code that is not working:

    private static void Aged_Patients(int ID [],String last_name[],String first_name[],int age[],int count)
    {
    String Aged_Patients = JOptionPane.showInputDialog(null,"Enter Age of Patient",
    "Input Data", JOptionPane.QUESTION_MESSAGE);
    int i = 0;
    if(Aged_Patients.equals(i))
    for(i=0;i<=count;++i)

    {
    System.out.println(ID[i]+" "+last_name[i]+" "+first_name[i]+" "+age[i]);
    }

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,960
    Rep Power
    8

    Default Re: Help with IF statement

    I don't see any if statements in your code.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Join Date
    Apr 2013
    Posts
    1
    Rep Power
    0

    Default Re: Help with IF statement

    Hi,

    try to execute the program moving the if statement inside the for loop.

    here is the sample example.

    private static void Aged_Patients(int ID [],String last_name[],String first_name[],int age[],int count)
    {
    String Aged_Patients = JOptionPane.showInputDialog(null,"Enter Age of Patient",
    "Input Data", JOptionPane.QUESTION_MESSAGE);
    int i = 0;
    for(i=0;i<=count;++i)
    {
    if(Aged_Patients.equals(i))
    System.out.println(ID[i]+" "+last_name[i]+" "+first_name[i]+" "+age[i]);
    }

  4. #4
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    335
    Rep Power
    4

    Default Re: Help with IF statement

    kararose820 and venkataSrujana, welcome to the forums.

    Quote Originally Posted by venkataSrujana View Post
    try to execute the program moving the if statement inside the for loop.
    That won't work as you would just be comparing 'i' to the user input. Instead you want to compare 'age'.

    To tidy things up it would be preferable to create a new class to hold patient information, for example a Patient class. Create an array of Patients which could then be passed into the Aged_Patients() method instead of all of the individual arrays. You would then loop through the array for '.length' number of times instead of declaring a 'count' variable.

    If you would prefer to stick with your current implementation for now, (I'm assuming you are not too familiar with methods), then you should loop for 'count' number of times. Converting Age_Patient to an integer you could perform basic mathematical functions against 'age' at position 'i' to determine if the patient details should be printed out.

    Forgive me for sounding picky but there are a couple of less critical points regarding the code.


    Regards.

  5. #5
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,960
    Rep Power
    8

    Default Re: Help with IF statement

    Gotta love attempted spoonfeeding.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

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

    Default Re: Help with IF statement

    1. Parallel arrays are the devil's spawn and should be avoided.

    2. Calling a variable and a method the same name is a bad idea as it leads to confusion.

Similar Threads

  1. Replies: 3
    Last Post: 11-19-2012, 08:12 PM
  2. if, else if, else statement
    By aragornfin in forum New To Java
    Replies: 5
    Last Post: 11-11-2012, 12:57 AM
  3. Help with IF statement
    By Rahim2312 in forum New To Java
    Replies: 16
    Last Post: 05-09-2012, 04:12 PM
  4. Replies: 2
    Last Post: 03-26-2012, 04:27 PM
  5. Statement or Prepared Statement ?
    By paty in forum JDBC
    Replies: 3
    Last Post: 08-01-2007, 04:45 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
  •