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 with statements to keep track of names and scores.

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default help with statements to keep track of names and scores.

    Project2.pdf (instructions)

    I have to write a program(instructions attached) to read in a file with golf scores and names, i am currently trying to get if statements to keep track of the best scores over 18 and under 18, but i cant seem to get it to work. I also attached a text document with the problem area between the asterisks. any help would be greatly appreciated.
    Thanks (code) java code.txt


  2. #2
    Administrator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    24,848
    Thanks
    64
    Thanked 2,645 Times in 2,615 Posts

    Default Re: help with statements to keep track of names and scores.

    What have you tried?

    Post your code and your questions here on the forum.

  3. #3
    Junior Member
    Join Date
    Mar 2013
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: help with statements to keep track of names and scores.

    This is what i currently have.
    I need to keep track of of top 2 child scores, and top three adult scores, but i cannot seem to get the if statement to remember the names and scores of the adults if statement.



    ****


    /**
    * THis program is to find who did best in a golf tournament, and what there total and average scores were.
    *
    * @author ()
    * @version (3/4/13)
    */
    import java.util.*;
    import java.io.*;
    public class FunWithGolf
    {
    public static void main(String args[]) throws Exception{
    Scanner readIn = new Scanner(new File("input.txt"));// reads in from .txt document

    double holesPlayed = readIn.nextInt();//stores number of holes played
    int totalScore = 0;
    int lowestChild=1000;
    int secondLowestChild=1000;
    int lowestAdult=1000;
    int secondAdult=1000;
    int thirdAdult=1000;
    String childFirstName= "abc";
    String childSecondName= "abc";
    String adultFirstName="abc";
    String adultSecondName="abc";
    String adultThirdName="abc";
    String bestHoleName="abc";
    String worstHoleName="abc";



    while(readIn.hasNext()){// need to find what to use for loop statement


    String firstName= readIn.next();//stores first and last name
    String lastName=readIn.next();
    String firstIn= firstName.substring(0, 2);
    String lastIn= lastName.substring(0,2);//finds first and last initials
    String fullName= firstIn+lastIn;

    int age = readIn.nextInt();

    double holes = holesPlayed;
    totalScore = 0;
    int hole = 0;

    while(holes!= 0){// while loop to run for amount of holes
    hole=readIn.nextInt();
    totalScore+=hole;// reads in each hole score and adds them together
    hole= 0;
    holes --;

    int currentScore= totalScore;



    }
    if(age<=18){//runs if age is less than or equal to 18



    if(totalScore<lowestChild){
    secondLowestChild=lowestChild;
    childSecondName=childFirstName;
    lowestChild=totalScore; // redeclares lowest score if totalscore is less
    childFirstName=fullName;


    }

    }

    if(age>18){// runs if age is greater than 18

    if(totalScore<lowestAdult){
    thirdAdult=secondAdult;
    adultThirdName=adultSecondName;

    secondAdult=lowestAdult;
    adultSecondName=adultFirstName;

    lowestAdult=totalScore;
    fullName=adultFirstName;}





    }
    double averageScore = totalScore/holesPlayed;//calculates average score per hole/player
    System.out.println(firstIn+lastIn+": "+totalScore +" " +averageScore);



    }
    System.out.println(lowestChild + " " + childFirstName);
    System.out.println(secondLowestChild + " " + childSecondName);
    System.out.println(lowestAdult + " " + adultFirstName);
    System.out.println(secondAdult + " " + adultSecondName);
    System.out.println(thirdAdult + " "+ adultThirdName);

    }

    }

  4. #4
    Junior Member
    Join Date
    Mar 2013
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: help with statements to keep track of names and scores.

    the scores of the first and second place adults come out fine, but the third place is wrong, and also all the names show up as "abc" which i previously declared them as, i am unsure if i'm completely wrong on doing so, but i was unsure. Thanks

  5. #5
    Administrator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    24,848
    Thanks
    64
    Thanked 2,645 Times in 2,615 Posts

    Default Re: help with statements to keep track of names and scores.

    Sorry, I forgot to ask this:
    Please edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.

    all the names show up as "abc"
    Where are the values of the variables changed? Is the code that is supposed to change the values executed?

  6. The Following User Says Thank You to Norm For This Useful Post:

    CleaVer (March 7th, 2013)

  7. #6
    Junior Member
    Join Date
    Mar 2013
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: help with statements to keep track of names and scores.

     
    /**
     * THis program is to find who did best in a golf tournament, and what there total and average scores were.
     * 
     * @author (Brice Bevers) 
     * @version (3/4/13)
     */
    import java.util.*;
    import java.io.*;
    public class FunWithGolf
    {
       public static void main(String args[]) throws Exception{
           Scanner readIn = new Scanner(new File("input.txt"));// reads in from .txt document
     
           double holesPlayed = readIn.nextInt();//stores number of holes played
           int totalScore = 0;
           int lowestChild=1000;
           int secondLowestChild=1000;
           int lowestAdult=1000; 
           int secondAdult=1000;// declares all scores 
           int thirdAdult=1000;
           int worstHole = 0;
           int bestHole=100;
           String childFirstName= "abc";
           String childSecondName= "abc";
           String adultFirstName="abc";
           String adultSecondName="abc";  // declares the strings for a wider scope
           String adultThirdName="abc";
           String bestHoleName="abc";
           String worstHoleName="abc";
     
     
     
           while(readIn.hasNext()){// need to find what to use for loop statement
     
     
            String firstName= readIn.next();//stores first and last name
            String lastName=readIn.next();
            String firstIn= firstName.substring(0, 2);
            String lastIn= lastName.substring(0,2);//finds first and last initials
     
     
            int age = readIn.nextInt();
     
            double holes = holesPlayed;
            totalScore = 0;
            int hole = 0;
     
            while(holes!= 0){// while loop to run for amount of holes
                hole=readIn.nextInt();
     
                int currentScore= totalScore;
                String fullName= firstIn+lastIn;
                if(hole<bestHole){
                    bestHole=hole;   // finds the best hole
                    bestHoleName=fullName;}
                if(hole>worstHole){
                    worstHole=hole; // finds worst hole
                    worstHoleName=fullName;}
     
                 if(hole<0){
     
                    }
     
     
                    totalScore+=hole; 
                    hole= 0;   // adds hole to total score and resets to 0, holes also reduced by one
                    holes --;
     
     
        }
     
     
        if(age<=18){//runs if age is less than or equal to 18
     
           String fullName= firstIn+lastIn;
     
            if(totalScore<lowestChild){
     
                secondLowestChild=lowestChild; //lowest child gets moved to second lowest if there is one lower
                childSecondName=childFirstName;
                lowestChild=totalScore;  // redeclares lowest score if totalscore is less
                childFirstName=fullName;
     
     
        }
     
        }
     
        if(age>18){// runs if age is greater than 18
            String fullName= firstIn+lastIn;
            if(totalScore<lowestAdult){
                  thirdAdult=secondAdult;// second lowest goes to second lowest
                adultThirdName=adultSecondName;
     
                secondAdult=lowestAdult;// first lowest goes to second lowest
                adultSecondName=adultFirstName;
     
                lowestAdult=totalScore;  // if total score is lower than new adultlowest then total score is changed
                adultFirstName=fullName;}
     
     
     
     
     
        }
        double averageScore = totalScore/holesPlayed;//calculates average score per hole/player
        System.out.println(firstIn+lastIn+": "+totalScore +" " +averageScore);
     
     
     
        }
        if(lowestChild<=0){
           System.out.println("Not enought players for a first place");
        }else{
        System.out.println("Best score 18 or under was " +childFirstName + ": " + lowestChild);}
     
        if(secondLowestChild<=0){
           System.out.println("Not enought players for a second place");
        }else{
        System.out.println("2nd Best score 18 or under was " +childSecondName + ": " +secondLowestChild );}
        if(lowestAdult<=0){
     
            System.out.println("Not enought players for a first place");
        }else{
        System.out.println("Best score over 18 was " +adultFirstName + ": " + lowestAdult);}
     
        if(secondAdult<=0){
           System.out.println("Not enought players for a second place");
        }else{
        System.out.println("2nd Best score over 18 was " +adultSecondName + ": " + secondAdult); }
     
        if(thirdAdult<=0){
           System.out.println("Not enought players for a third place");
        }else{                                                                               // displays the results of tournament
        System.out.println("3rd Best score over 18 was " +adultThirdName + ": "+thirdAdult );}
     
     
        System.out.println("Best hole by " +bestHoleName + " " + bestHole);
        System.out.println("Worst hole by " +worstHoleName + " " + worstHole);
     
    }
     
    }



    actually, i fixed the "abc" variable thing, stupid mistake. i have everything i need except for the 3rd best score keeps coming out wrong. i will put my output compared to what the output should be


    ****What i have
    BrMo: 237 6.583333333333333
    KeWa: 280 7.777777777777778
    ElMo: 225 6.25
    BrSm: 256 7.111111111111111
    ThJo: 248 6.888888888888889
    KrWa: 259 7.194444444444445
    PaCo: 242 6.722222222222222
    KrJo: 236 6.555555555555555
    JaCo: 230 6.388888888888889
    JaGr: 216 6.0
    Best score 18 or under was KrJo: 236
    2nd Best score 18 or under was ThJo: 248
    Best score over 18 was JaGr: 216
    2nd Best score over 18 was ElMo: 225
    3rd Best score over 18 was BrMo: 237 ************problem line
    Best hole by ThJo 1
    Worst hole by KrJo 15




    *****What it should be
    BrMo: 237 6.583333333333333
    KeWa: 280 7.777777777777778
    ElMo: 225 6.25
    BrSm: 256 7.111111111111111
    ThJo: 248 6.888888888888889
    KrWa: 259 7.194444444444445
    PaCo: 242 6.722222222222222
    KrJo: 236 6.555555555555555
    JaCo: 230 6.388888888888889
    JaGr: 216 6.0
    Best score 18 or under was KrJo: 236
    2nd Best score 18 or under was ThJo: 248
    Best score over 18 was JaGr: 216
    2nd Best score over 18 was ElMo: 225
    3rd Best score over 18 was JaCo: 230 ************problem line
    Best hole by ThJo: 1
    Worst hole by KrJo: 15



    hope i did it alright, and its not to much, thanks

Similar Threads

  1. use array of scores to calculate an average
    By littlebit45 in forum Other Programming Languages
    Replies: 1
    Last Post: November 30th, 2012, 02:25 PM
  2. [SOLVED] Keeping Track
    By Nuggets in forum Loops & Control Statements
    Replies: 2
    Last Post: April 15th, 2012, 05:37 PM
  3. keep track of objects
    By keep smiling in forum Java Theory & Questions
    Replies: 4
    Last Post: February 20th, 2012, 02:44 PM
  4. How to get scores from user in Canvas J2ME?
    By elenora in forum Java ME (Mobile Edition)
    Replies: 0
    Last Post: April 4th, 2011, 04:10 AM
  5. Arcade scores reset
    By JavaPF in forum The Cafe
    Replies: 1
    Last Post: July 3rd, 2010, 05:31 PM