2011-09-25 6 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) 옥 토 페스트의 aanstaande의

    • 둘째 월요일.
    • 첫 번째 일요일 2012

    이들의 춘분 후 첫 만월 후 모든 날짜가 있으며, 더 많은 가능성이 가능합니다. 대부분의 날짜를 찾을 수는 있지만 모든 날짜는 찾을 수없는 스크립트를 작성할 수 있습니다.

    그러나 가장 좋은 전략은 일반적인 패턴을 여러 개 정의하고 텍스트 파일을 한 줄씩 스캔하는 것입니다.

  • 관련 문제