Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New Coder
    Join Date
    Jul 2010
    Location
    Canada
    Posts
    26
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Outputting Java Program to Text File

    i'm trying to make a small and simple Java program that uses the scanner class to take in my stats from my basketball games and output to a CSV file. I made the first part but have no idea how to do the other. How can I get it to output to a CSV/excel or a text file????

    Code:
    import java.util.Scanner
    
    class basketball 
    {
    	public static void main (String [] args)
    	{
    		Scanner input = new Scanner (System.in) ();
    		System.out.println("How many points did you score?");
    		points = input.nextInt();
    		System.out.println("How many assists did you dish?");
    		assists = input.nextInt();
    		System.out.println("How many boards did you grab?");
    		rebounds = input.nextInt();
    		
    	}
    }

  • #2
    Regular Coder
    Join Date
    Feb 2008
    Location
    Edinburgh - Scotland
    Posts
    107
    Thanks
    0
    Thanked 12 Times in 12 Posts

    Cool

    Im surprised that would compile!

    You probably want

    int rebounds = scanner.nextInt(); or something along those lines.

    So after you have your 3 variables for instance:

    int points;
    int rebounds,
    int layups;

    You could do the following to output it to a .txt or .csv file:

    Code:
    public void writeFile(String filename,int points, int rebounds, int layups){
     try{
        // Create file 
        FileWriter fstream = new FileWriter(filename);
            BufferedWriter out = new BufferedWriter(fstream);
        out.write("Points,"+points+",rebounds,"+rebounds+"layups,"+layups);
        //Close the output stream
        out.close();
        }catch (Exception e){//Catch exception if any
          System.err.println("Error: " + e.getMessage());
        }
      }
    }
    You could then call it from the bottom of your code by doing :

    Code:
    writeFile("myStatsFileToday.txt",points,rebounds,layups);
    Bare in mind the file would then be written to the directory where your program is running - if you wanted a different output directory you just append that to the filename i.e

    Code:
    writeFile("C:\\mystats\\myStatsFileToday.txt",points,rebounds,layups);
    double \\ are needed because you need to escape the "\". You may also want to append to a single file so you can use the same program to add to a single file to build a record of your stats - but thats your own little addition if you wanted it

  • #3
    New Coder
    Join Date
    Jul 2010
    Location
    Canada
    Posts
    26
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Code:
    public void writeFile(String filename,int points, int rebounds, int layups){
     try{
        // Create file 
        FileWriter fstream = new FileWriter(filename);
            BufferedWriter out = new BufferedWriter(fstream);
        out.write("Points,"+points+",rebounds,"+rebounds+"layups,"+layups);
        //Close the output stream
        out.close();
        }catch (Exception e){//Catch exception if any
          System.err.println("Error: " + e.getMessage());
        }
      }
    }
    where do I put that code in the source?

  • #4
    Regular Coder
    Join Date
    Feb 2008
    Location
    Edinburgh - Scotland
    Posts
    107
    Thanks
    0
    Thanked 12 Times in 12 Posts
    You would put it underneath your Main - as its a different method.

    As follows:

    Code:
    import java.util.Scanner
    
    class basketball 
    {
    	public static void main (String [] args)
    	{
    		Scanner input = new Scanner (System.in) ();
    		System.out.println("How many points did you score?");
    		points = input.nextInt();
    		System.out.println("How many assists did you dish?");
    		assists = input.nextInt();
    		System.out.println("How many boards did you grab?");
    		rebounds = input.nextInt();
    		
    	}
    
    public void writeFile(String filename,int points, int rebounds, int layups){
     try{
        // Create file 
        FileWriter fstream = new FileWriter(filename);
            BufferedWriter out = new BufferedWriter(fstream);
        out.write("Points,"+points+",rebounds,"+rebounds+"layups,"+layups);
        //Close the output stream
        out.close();
        }catch (Exception e){//Catch exception if any
          System.err.println("Error: " + e.getMessage());
        }
      }
    }
    You will need a couple of different import statements for FileWriter and BufferedWriter but if you are using eclipse or something similar it will help you with that!

  • Users who have thanked renegadeandy for this post:

    roher4 (12-07-2010)

  • #5
    New Coder
    Join Date
    Jul 2010
    Location
    Canada
    Posts
    26
    Thanks
    2
    Thanked 0 Times in 0 Posts
    ok thanks

  • #6
    Regular Coder
    Join Date
    Feb 2008
    Location
    Edinburgh - Scotland
    Posts
    107
    Thanks
    0
    Thanked 12 Times in 12 Posts
    Woops! Just noticed another error in your code - in the Scanner declaration you have an extra set of (), it should be as follows :

    Code:
    Scanner input = new Scanner (System.in);

  • #7
    New Coder
    Join Date
    Jul 2010
    Location
    Canada
    Posts
    26
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Thanks, it was really late at night when I put this simple program that I failed at together. This will be really useful thanks.

  • #8
    Regular Coder
    Join Date
    Feb 2008
    Location
    Edinburgh - Scotland
    Posts
    107
    Thanks
    0
    Thanked 12 Times in 12 Posts
    Its a pleasure - be sure to report back how you got along

  • #9
    New Coder
    Join Date
    Jul 2010
    Location
    Canada
    Posts
    26
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by renegadeandy View Post
    Its a pleasure - be sure to report back how you got along

    i don't have access to the file right now but when i try, ill post it...


  •  

    Posting Permissions

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