Results 1 to 6 of 6
  1. #1
    JTS25 is offline Member
    Join Date
    Oct 2010
    Posts
    3
    Rep Power
    0

    Unhappy Output console results to a HTML browser

    Hi, I am hoping for help as I can't seem to work out how to do the following:

    As part of an assignment I wrote a program that took a text file containing football results, stored each of the split up fields into an array, and displayed them in the console.

    I am now required to take that output and format it as HTML so it can be displayed in a browser using tags etc.

    I have spent ages trying to work this out and how it can be done but am stuck! Seen various references to using BufferedReader and PrintWritter to pass the output, but not sure how it can be shown as recognised by HTML.We haven't been taught applets and don't think our course covers that.

    Any help would be much appreciated, thanks. I have included the results file, and the code for the first part of the assignment.

    Java Code:
    import java.io.*;
    import java.util.Scanner;
    
    public class Generator {
    
    	/**
    	 * @ Program to read and output the contents of a football results text file
    	 * @ Results to show the total number of games, goals and invalid matches
    	 */
    	public static void main(String[] args) {
    		
    		try {
    			int homeGoals = 0;
    			int awayGoals = 0;
    			int validMatch = 0;
    			int invalidMatch = 0;
    			Scanner s, input = null;
    		
    			int totalGoals = 0;						// keeps a running total of the goals scored in the game
    						
    			s = new Scanner(System.in);				// create a scanner which scans user input
    			
    			System.out.print("Please enter the name of the text file you wish to generate stats from i.e. results.txt: ");
    			
    			String fileName = s.nextLine(); 		// read the input from the user
    			
    			File inFile = new File(fileName); 		// create a new File object
    			
    			input = new Scanner(inFile);			// put the contents of the file into the input
    			
    			while ( input.hasNext() ) {				// run whilst the input text has another line
    				
    				String line = input.nextLine();		// read the next line of text from the file in order to grab scores
    				
    				String [] splitupText = line.split(" : ");			// split the text into multiple elements
    				
    				if (splitupText.length == 4) {						// check that the array contains four values	
    				
    					//collect the results from each line, trim the whitespace and store in an array
    					String homeTeam = splitupText[0].trim();	
    					String awayTeam = splitupText[1].trim();	
    					String hGoals = splitupText[2].trim();		
    					String aGoals = splitupText[3].trim(); 
    					
    					// check all the elements contain a value
    					if ((homeTeam.length() > 0) && (awayTeam.length() > 0) && (hGoals.length() > 0) && (aGoals.length() > 0)){
    							
    						try {
    							homeGoals = Integer.parseInt(hGoals);	// convert string into a int so it can be added
    							awayGoals = Integer.parseInt(aGoals);
    							validMatch++;							// keep a track of the number of games
    						}
    						
    						catch (NumberFormatException e) {	// catch the exception when a string is being parsed
    							invalidMatch++;					// keeps a track of the invalid games
    							continue;						// loop back to the beginning
    						}
    						
    						int gameGoals = homeGoals + awayGoals;		// add up the goals from the game
    						
    						totalGoals = totalGoals + gameGoals;				// save a running total of goals
    					
    						// arrange the array values into the appropriate format and output to the console
    						System.out.println(homeTeam + " [" + hGoals + "] " + "| " + awayTeam + " [" + aGoals + "]");	
    					}
    					
    					else{		// if the element does contain an empty value
    						
    						invalidMatch++;		// keeps a track of the invalid games
    					}
    				}
    				
    				else{			// if the number of array elements does not equal four
    					
    					invalidMatch++;			// keeps a track of the invalid games
    
    				}
    			}
    			
    			// print the details on the screen	
    			System.out.println();
    			System.out.println("Valid match count was " + validMatch + ", total goals scored were " + totalGoals);
    			System.out.println("Invalid match count was " + invalidMatch);
    		}
    		
    		//Catch the exception - file not found
    		catch (FileNotFoundException e) {
    			
    			System.out.println("ERROR - file not found!");
    		}
    
    	}
    
    }
    Attached Files Attached Files

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    I am now required to take that output and format it as HTML so it can be displayed in a browser using tags etc.

    Html files are just plain text files, so the problem is somewhat simpler than suggested by the title of this thread. You are not so much outputting to an HTML browser as writing the output with some formatting changes to a file.

    Usage of PrintWriter is explained in the Formatting section of Oracle's Tutorial. It illustrates the idea with System.out - a PrintStream - but you should be able to modify things to work with a PrintWriter which can be constructed from a file:

    Java Code:
    public static void main(String[] args) {
        try {
            PrintWriter out = new PrintWriter("output.txt");
    
            // try the rest of your code using out rather than System.out
    
            // etc
            // (out has to be closed when you're done)

    This won't get you HTML formatted text of course. For that you need to add some html tags as appropriate:

    * For a new line start the next line with "<br />"
    * Start paragraphs by writing <p> and end them by writing "</p>"
    * For tables you will have to be creative!
    * HTML documents have to start and end in a valid way. In the simplest case you might start your document by writing "<html><body>" and end it with "</body></html>"

    Your main() method is long as it is, so consider using more methods.

    As well as the PrintWriter tutorial linked to above you may want to read up on how you go about file output i general terms - how to deal file names that are "bad" (like you don't have access writes etc), how to deal with i/o errors, how and why to close file resources when you are finished with them. Having said that, the main focus of your problem is formatting so start with that.

  3. #3
    JTS25 is offline Member
    Join Date
    Oct 2010
    Posts
    3
    Rep Power
    0

    Default

    Thanks very much for that information. You've given some good advice

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    You're welcome - good luck!

    (of course if you get stuck post back with a description/code of the approach you're taking and the bad/frustrating things that happen)

  5. #5
    JTS25 is offline Member
    Join Date
    Oct 2010
    Posts
    3
    Rep Power
    0

    Default

    Usage of PrintWriter is explained in the Formatting section of Oracle's Tutorial. It illustrates the idea with System.out - a PrintStream - but you should be able to modify things to work with a PrintWriter which can be constructed from a file:
    I've made a start on writing the code using the PrintWriter, and have actually got something to display in the browser. :O)

    Your main() method is long as it is, so consider using more methods.
    I'm still getting used to Java way of programming i.e. using methods instead of everything in one chunk. Would I use a method to return the array of results?

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    Methods are handy when you find yourself writing similar over and over again. It is not a given that your code is like that. I was actually thinking along the lines of methods that would format strings for use in html (making a paragraph, making text bold, making text show up as a cell in a table, ...) and your code may not involve such uses.

    Concentrate on getting the code correct (tested and meeting the requirements you were given) first and post code if you want people to comment on it.

Similar Threads

  1. Need help with printing console output to JTextArea
    By ShinTec in forum AWT / Swing
    Replies: 4
    Last Post: 06-04-2010, 11:10 AM
  2. output to terminal instead of console on mac
    By firen in forum Advanced Java
    Replies: 0
    Last Post: 06-01-2010, 01:42 PM
  3. Output to results file help!
    By javanator in forum New To Java
    Replies: 6
    Last Post: 04-18-2010, 02:59 AM
  4. How Do You?? Get the Console Output as a GUI??
    By Lyricid in forum AWT / Swing
    Replies: 10
    Last Post: 11-21-2009, 12:35 AM
  5. How to align the output on console?
    By sfe23 in forum New To Java
    Replies: 5
    Last Post: 03-30-2009, 04:28 AM

Posting Permissions

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