Java字符串格式不能按预期工作

时间:2017-08-02 15:59:12

标签: java string

请帮我解决字符串格式问题。 我有**/modules/*.py ,我将每个String添加到结果字符串中,该字符串将显示在String []中。当我将每个String添加到结果中时,我使用JOptionPane来确保添加到最终结果的每个字符串的最小长度为20并且左对齐。每5个单词在新行上开始。但是,String.format("%-20s", String[i])中显示的结果并非我所期望的结果。这些词没有对齐。每个单词似乎没有最小长度20

JOptionPane

2 个答案:

答案 0 :(得分:0)

首先检查getKeywordList()函数的结果是否符合预期。

例如

Unhandled Error
Traceback (most recent call last):
  File "C:\Python27\lib\site-packages\twisted\python\log.py", line 103, in 
callWithLogger
    return callWithContext({"system": lp}, func, *args, **kw)
  File "C:\Python27\lib\site-packages\twisted\python\log.py", line 86, in callWithContext
    return context.call({ILogContext: newCtx}, func, *args, **kw)
  File "C:\Python27\lib\site-packages\twisted\python\context.py", line 122, in callWithContext
   return self.currentContext().callWithContext(ctx, func, *args, **kw)
  File "C:\Python27\lib\site-packages\twisted\python\context.py", line 85, in callWithContext
    return func(*args,**kw)
--- <exception caught here> ---
  File "C:\Python27\lib\site-packages\twisted\internet\selectreactor.py", line 149, in _doReadOrWrite
    why = getattr(selectable, method)()
  File "C:\Python27\lib\site-packages\twisted\internet\tcp.py", line 208, in doRead
    return self._dataReceived(data)
  File "C:\Python27\lib\site-packages\twisted\internet\tcp.py", line 214, in _dataReceived
    rval = self.protocol.dataReceived(data)
  File "C:\Python27\lib\site-packages\twisted\protocols\basic.py", line 571, in dataReceived
    why = self.lineReceived(line)
  File "C:\Python27\lib\site-packages\twisted\protocols\basic.py", line 625, in lineReceived
    raise NotImplementedError
exceptions.NotImplementedError: 

所以你会得到:

  ...
  // print out for debug
  System.out.println(result);
  return result;
}

如果结果符合您的预期,那么问题与JOptionPane有关,可能需要用&amp; nbsp;替换空格。和\ n与&lt; br /&gt;

soupsbases          hotpots             meats               poultryies          beefs               
cows                oxes                bulls               porks               pigs                
oink                muttons             lambs               sheeps              chickens            
hens                roosters            seafoods            sea                 ocean               
shellfish           fishees             vegetables          veges               green               
plants              veggies             signatures          recommendations     recommend           
hi                  hello               yo                  hey                 morning             
afternoon           evening             lovely              great               good                
thanks              amazing             excellent           brilliant           outstanding         
wonderful           awesome             okay                bad                 lousy               
useless             stupid              brainless           foolish             gotget              
provides            menus               supplyies           offers              goodbye             
leave               end                 stop                

btw:如果你想拥有一个正确的对齐列,你将不得不使用等宽字体...所以将结果包装在&lt; pre&gt; ...&lt; / pre&gt;标签

答案 1 :(得分:0)

您正在尝试进行基于原始字符文本的对齐,但是这种技术假定文本是使用等宽字体呈现的。但是,JOptionPane的默认字体可能是按比例间隔的字体。但是试图将其字体设置为像Courier这样的东西可能是错误的想法,因为这会破坏Swing的预期外观和感觉。最好是打破基于文本的思维,而不是像UI开发人员那样思考。您真正想要的是在JTable组件的单元格中排列关键字字符串,并将其作为message参数传递给JOptionPane。该消息不必是StringString []。它可以是一流的嵌套组件对象。