Java-Gaming.org Java4K winners: [ by our judges | by the community ]         
Featured games (67)
games approved by the League of Dukes
Games in Showcase (∞)
games submitted by our members



News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  Print  
  varargs question  (Read 249 times)
0 Members and 2 Guests are viewing this topic.
Offline wingsOrc

JGO n00b
*

Posts: 6



« on: 2012-01-21 19:38:28 »

I have the following code:
1  
2  
3  
4  
5  
6  
7  
8  
   public static String dumpHex(byte... hex) {
      String format = "$";

      for (int i = 0; i < hex.length; i++) {
         format += "%02X ";
      }
      return String.format(format,hex);
   }


My methods takes varargs and String.format too. But this gives me a IllegalFormatConversionException. How do I solve this ?
Offline ra4king

JGO Kernel
*****

Posts: 3160
Medals: 196


I'm the King!


« Reply #1 on: 2012-01-21 21:33:23 »

I don't know about formatting but the Javadoc point to this for more details Smiley

Offline Matthias

Jr. Member
**

Posts: 71
Medals: 2


TWL - Themable Widget Library


« Reply #2 on: 2012-01-22 08:56:28 »

String.format expects an Object[] where each entry corresponds to one argument you want to format.
You could try this:
1  
2  
3  
4  
5  
6  
...
Object[] tmp = new Object[hex.length];
for(int i=0 ; i<hex.length ; i++) {
   tmp[i] = Integer.valueOf(hex[i] & 255);
}
return String.format(format, tmp);
Pages: [1]
  Print  
 
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.16 | SMF © 2011, Simple Machines Valid XHTML 1.0! Valid CSS!
Page created in 0.093 seconds with 19 queries.