Note that there are some explanatory texts on larger screens.

plurals
  1. PODates from a text file
    primarykey
    data
    text
    <p>I just wanted to ask whether it is possible to find Dates in all possible formats in a text file and print the results. I was able to open the file and format the dates but I couldn't combine the two elements. Here are the codes so far. I appreciate any help.Thanks</p> <p>here are the requirements and my approach : </p> <p>Code needs to search all headings, headers, text and footnotes. Code searches for a date, such as day of week, month, numeral 1 to 31 followed by a month, year between 1950 &amp; 2050. Code needs to get date as well as the nearest heading above and get the applicable main section Code needs to get page number.</p> <p>For Date :</p> <pre><code>import java.text.*; import java.util.*; public class Dates { public static void main(String[] args) { Date d1 = new Date(); DateFormat[] dfa = new DateFormat[6]; dfa[0] = DateFormat.getInstance(); dfa[1] = DateFormat.getDateInstance(); dfa[2] = DateFormat.getDateInstance(DateFormat.SHORT); dfa[3] = DateFormat.getDateInstance(DateFormat.MEDIUM); dfa[4] = DateFormat.getDateInstance(DateFormat.LONG); dfa[5] = DateFormat.getDateInstance(DateFormat.FULL); for(DateFormat df : dfa) { System.out.println(df.format(d1)); } DateFormat df2 = DateFormat.getDateInstance(DateFormat.FULL); String s = df2.format(d1); System.out.println(s); try { Date d2 = df2.parse(s); System.out.println("parsed = " +d2.toString()); } catch(ParseException pe) { System.out.println("Parse Exception"); } } } </code></pre> <p>for opening files : </p> <pre><code>import javax.swing.*; import java.io.*; import java.util.*; public class FileOpener { /** * use a dialog box to select a text file (.txt) * @return a Scanner for the selected file, or null if cancel selected */ public static Scanner selectTextFile() { do { JFileChooser chooser = new JFileChooser(); FileNameExtensionFilter filter = new FileNameExtensionFilter( "Text/Java files","doc", "txt", "java"); chooser.setFileFilter(filter); int returnVal = chooser.showOpenDialog(null); try { if(returnVal == JFileChooser.APPROVE_OPTION) { return new Scanner(chooser.getSelectedFile()); } else { return null; } } catch (FileNotFoundException e) { JOptionPane.showMessageDialog(null, "Invalid file!", "error", JOptionPane.ERROR_MESSAGE); } } while (true); } /** * given a String, uses a Scanner to count the number of words * @return number of words in the String */ public static int countWordsOnLine(String line) { Scanner s = new Scanner(line); //int count = 0; while (s.hasNext()) { s.next(); //count++; } //return count; } public static void main(String[] args) { // make Java look like your normal OS try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (Exception e) { // ignore exceptions and continue } Scanner lineScanner = FileOpener.selectTextFile(); int numberOfWords = 0; if (lineScanner!=null) { while (lineScanner.hasNextLine()) { numberOfWords += FileOpener.countWordsOnLine( lineScanner.nextLine()); } System.out.println("The number of words is: " + numberOfWords); //System.out.println(getPageNumber()); } } } </code></pre> <p>Table that will contain the end result:</p> <pre><code>import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTable; import java.awt.Dimension; import java.awt.GridLayout; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; public class SimpleTableDemo extends JPanel { private boolean DEBUG = false; public SimpleTableDemo() { super(new GridLayout(1,0)); String[] columnNames = {"HEADER", "SENTENCE", "PAGE", "DATE"}; Object[][] data = { {" ", " ", " ", new Integer(5)}, {" ", " ", " ", new Integer(3)}, {" ", " ", " ", new Integer(2)}, {" ", " ", " ", new Integer(20)}, {" ", " ", " ", new Integer(10)} }; final JTable table = new JTable(data, columnNames); table.setPreferredScrollableViewportSize(new Dimension(500, 70)); table.setFillsViewportHeight(true); if (DEBUG) { table.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) { printDebugData(table); } }); } //Create the scroll pane and add the table to it. JScrollPane scrollPane = new JScrollPane(table); //Add the scroll pane to this panel. add(scrollPane); } private void printDebugData(JTable table) { int numRows = table.getRowCount(); int numCols = table.getColumnCount(); javax.swing.table.TableModel model = table.getModel(); System.out.println("Value of data: "); for (int i=0; i &lt; numRows; i++) { System.out.print(" row " + i + ":"); for (int j=0; j &lt; numCols; j++) { System.out.print(" " + model.getValueAt(i, j)); } System.out.println(); } System.out.println("--------------------------"); } /** * Create the GUI and show it. For thread safety, * this method should be invoked from the * event-dispatching thread. */ private static void createAndShowGUI() { //Create and set up the window. JFrame frame = new JFrame("SimpleTableDemo"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Create and set up the content pane. SimpleTableDemo newContentPane = new SimpleTableDemo(); newContentPane.setOpaque(true); //content panes must be opaque frame.setContentPane(newContentPane); //Display the window. frame.pack(); frame.setVisible(true); } public static void main(String[] args) { //Schedule a job for the event-dispatching thread: //creating and showing this application's GUI. javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGUI(); } }); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload