2013-03-05 2 views
22

मैं इस तरह की जानकारी को व्यवस्थित करना चाहते हैं:कंसोल में ASCII का उपयोग करके मैं तालिका कैसे बना सकता हूं?

जानकारी, कोशिकाओं के साथ आयोजित किया जाता है, जबकि System.out.println साथ जानकारी बहुत अव्यवस्थित होगा।

or this

+2

+1। बुरा सवाल नहीं है। अगर किसी के पास इसके लिए अच्छी लाइब्रेरी है, तो कृपया अनुशंसा करें। अन्यथा, System.out.format – Thilo

+1

देखें, आप [this] (http://stackoverflow.com/questions/15193812/how-to-print-a-table-of-arrays/15194265#15194265) को देख सकते हैं उदाहरण – MadProgrammer

+0

आपके सर्वोत्तम शर्त मुख्य रूप से 'System.out.printf (...)' और समकक्ष 'System.out.format (...)' का उपयोग करने के लिए हैं। –

उत्तर

38

System.out.format() या System.out.printf() (printf का उपयोग करने का प्रयास करें, बस format को आमंत्रित करता है, इसलिए दोनों विधियां एक ही परिणाम देती हैं)।

यहां आपके पास एक साधारण उदाहरण है जो टेक्स्ट को बाएं से संरेखित करने और रिक्त स्थान के साथ अप्रयुक्त स्थानों को भरने का प्रयास करेगा। बाएं से संरेखित स्ट्रिंग को %-15s के साथ प्राप्त किया जा सकता है, जिसका अर्थ है स्ट्रिंग 15 स्ट्रिंग (s) डेटा के लिए स्थान और बाएं (-) से इसे लिखना शुरू करें। यदि आप अंक जोड़ना चाहते हैं तो d प्रत्यय %-4d जैसे अधिकतम 4 अंक संख्याओं के लिए कॉलम के बाईं तरफ रखा जाना चाहिए।
बीटीडब्लू मैंने \n के बजायका उपयोग किया ताकि वर्तमान ओएस द्वारा विंडोज ओएस द्वारा उपयोग किए जाने वाले लाइन सेपरेटर अनुक्रम का प्रतिनिधित्व किया जा सके, यह \r\n होगा।

आप Formatter class documentation पर अधिक जानकारी प्राप्त कर सकते हैं।

String leftAlignFormat = "| %-15s | %-4d |%n"; 

System.out.format("+-----------------+------+%n"); 
System.out.format("| Column name  | ID |%n"); 
System.out.format("+-----------------+------+%n"); 
for (int i = 0; i < 5; i++) { 
    System.out.format(leftAlignFormat, "some data" + i, i * i); 
} 
System.out.format("+-----------------+------+%n"); 

उत्पादन

+-----------------+------+ 
| Column name  | ID | 
+-----------------+------+ 
| some data0  | 0 | 
| some data1  | 1 | 
| some data2  | 4 | 
| some data3  | 9 | 
| some data4  | 16 | 
+-----------------+------+ 
+0

¡¡एक सही उत्तर !! ¡बहुत बहुत धन्यवाद! मैं ऐसा कर सकता था जो मैं चाहता था =) –

+0

निश्चित आवश्यकताओं के लिए, यह समाधान सही है। अधिक वास्तविक जीवन के मामलों के लिए, पुस्तकालय अधिक उपयुक्त है। यह उत्तर देखें: http://stackoverflow.com/a/35961774/363573। – Stephan

5

उपयोग System.out.printf()

उदाहरण के लिए,

String s = //Any string 
System.out.printf(%10s, s); 

बाहर स्ट्रिंग रों की सामग्री मुद्रित, वास्तव में 10 अक्षर ले जाएगा। तो यदि आप एक टेबल चाहते हैं, तो बस सुनिश्चित करें कि तालिका में प्रत्येक कक्ष एक ही लंबाई में मुद्रित है। यह भी ध्यान दें कि printf() एक नई लाइन मुद्रित नहीं करता है, इसलिए आपको इसे स्वयं प्रिंट करना होगा।

0

आप सही विधि साथ String.Format() का उपयोग कर सकते कोड कुछ इस तरह दिखाई दे सकता है मैं लगता है कि

StringBuilder sb=new StringBuilder(); 

