Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-04-2010, 06:57 PM   PM User | #1
roher4
New Coder

 
Join Date: Jul 2010
Location: Canada
Posts: 26
Thanks: 2
Thanked 0 Times in 0 Posts
roher4 is an unknown quantity at this point
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();
		
	}
}
roher4 is offline   Reply With Quote
Old 12-07-2010, 11:30 PM   PM User | #2
renegadeandy
Regular Coder

 
Join Date: Feb 2008
Location: Edinburgh - Scotland
Posts: 107
Thanks: 0
Thanked 12 Times in 12 Posts
renegadeandy is an unknown quantity at this point
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
renegadeandy is offline   Reply With Quote
Old 12-07-2010, 11:35 PM   PM User | #3
roher4
New Coder

 
Join Date: Jul 2010
Location: Canada
Posts: 26
Thanks: 2
Thanked 0 Times in 0 Posts
roher4 is an unknown quantity at this point
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?
roher4 is offline   Reply With Quote
Old 12-07-2010, 11:39 PM   PM User | #4
renegadeandy
Regular Coder

 
Join Date: Feb 2008
Location: Edinburgh - Scotland
Posts: 107
Thanks: 0
Thanked 12 Times in 12 Posts
renegadeandy is an unknown quantity at this point
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!
renegadeandy is offline   Reply With Quote
Users who have thanked renegadeandy for this post:
roher4 (12-07-2010)
Old 12-07-2010, 11:41 PM   PM User | #5
roher4
New Coder

 
Join Date: Jul 2010
Location: Canada
Posts: 26
Thanks: 2
Thanked 0 Times in 0 Posts
roher4 is an unknown quantity at this point
ok thanks
roher4 is offline   Reply With Quote
Old 12-07-2010, 11:46 PM   PM User | #6
renegadeandy
Regular Coder

 
Join Date: Feb 2008
Location: Edinburgh - Scotland
Posts: 107
Thanks: 0
Thanked 12 Times in 12 Posts
renegadeandy is an unknown quantity at this point
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);
renegadeandy is offline   Reply With Quote
Old 12-07-2010, 11:51 PM   PM User | #7
roher4
New Coder

 
Join Date: Jul 2010
Location: Canada
Posts: 26
Thanks: 2
Thanked 0 Times in 0 Posts
roher4 is an unknown quantity at this point
Thanks, it was really late at night when I put this simple program that I failed at together. This will be really useful thanks.
roher4 is offline   Reply With Quote
Old 12-07-2010, 11:59 PM   PM User | #8
renegadeandy
Regular Coder

 
Join Date: Feb 2008
Location: Edinburgh - Scotland
Posts: 107
Thanks: 0
Thanked 12 Times in 12 Posts
renegadeandy is an unknown quantity at this point
Its a pleasure - be sure to report back how you got along
renegadeandy is offline   Reply With Quote
Old 12-08-2010, 12:37 AM   PM User | #9
roher4
New Coder

 
Join Date: Jul 2010
Location: Canada
Posts: 26
Thanks: 2
Thanked 0 Times in 0 Posts
roher4 is an unknown quantity at this point
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...
roher4 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:14 AM.


Advertisement
Log in to turn off these ads.