2014-06-15 3 views
0
Exception in thread "main" java.util.UnknownFormatConversionException: Conversion = '-' 

    at java.util.Formatter.checkText(Formatter.java:2579) 
    at java.util.Formatter.parse(Formatter.java:2565) 
    at java.util.Formatter.format(Formatter.java:2501) 
    at java.io.PrintStream.format(PrintStream.java:970) 
    at java.io.PrintStream.printf(PrintStream.java:871) 
    at IntGrid.printGrid(IntGrid.java:28) 
    at IntGrid.main(IntGrid.java:20) 

이 오류가 발생합니다. 나는 그것이 printf 라인에 대한 나의 포맷팅과 관련이 있다고 생각하지만 그것을 고치는 방법을 잘 모른다. 정수를 인쇄하려고 시도하고 전체 너비를 5 문자로 만들기 위해 몇 개의 공백이옵니다.Java 오류 - 알 수없는 형식 변환 예외

내 코드 :

import java.util.*; 

public class IntGrid { 
public static void main(String[] args) { 

    Scanner reader = new Scanner(System.in); 
    System.out.println("Please enter a number of rows:"); 
    int rows = reader.nextInt(); 
    System.out.println("Please enter a number of columns:"); 
    int columns = reader.nextInt(); 

    printGrid(rows, columns); 

} 

public static void printGrid(int rows, int columns) { 
    int start = 1; 
    for (int i = 1; i <= rows; i ++) { 
     for (int j = 1; j <= columns; j = j + rows) { 
      System.out.printf("%-5.d", j); 
     } 
     System.out.println(); 
    } 
} 
} 

모든 팁?

답변

0

대신이 할 수 있습니다 :

System.out.printf("%-5d", j); 

. 때문에 (소수점) 수레/복식에 사용됩니다.