Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Help me please with my codes

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help me please with my codes

    Hi everyone
    I've just registered here, and really need your help with my assignment
    I have these 2 codes that won't work with me

    Q1: Write a program that reads a number of patients and then let the employee enter their names and ages and count them. The program should check if the age of the patient is below 25 years old , otherwise, the patient will not be counted . your program should display total number of patient under the age 25.

    Q2: Write a program that reads unlimited number of items and their prices until the user enters “Exit “, after that the program should print out the total bill.

    I uploaded my codes and print screen for sample run ,
    the problem with Q1 is that the print should be like this :
    Enter a Number of Patients > 20
    Enter patients Name: sara
    Enter patients Age: 23
    so I can count the ages under 25 .. ???

    the problem with Q2 is when I enter the sentinel "Exit" it won't end the loop !!!

    that's all , waiting for your help
    Attached Images Attached Images


  2. #2
    Junior Member
    Join Date
    Oct 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help me please with my codes

    no replay
    I guess the codes aren't clear

    Q1:

    PHP Code:
    import java.util.*;
    public class 
    Q_1 {
    static 
    Scanner console = new Scanner (System.in);
    public static 
    void main (String [] args) {

    System.out.print("Enter a number of Patients : " );
    int numconsole.nextInt();
    int count=0;
    while (
    num>=20)
    {
    System.out.print("Enter patient Name : " );
    String s1console.nextLine();

    System.out.println("Enter patient age : " );
    int xconsole.nextInt();

    if (
    x<25){
    count++;
    num--;
    }
    }
    System.out.println("Total number of patient under age 25  : "count );
    }


    Q2:

    PHP Code:
    import java.util.*;
    public class 
    Q_2 {
    static 
    Scanner console = new Scanner (System.in);
    public static 
    void main (String [] args) {
    final 
    String SENTINEL "Exit";
    System.out.println("Enter an Item and its price (ending with '" SENTINEL "'):");
    String s1console.nextLine();
    int sum=0;
    int x=s1.indexOf(' ',0);
    String s2s1.substring(0,x);
    String s3s1.substring(x+1,s1.length());
    int yInteger.parseInt(s3);

    boolean ts1.equals (SENTINEL);
    while (
    t==false)
    {
    sumsum y;
    System.out.println("Enter an Item and its price (ending with '" SENTINEL "'):");
    s1console.nextLine();
    }

    System.out.println("the total bill is : " sum);
    }

    help me please
    Last edited by sara92; October 12th, 2012 at 07:32 AM.

  3. #3
    Member
    Join Date
    Sep 2012
    Location
    The Netherlands
    Posts
    84
    My Mood
    Inspired
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default Re: Help me please with my codes

    Try putting the code between code tags.

    [*code]

    [*/code]
    Without the *

    Then you will get more replies then with screens.

  4. #4
    Member
    Join Date
    Sep 2012
    Location
    The Netherlands
    Posts
    84
    My Mood
    Inspired
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default Re: Help me please with my codes

    For Q1:
    - I would use an HashMap to store the name with the age. (they they are linked together)
    - Also add the age to an arrayList if age is below the 25.
    - Then have the program look inside the arrayList how many values are below the 25.

    Hope that helped.
    Last edited by Purple01; October 12th, 2012 at 07:48 AM.

  5. #5
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 262 Times in 231 Posts

    Default Re: Help me please with my codes

    Sara92 Hi and welcome to the forum!
    Please see the announcements page for the use of code tags and other useful facts.


    Q1: The user is to give a name and an age one at a time. If the current age is less than 25 add 1 to the number of people under 25. If not do not add 1 and continue. How would you do it without a computer? Write the steps down on paper. (No, seriously, write them down) When you have the steps in front of you it is much easier to write the code.

    Q2: The problem I see with this program is logical. You didn't write the code to do what you wanted. Again write the steps down on paper and use that to code from. Doing so will help keep the steps in order. For one thing you set the boolean variable before the loop, and the value never changes. You need some way to change the value of that variable inside the loop. Another thing I see is duplicated lines of code. Any time you see yourself writing the same thing over again, question the design. Use your favorite search engine and 'java do while loop' and 'java loop and a half'

    The following code can be seen here:

    You use loop-and-a-half to avoid repeating code from outside the loop to the inside. Example:
    read a;
    while a != b do
      stuff;
      read a;
    end
    becomes
    while true do
      read a
      if a == b then break
      stuff;
    end
    Now I only have the read in one place.
    Edit
    I wouldn't worry about the hash map just yet. That is likely some time down the road from now.

  6. #6
    Junior Member
    Join Date
    Oct 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help me please with my codes

    thanks a lot,, I got my mistakes now

    I appreciate your advice & i will do it in future..

Similar Threads

  1. Putting codes in order......
    By rdmighty in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 1st, 2012, 11:51 AM
  2. Looking for help on two codes:
    By captianjaax in forum What's Wrong With My Code?
    Replies: 24
    Last Post: September 20th, 2011, 11:49 PM
  3. [SOLVED] Help with understanding the following commented codes!
    By u-will-neva-no in forum Java Theory & Questions
    Replies: 2
    Last Post: April 2nd, 2011, 04:26 PM
  4. Please Help What's Wrong with my Codes!
    By bertzki10 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 25th, 2011, 10:03 AM
  5. help us in eclipse basic codes
    By bil_Imma in forum AWT / Java Swing
    Replies: 1
    Last Post: January 24th, 2009, 05:02 PM