Results 1 to 2 of 2
  1. #1
    tgwinford is offline Member
    Join Date
    Mar 2011
    Rep Power

    Default Help with while loops

    For an assignment for Computer Science I have to write a program that prompts the user to input a name, number of hits, and number of at bats in order to calculate an average. I have to put it into a loop so that the user can continue inputting players until they enter 'q' to quit. Once the loop exits, the program is supposed to print the name and average of the best and worst averages. I have gotten the loop working right, but I don't know how to do the final part. Here is what I have so far:

    import java.util.*;
    import java.text.*;

    public class BattingStatsTGW
    public static void main (String [] args)
    Scanner scan = new Scanner (;

    String name; // stores hitter's name
    int hits, bats; // stores hits, number of at bats
    double avg; // stores batting average
    char SENTINEL = 'q'; // sets the sentinel at 'q'

    System.out.print ("\nEnter the name of a batter OR 'q' to quit > ");
    name = scan.nextLine ();

    while (name.charAt(0) != SENTINEL) // compares name to 'q'


    System.out.print ("\nEnter the number of Hits > ");
    hits = scan.nextInt ();

    System.out.print ("\nEnter the number of At Bats > ");
    bats = scan.nextInt ();

    avg = (double) hits / bats;

    DecimalFormat dfmt = new DecimalFormat ("0.000");

    System.out.println ("\n\tThe average is: " + dfmt.format (avg));

    if (avg >= .300) // compares the average to .300
    {System.out.println ("\n\t" + name + " is eligible for All Star Game!");}

    {System.out.println ("\n\t" + name + " is NOT eligible for All Star Game.");}

    System.out.print ("\nEnter the name of the next batter OR 'q' to quit > ");
    name =;


    // output the player name and average of the highest average
    // output the player name and average of the lowest average

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Richmond, Virginia
    Blog Entries
    Rep Power


    You need to store the names and averages of the players in some way, try possibly using a map of some sort that's takes string and integer, the name Will be the key and the average will be the value. Another thought is to make a player class which has a string for the name and double for the average and create an arraylist of players, then create a comparator and sort the arraylist
    Last edited by sunde887; 03-05-2011 at 12:05 PM.

Similar Threads

  1. Help with loops!
    By jrelvi23 in forum New To Java
    Replies: 9
    Last Post: 09-04-2012, 02:36 PM
  2. need some help with loops!
    By Chewart in forum New To Java
    Replies: 2
    Last Post: 12-04-2009, 12:32 AM
  3. While Loops, need a bit of help.
    By Keno777 in forum New To Java
    Replies: 7
    Last Post: 10-30-2009, 09:24 PM
  4. these loops...
    By Blaedel in forum New To Java
    Replies: 0
    Last Post: 10-01-2009, 07:59 PM
  5. how to use do while loops
    By mikeitalydz in forum New To Java
    Replies: 32
    Last Post: 09-26-2009, 09:30 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts