CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jan 2013
    Posts
    20

    File import and read file, HELP PLEASE!!!:) beginner coder

    I am trying to write this code but I am running into a brick wall, I Import this file and everything is good. but when I try to add the Players and Par for a Total at the bottom I keep getting different errors. Can someone please help. My code is as follows....
    import java.io.*;
    import java.util.*;

    public class Readscorecard {

    private Scanner file;
    private double TPar, TPlayer1, TPlayer2 , TPlayer3, TPlayer4;
    private double Par, Player1, Player2, Player3, Player4, Sum;

    public void scorecard(){

    try{
    file = new Scanner(new File("golfFile.txt"));
    }
    catch (Exception e){
    System.out.println("you received an Error");
    }
    }

    public void readgolffile(){
    System.out.printf("%-10s %1s %12s %12s %12s\n", "Par", "Player 1", "Player 2", "Player 3", "Player 4\n");

    while (file.hasNext()){
    Par = file.nextDouble(); //Line 31

    Player1 = file.nextDouble();

    Player2 = file.nextDouble();

    Player3 = file.nextDouble();

    Player4 = file.nextDouble();

    System.out.printf("%-10s %1s %12s %12s %12s\n", Par, Player1, Player2, Player3, Player4);
    }

    System.out.println("_________________________________________________");

    Par = Par + Par;
    System.out.println(Par);
    }



    OUTPUT:
    Par Player 1 Player 2 Player 3 Player 4

    5.0 6.0 6.0 4.0 7.0
    5.0 6.0 2.0 7.0 3.0
    3.0 2.0 4.0 4.0 7.0
    5.0 5.0 2.0 2.0 4.0
    4.0 7.0 5.0 4.0 2.0
    5.0 4.0 6.0 7.0 3.0
    4.0 2.0 2.0 2.0 2.0
    3.0 3.0 7.0 5.0 7.0
    5.0 2.0 4.0 6.0 6.0
    3.0 7.0 6.0 5.0 4.0
    4.0 6.0 3.0 7.0 6.0
    5.0 4.0 6.0 5.0 3.0
    4.0 4.0 7.0 5.0 3.0
    4.0 6.0 4.0 2.0 4.0
    4.0 5.0 3.0 3.0 6.0
    5.0 3.0 6.0 4.0 2.0
    5.0 6.0 7.0 3.0 2.0
    3.0 3.0 5.0 4.0 2.0

  2. #2
    Join Date
    Aug 2011
    Location
    West Yorkshire, U.K.
    Posts
    54

    Re: File import and read file, HELP PLEASE!!!:) beginner coder

    Code is incomplete and not runnable!
    The line:
    Code:
    Par = Par + Par;
    probably does not do as you are expecting ... looks like you're trying to keep a running total of "par" but this is not happening because (1) Par is being reset each time you read a line from the file and (2) this is outside the loop where "Par" and the other scores are being read from the file.
    You are not keeping running totals of the player's scores either.
    I suspect you could also use integers rather than Doubles for this exercise.

  3. #3
    Join Date
    Jan 2013
    Posts
    20

    Re: File import and read file, HELP PLEASE!!!:) beginner coder

    thank you Alex for your reply. I do need to have a total of all the numbers by column. for instance....column 1 Par, Column 2-5 Player1-4. I have tried different ways to get a total, my latest attempt has been to tackle it column by column, but do not know or understand where to start. I can import the file which the professor gave us to use but I can not get it to add the columns at the bottom. at this point I am dazed and confused.. I need help !!

    This is my latest attempt....
    It runs great, BUT it doesn't have the Columns added at the bottom of it, which is where I don't know how to get to start or complete it.


    import java.io.*;
    import java.util.*;



    public class Readscorecard {
    public static void main(String[] args) {
    // TODO Auto-generated method stub


    Readscorecard h = new Readscorecard ();

    h.scorecard();
    h.readgolffile();
    h.closefile();
    }



    private Scanner file;

    private double TPar, TPlayer1, TPlayer2 , TPlayer3, TPlayer4;
    private int Par, Player1, Player2, Player3, Player4, Sum;

    public void scorecard(){

    try{
    file = new Scanner(new File("golfFile.txt"));
    }
    catch (Exception e){
    System.out.println("you received an Error");
    }
    }

    public void readgolffile(){
    System.out.printf("%-10s %1s %12s %12s %12s\n", "Par", "Player 1", "Player 2", "Player 3", "Player 4\n");
    Par = 0;
    Player1 = 0;

    while (file.hasNext()){
    Par = file.nextInt();
    TPar = Par;
    TPar = TPar + Par;
    Player1 = file.nextInt();
    TPlayer1 = Player1++;
    Player2 = file.nextInt();

    Player3 = file.nextInt();

    Player4 = file.nextInt();

    System.out.printf("%-10s %1s %12s %12s %12s\n", Par, Player1, Player2, Player3, Player4);
    }

    System.out.println("_________________________________________________");

    System.out.printf("%-10s %1s %12s %12s %12s\n", TPar, TPlayer1, Player2, Player3, Player4);

    }

    public void closefile(){
    file.close();
    }
    }

  4. #4
    Join Date
    Aug 2011
    Location
    West Yorkshire, U.K.
    Posts
    54

    Re: File import and read file, HELP PLEASE!!!:) beginner coder

    You should use "code tags" when posting code on the forum ... it maintains the formatting!
    I assume TPar is the running total for par, TPlayer1 the running total for player 1 etc.
    You are almost there - the problem is in the loop which reads the data file.
    Each time a line is read from the file, TPar is set to Par, then you add Par to it again.
    So your "total" par will always be twice the value of the "par" for the last line in the data file.
    Also, when you add Player1 score to TPlayer1, what your code is doing is setting TPlayer1 to the value for Player1 read from the file, and then adding 1 to the value.
    What you should do is initialise the totals to zero before the loop, and add the individual score values to them inside the loop ... then (as you are doing) print the totals after the loop, i.e.
    Code:
            TPar = 0;
            TPlayer1 = 0;
            // ... etc
            
            while (file.hasNext()){
                // Read par and 4 player scores
                Par = file.nextInt();
                Player1 = file.nextInt();
                Player2 = file.nextInt();
                Player3 = file.nextInt();
                Player4 = file.nextInt();
                // Increment total counters
                TPar = TPar + Par;
                TPlayer1 += Player1;
                // ... etc
                // Print individual score line
                System.out.printf("%-10s %1s %12s %12s %12s\n", Par, Player1, Player2, Player3, Player4);
            }
            // Print totals
            System.out.println("_________________________________________________");
            System.out.printf("%-10s %1s %12s %12s %12s\n", TPar, TPlayer1, TPlayer2, TPlayer3, TPlayer4);
    The line "TPlayer1 += Player1" is equivalent to doing "TPlayer1 = TPlayer1 + Player1"

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
  •  





Click Here to Expand Forum to Full Width

Featured