for(int i = 1; i <= numberOfColumns; i++) 
{ 
     sb.append(String.format(%-10s,rsMetaData.getColumnLabel(i); 
} 

पुस्तकालय मुझे नहीं लगता है कि वहाँ किसी भी है कि काम करते हैं जाएगा, फिर भी मैं हो सकता है के रूप में गलत! वास्तव में इस पर शोध करना होगा

इसके अलावा इस http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Formatter.html#syntax

0

TUIAWT पर एक नजर है आप एक कंसोल विंडो में AWT घटकों का उपयोग करने देता है। ऐसा लगता है कि यह List या Table का समर्थन करता है, लेकिन यह आपको एक प्रारंभिक बिंदु दे सकता है।

+0

इस लेखन के अनुसार, अंतिम अद्यतन टीयूआईएडब्ल्यूटी को मार्च 2015 में लगता है। – Stephan

4

आप java-ascii-table इस्तेमाल कर सकते हैं। the author's site भी देखें।

+3

जावा-एएससीआई-टेबल छोड़ दिया गया लगता है। इस उत्तर को वैकल्पिक के लिए देखें: http://stackoverflow.com/a/35961774/363573 – Stephan

1

यह भी अच्छी तरह से http://sourceforge.net/projects/texttablefmt/ काम करता है। अपाचे भी लाइसेंस प्राप्त है।

+0

टेक्स्टटेबलएफटी मेवेन पर उपलब्ध नहीं प्रतीत होता है। मेवेन पर एक और लाइब्रेरी उपलब्ध है: http://stackoverflow.com/a/35961774/363573 – Stephan

13

इस विकल्प को आजमाएं: asciitable

यह टेक्स्ट तालिका के कई कार्यान्वयन प्रदान करता है, मूल रूप से सीमाओं के लिए ASCII और UTF-8 वर्णों का उपयोग करता है।http://mvnrepository.com/artifact/de.vandermeer/asciitable

भी देखें:

 ┌──────────────────────────────────────────────────────────────────────────┐ 
    │ Table Heading               │ 
    ├──────────────────┬──────────────────┬──────────────────┬─────────────────┤ 
    │ first row (col1) │ with some  │ and more   │ even more  │ 
    │     │ information  │ information  │     │ 
    ├──────────────────┼──────────────────┼──────────────────┼─────────────────┤ 
    │ second row  │ with some  │ and more   │ even more  │ 
    │ (col1)   │ information  │ information  │     │ 
    │     │ (col2)   │ (col3)   │     │ 
    └──────────────────┴──────────────────┴──────────────────┴─────────────────┘

नवीनतम संस्करण खोजें: https://stackoverflow.com/a/39806611/363573

5

मेरी कक्षा मैं ऐसा करने के लिए विशेष रूप से बनाई गई पूरी तरह से है

यहां एक नमूना तालिका है गतिशील: https://github.com/MRebhan/crogamp/blob/master/src/com/github/mrebhan/crogamp/cli/TableList.java

आप इस तरह इसका इस्तेमाल कर सकते हैं:

TableList tl = new TableList(3, "ID", "String 1", "String 2").sortBy(0).withUnicode(true); 
// from a list 
yourListOrWhatever.forEach(element -> tl.addRow(element.getID(), element.getS1(), element.getS2())); 
// or manually 
tl.addRow("Hi", "I am", "Bob"); 

tl.print(); 

यह यूनिकोड वर्ण के साथ इस तरह दिखेगा (ध्यान दें: के बाद से सभी वर्ण समान रूप से व्यापक हैं कंसोल में बेहतर दिखेगा):

┌─────────┬─────────────────────────────────────────────────────────────────────────┬────────────────────────────┐ 
│ Command │ Description                │ Syntax      │ 
┢━━━━━━━━━╈━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╈━━━━━━━━━━━━━━━━━━━━━━━━━━━━┪ 
┃ bye  ┃ Quits the application.             ┃       ┃ 
┃ ga  ┃ Adds the specified game.            ┃ <id> <description> <path> ┃ 
┃ gl  ┃ Lists all currently added games           ┃ [pattern]     ┃ 
┃ gr  ┃ Rebuilds the files of the currently active game.      ┃       ┃ 
┃ gs  ┃ Selects the specified game.            ┃ <id>      ┃ 
┃ help ┃ Lists all available commands.           ┃ [pattern]     ┃ 
┃ license ┃ Displays licensing info.            ┃       ┃ 
┃ ma  ┃ Adds a mod to the currently active game.        ┃ <id> <file>    ┃ 
┃ md  ┃ Deletes the specified mod and removes all associated files.    ┃ <id>      ┃ 
┃ me  ┃ Toggles if the selected mod is active.         ┃ <id>      ┃ 
┃ ml  ┃ Lists all mods for the currently active game.       ┃ [pattern]     ┃ 
┃ mm  ┃ Moves the specified mod to the specified position in the priority list. ┃ <id> <position>   ┃ 
┃ top kek ┃ Test command. Do not use, may cause death and/or destruction   ┃       ┃ 
┃ ucode ┃ Toggles advanced unicode. (Enhanced characters)       ┃ [on|true|yes|off|false|no] ┃ 
┗━━━━━━━━━┻━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┻━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛ 

और यूनिकोड वर्णों के साथ (.withUnicode (सत्य) को छोड़ दें):

Command | Description                | Syntax      
--------+-------------------------------------------------------------------------+--------------------------- 
bye  | Quits the application.             |       
ga  | Adds the specified game.            | <id> <description> <path> 
gl  | Lists all currently added games           | [pattern]     
gr  | Rebuilds the files of the currently active game.      |       
gs  | Selects the specified game.            | <id>      
help | Lists all available commands.           | [pattern]     
license | Displays licensing info.            |       
ma  | Adds a mod to the currently active game.        | <id> <file>    
md  | Deletes the specified mod and removes all associated files.    | <id>      
me  | Toggles if the selected mod is active.         | <id>      
ml  | Lists all mods for the currently active game.       | [pattern]     
mm  | Moves the specified mod to the specified position in the priority list. | <id> <position>   
top kek | Test command. Do not use, may cause death and/or destruction   |       
ucode | Toggles advanced unicode. (Enhanced characters)       | [on|true|yes|off|false|no] 
संबंधित मुद्दे

 संबंधित मुद्दे