2015-01-08 4 views
2
System.out.println("Please choose an option to forsort the following stats"); 
System.out.println("A.Player's j=0;Name"); 
System.out.println("B.Player's FG%"); 
System.out.println("C.Player's 3P%"); 
System.out.println("D.Player's FT%"); 
System.out.println("E.Player's REB"); 
System.out.println("F.Player's AST"); 
System.out.println("G.Player's PTS"); 

if(player.equalsIgnoreCase("A")) 
{ 
    for (int int x=1;x<s.length;++x) 
    { 
     for (int y=1;y<s.length-1;++y) 
       if (s[y][0].compareTo(s[y+1][0])>0) 
       { 
        String temp[]=s[y]; 
        s[y]= s[y+1]; 
        s[y+1]=temp; 
       } 
      } 
     } 
    } 
} 

예를 들어, 사용자가 a를 누르면 플레이어 이름으로 만 정렬하고 또한 을 말하는 stats[j][i]=temp[i];에 대한 오류가 발생합니다. 나는 왜 나는이 오류를 계속하는지 모르겠다.Java에서 특정 열을 정렬 하시겠습니까?

다음은 내 데이터 텍스트 파일입니다. 플레이어의 이름은 0 열, Lou, Kyle, Patrick 등입니다. 이걸 얻으면 다른 것들도 쉽게 분류 할 수 있습니까?

Lou Williams,41.1,36.3,87.6,60,53,508 
Kyle Lowry,44.9,35.1,81.3,160,260,702 
Patrick Patterson,48.8,46.0,75.0,177,61,286 
Terrence Ross,42.9,38.7,87.9,119,30,411 
Jonas Valanciunas,54.2,0.0,79.2,283,16,414 
Amir Johnson,57.8,38.9,60.0,171,47,295 
James Johnson,58.3,19.4,65.7,129,46,247 
Demar Derozan,39.4,25.0,82.3,67,42,310 
Landry Fields,52.9,50.0,83.3,22,13,42 
Greivis Vasquez,38.7,31.7,71.1,81,119,321 
+0

짧은 ANS를 wer :'String [] [] stats = new String [10] [6]; ' –

+0

게시 된 코드의 98 %는 질문과 관련이 없습니다. 특히 printlns. 문제를 시연하는 데 필요한 최소한으로 잘라내십시오. [SSCCE] (http://sscce.org) – Bohemian

+0

7 열 – Gus

답변

0

또 다른 대답,하지만 지금은 추가적인 제약 조건이 추가 클래스 없다

File file = new File("file.txt"); 
FileReader reader = new FileReader(file); 
BufferedReader r = new BufferedReader(reader); 

String[][] stats = new String[10][]; 
String line; 
int index = 0; 
while ((line = r.readLine()) != null && index < 10) 
stats[index++] = line.split(","); 

// sort based on the first column 
for (int i = 0; i < stats.length; ++i) 
    for (int j = i + 1; j < stats.length; ++j) 
     if (stats[i][0].compareTo(stats[j][0]) > 0) { 
      // swap 
      String[] temp = stats[i]; 
      stats[i] = stats[j]; 
      stats[j] = temp; 
     } 
for(int i=0;i<stats.length;++i){ 
    for(int j=0;j<stats[i].length;++j) 
     System.out.print(stats[i][j] + " "); 
    System.out.println(); 
} 
+0

Niels Billen? 나는 문제가있다 –

+0

@ Let'sgothebeastboy 무엇이 문제인가? –

+0

nvm 나는 그것을 고쳤다. 어리석은 실수였다. –

1

OO 방식으로 해결합니다.

private class Statistic { 
    private String[] string; 

    public Statistic(String... strings) { 
     this.string = strings; 
    } 

    public String get(int index) { 
     return string[index]; 
    } 

    @Override 
    public String toString() { 
     StringBuilder builder = new StringBuilder(); 
     for (String string : this.string) 
      builder.append(string).append(" "); 
     return builder.toString(); 
    } 
} 

public static Comparator<Statistic> getComparator(final int column) { 
    return new Comparator<Statistic>() { 
     @Override 
     public int compare(Statistic o1, Statistic o2) { 
      return o1.get(column).compareTo(o2.get(column)); 
     } 
    }; 
} 

테스트 코드 :

File file = new File("file.txt"); 
FileReader reader = new FileReader(file); 
BufferedReader r = new BufferedReader(reader); 

List<Statistic> statistics = new ArrayList<Statistic>(); 
String line; 
while ((line = r.readLine()) != null) { 
    statistics.add(new Statistic(line.split(","))); 
} 

int column = 0; 
Collections.sort(statistics, getComparator(column)); 
for (Statistic statistic : statistics) 
    System.out.println(statistic); 
I는 Strings 배열과 특정 열을 따라 Statistic 오브젝트를 정렬하는 Comparator를 돌려 고정 방법으로서, 하나의 플레이어의 통계를 저장하는 Statistic 클래스를 생성

출력 :

Amir Johnson 57.8 38.9 60.0 171 47 295 
Demar Derozan 39.4 25.0 82.3 67 42 310 
Greivis Vasquez 38.7 31.7 71.1 81 119 321 
James Johnson 58.3 19.4 65.7 129 46 247 
Jonas Valanciunas 54.2 0.0 79.2 283 16 414 
Kyle Lowry 44.9 35.1 81.3 160 260 702 
Landry Fields 52.9 50.0 83.3 22 13 42 
Lou Williams 41.1 36.3 87.6 60 53 508 
Patrick Patterson 48.8 46.0 75.0 177 61 286 
Terrence Ross 42.9 38.7 87.9 119 30 411 
+0

Nielis Billen이 포함되어 있습니까? 난 단지 열 0, 모든 일을하고 싶지 않아 그리고 내가 할 수있는 무시 무시한 아무것도 함께하고 싶지 않아 이것은 10 % 가치가 내 최종 프로젝트입니다 –

+0

@ Let'sgothebeastboy, 알았어 난 당신의 질문에서 그 제약을받지 못했어요 ;) 나는 다른 게시물을 다음에 만들거야;) –

+0

Niels Billen 그리고 내가 물었던 것을 도와주세요, 사촌 혼란스러워 질 것입니다 –

2

는 "내가 기록하는 오류 (@ Let'sgothebeastboy 요청에 따라)가 생성되어야한다 [J] [i]를 임시 = [I]; java.lang.ArrayIndexOutOfBoundsException : 6. Idk 왜이 오류가 계속 발생합니까? "

가 있기 때문에 statsString[6][10]로 할당되기 때문에 최초의 인덱스는 0에서 5까지의 범위에있을 수 있지만 jfor (int j=0; j<10; j++)에서오고, 그래서 6 될 때, 당신은 예외를 얻을.

당신이 첫 번째 열을 기준으로 정렬 할 경우

지금, 당신은 정말 그냥 목록 및 종류에 침을 읽을 등 어떤 분할이 필요하지 않습니다.

List<String> strings = new ArrayList<>(); 
String s = null; 
while((s = br.readLine()) != null) strings.add(s); 
Collections.sort(strings); 

완료.

관련 문제