【问题标题】:Java ArrayindexOutofBoundsException from char to int [duplicate]从char到int的Java ArrayindexOutofBoundsException [重复]
【发布时间】:2016-09-21 18:05:02
【问题描述】:

我很难弄清楚这一点,我尝试了不同的方法和解决方法,但我认为我什至不敢触摸代码,因为我每次都设法得到大量错误!

问题是我试图简单地从一个字符数组中读取,该数组当前在每个元素中有 1000 个随机生成的数字。 我需要显示这个字符串/字符值的统计信息,范围在 1-6 之间,并将其存储到统计数据数组中,然后打印出来。我在哪里做错了?

这是我的代码的 sn-p:

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;      
        }

编辑:或者可能是因为我没有从 main 正确调用它?!

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 通过readreadLine 读取文件并从中创建一个char 数组。
  • 你的 indata 调试器是怎么说的?是你所期望的吗?长度和你想要的一致吗?可能不会是这些中的任何一个。还。一般来说,如果您打算阅读整个文件,那么硬编码 1000 是不好的做法,请使用 .length

标签: java arrays char int dice


【解决方案1】:

你这样做:

for (int i = 0; i < 1000; i++) {       
  resultat[i] = tempStr.charAt(i);

好吧,你不能确定tempStr 必须是 1000 个字符或更长。做类似的事情:

for (int i = 0; i < 1000; i++) {    
  if(tempStr.length() <= i)
    break;
  resultat[i] = tempStr.charAt(i);

而且你不能只在BufferedReader上使用toString,通过nextLine方法读取整个文件,然后用它做事。

【讨论】:

  • 嗯,我让它工作了,但统计数据似乎不正确,我目前得到 2,1,0,0,2,1
  • 谢谢!我试过扫描仪,它现在可以正确地写出金额!
猜你喜欢
  • 1970-01-01
  • 2016-08-31
  • 2011-02-25
  • 1970-01-01
  • 1970-01-01
  • 2012-03-30
  • 2016-03-05
  • 2014-08-31
  • 2011-03-30
相关资源
最近更新 更多