2011-09-25 75 views
0

我只想問是否可以在文本文件中找到所有可能格式的日期並打印結果。我能夠打開文件並格式化日期,但我無法將這兩個元素結合起來。以下是目前的代碼。 我明白任何help.Thanks來自文本文件的日期

這裏的要求,我的方法:

代碼需要搜索所有標題,標題,正文和腳註。 代碼搜索日期,例如星期幾,月份,數字1到31,然後是一個月,年份是1950年& 2050. 代碼需要獲取日期以及上面最近的標題並獲取適用的主要部分 代碼需要獲取頁碼。

對於日期:

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"); 
} 
} 
} 

打開文件:

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()); 
} 
} 
} 

表將包含最終的結果:

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 < numRows; i++) { 
System.out.print(" row " + i + ":"); 
for (int j=0; j < 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(); 
} 
}); 
} 
} 
+0

您可能想要一致地縮進代碼以獲得更好的可讀性。 –

+1

請將您的代碼限制在相關部分,並提出具體問題。 –

回答

1

所有時間幾乎肯定是不可能的。你如何解析這些? 2012年3月

  • Donderdag 14十月aanstaan​​de的

    • 第二個星期一。 2012

    這些春分後的第一個月圓後

  • 第一個星期日是所有日期,還有更多的可能性是可能的。你可以編寫一個腳本來查找大部分日期,但不是全部。

    但是,您最好的策略是定義一些常用模式,並逐行掃描文本文件。