나는이 문제를 해결하는 데 많은 어려움을 겪었지만, 다른 방법과 해결 방법을 시도했지만 너무 매번 얻을 수있는 오류의 양을 코드를 만지는 것을 두려워한다고 생각합니다. !Java ArrayindexOutofBoundsException from char to int
문제는 현재 각 요소에 1000 개의 임의로 생성 된 숫자가있는 char 배열에서 읽는 중입니다. 1-6 사이의 범위에있는이 String/chars 값에 대한 통계를 표시하고 통계 배열에 저장 한 다음 인쇄해야합니다. 어디에서 잘못 했습니까?
Heres는 내 코드의 조각 :
public static int[] showGame(String tempResult) throws IOException{
int statistics [] = new int [6];
char [] resultat = new char[1000];
String tempStr;
try{
BufferedReader indata = new BufferedReader(new FileReader("DiceGame.txt"));
tempStr = indata.toString();
char result [] = tempStr.toCharArray();
for (int i = 0; i < 1000; i++) {
resultat[i] = tempStr.charAt(i);
switch (result[i]) {
case '1':
statistics[0]++;
break;
case '2':
statistics[1]++;
break;
case '3':
statistics[2]++;
break;
case '4':
statistics[3]++;
break;
case '5':
statistics[4]++;
break;
case '6':
statistics[5]++ ;
break;
}
}
} catch (IOException ex) {
JOptionPane.showMessageDialog(null, "An error occured: " + ex);
}catch (NumberFormatException e){
JOptionPane.showMessageDialog(null,"Error: " + e);
}
//Arrays.toString(statistics);
return statistics;
}
편집 : 내가 주에서 제대로 호출 아니에요 때문에 OR이 될 수있다?!
public static void main(String[] args) throws IOException {
int []Statistik = new int[6];
String tossValue = JOptionPane.showInputDialog("set value for amount f toss");
DiceWithArray.Game(tossValue); //Kasta in värdet in i klass metoden
String tempToss = DiceWithArray.Game(tossValue);
Statistik = DiceWithArray.showGame(tempToss);
System.out.println("Resultatet : " + Arrays.toString(Statistik));
'BufferedReader.toString'은 당신이 생각하는대로하지 않습니다. 'BufferedReader'를 사용하여 파일을'read' 또는'readLine'을 통해 읽고 char 배열을 만들어야합니다. – Zircon
당신의 디버거 인더타는 무엇을 말합니까? 그것은 당신이 기대 한 것입니까? 길이가 원하는대로 줄 지니? 아마 이것들 중 하나에 있지 않을 것입니다. 또한. 일반적으로 전체 파일을 읽으려는 경우 1000을 하드 코드하는 것은 나쁜 습관입니다 .length – CodeMonkey