简体   繁体   English

我不断收到错误“索引 5 超出长度 5 的范围”,但不知道要更改什么

[英]I keep getting the error "Index 5 out of bounds for length 5" but don't know what to change

I'm pretty new to coding so please bear with me, I am just experimenting with ways to shuffle an array for eventual use in shuffling a deck of cards in a card game I am creating in Java.我对编码很陌生,所以请耐心等待,我只是在尝试洗牌的方法,以便最终在我用 Java 创建的纸牌游戏中洗牌。 I know that index 5 is out of bounds for length 5 but what is puzzling to me is that the error doesn't ALWAYS pop up.我知道索引 5 超出了长度 5 的范围,但令我感到困惑的是错误并不总是弹出。 Sometimes I try to run my code and it works just fine, other times I get the error even if I haven't changed anything between times that I run it.有时我尝试运行我的代码并且它工作得很好,其他时候即使我在运行它的两次之间没有改变任何东西我也会得到错误。

public class Card{

    public static void shuffle(Object[] array) {

        int noOfCards = array.length;

        for (int i = 0; i < noOfCards; i++) {

            int s = i + (int)(Math.random() * (noOfCards - 1));

            Object temp = array[s]; //this is the first line it says has a problem
            array[s] = array[i];
            array[i] = temp;
        }
    }

    public static void main(String[] args) {

        String[] strOfCards = {"A","B","C","D","E"};

        Card.shuffle(strOfCards); //this is the second line that has a problem
        for(int i = 0; i < strOfCards.length; i++) {
            System.out.println(strOfCards[i] + " ");
        }
    }
}

I have no idea how to change the flawed lines, any suggestions are welcome!我不知道如何更改有缺陷的线条,欢迎提出任何建议! *** i have tried changing the number of letters in the string but then the error changes with it ie "Index 6 out of bounds for length 6" *** 我曾尝试更改字符串中的字母数,但随后错误随之更改,即“索引 6 超出长度 6 的范围”

I'm pretty new to coding so please bear with me, I am just experimenting with ways to shuffle an array for eventual use in shuffling a deck of cards in a card game I am creating in Java.我对编码很陌生,所以请耐心等待,我只是在尝试洗牌的方法,以最终在我用 Java 创建的纸牌游戏中洗牌。 I know that index 5 is out of bounds for length 5 but what is puzzling to me is that the error doesn't ALWAYS pop up.我知道索引 5 超出了长度 5 的范围,但令我感到困惑的是错误并不总是弹出。 Sometimes I try to run my code and it works just fine, other times I get the error even if I haven't changed anything between times that I run it.有时我尝试运行我的代码并且它工作得很好,有时我会收到错误,即使我在运行它的两次之间没有更改任何内容。

public class Card{

    public static void shuffle(Object[] array) {

        int noOfCards = array.length;

        for (int i = 0; i < noOfCards; i++) {

            int s = i + (int)(Math.random() * (noOfCards - 1));

            Object temp = array[s]; //this is the first line it says has a problem
            array[s] = array[i];
            array[i] = temp;
        }
    }

    public static void main(String[] args) {

        String[] strOfCards = {"A","B","C","D","E"};

        Card.shuffle(strOfCards); //this is the second line that has a problem
        for(int i = 0; i < strOfCards.length; i++) {
            System.out.println(strOfCards[i] + " ");
        }
    }
}

I have no idea how to change the flawed lines, any suggestions are welcome!我不知道如何更改有缺陷的线条,欢迎提出任何建议! *** i have tried changing the number of letters in the string but then the error changes with it ie "Index 6 out of bounds for length 6" *** 我曾尝试更改字符串中的字母数,但随后错误随之更改,即“索引 6 超出长度 6 的范围”

I'm pretty new to coding so please bear with me, I am just experimenting with ways to shuffle an array for eventual use in shuffling a deck of cards in a card game I am creating in Java.我对编码很陌生,所以请耐心等待,我只是在尝试洗牌的方法,以最终在我用 Java 创建的纸牌游戏中洗牌。 I know that index 5 is out of bounds for length 5 but what is puzzling to me is that the error doesn't ALWAYS pop up.我知道索引 5 超出了长度 5 的范围,但令我感到困惑的是错误并不总是弹出。 Sometimes I try to run my code and it works just fine, other times I get the error even if I haven't changed anything between times that I run it.有时我尝试运行我的代码并且它工作得很好,有时我会收到错误,即使我在运行它的两次之间没有更改任何内容。

public class Card{

    public static void shuffle(Object[] array) {

        int noOfCards = array.length;

        for (int i = 0; i < noOfCards; i++) {

            int s = i + (int)(Math.random() * (noOfCards - 1));

            Object temp = array[s]; //this is the first line it says has a problem
            array[s] = array[i];
            array[i] = temp;
        }
    }

    public static void main(String[] args) {

        String[] strOfCards = {"A","B","C","D","E"};

        Card.shuffle(strOfCards); //this is the second line that has a problem
        for(int i = 0; i < strOfCards.length; i++) {
            System.out.println(strOfCards[i] + " ");
        }
    }
}

I have no idea how to change the flawed lines, any suggestions are welcome!我不知道如何更改有缺陷的线条,欢迎提出任何建议! *** i have tried changing the number of letters in the string but then the error changes with it ie "Index 6 out of bounds for length 6" *** 我曾尝试更改字符串中的字母数,但随后错误随之更改,即“索引 6 超出长度 6 的范围”

I'm pretty new to coding so please bear with me, I am just experimenting with ways to shuffle an array for eventual use in shuffling a deck of cards in a card game I am creating in Java.我对编码很陌生,所以请耐心等待,我只是在尝试洗牌的方法,以最终在我用 Java 创建的纸牌游戏中洗牌。 I know that index 5 is out of bounds for length 5 but what is puzzling to me is that the error doesn't ALWAYS pop up.我知道索引 5 超出了长度 5 的范围,但令我感到困惑的是错误并不总是弹出。 Sometimes I try to run my code and it works just fine, other times I get the error even if I haven't changed anything between times that I run it.有时我尝试运行我的代码并且它工作得很好,有时我会收到错误,即使我在运行它的两次之间没有更改任何内容。

public class Card{

    public static void shuffle(Object[] array) {

        int noOfCards = array.length;

        for (int i = 0; i < noOfCards; i++) {

            int s = i + (int)(Math.random() * (noOfCards - 1));

            Object temp = array[s]; //this is the first line it says has a problem
            array[s] = array[i];
            array[i] = temp;
        }
    }

    public static void main(String[] args) {

        String[] strOfCards = {"A","B","C","D","E"};

        Card.shuffle(strOfCards); //this is the second line that has a problem
        for(int i = 0; i < strOfCards.length; i++) {
            System.out.println(strOfCards[i] + " ");
        }
    }
}

I have no idea how to change the flawed lines, any suggestions are welcome!我不知道如何更改有缺陷的线条,欢迎提出任何建议! *** i have tried changing the number of letters in the string but then the error changes with it ie "Index 6 out of bounds for length 6" *** 我曾尝试更改字符串中的字母数,但随后错误随之更改,即“索引 6 超出长度 6 的范围”

I'm pretty new to coding so please bear with me, I am just experimenting with ways to shuffle an array for eventual use in shuffling a deck of cards in a card game I am creating in Java.我对编码很陌生,所以请耐心等待,我只是在尝试洗牌的方法,以最终在我用 Java 创建的纸牌游戏中洗牌。 I know that index 5 is out of bounds for length 5 but what is puzzling to me is that the error doesn't ALWAYS pop up.我知道索引 5 超出了长度 5 的范围,但令我感到困惑的是错误并不总是弹出。 Sometimes I try to run my code and it works just fine, other times I get the error even if I haven't changed anything between times that I run it.有时我尝试运行我的代码并且它工作得很好,有时我会收到错误,即使我在运行它的两次之间没有更改任何内容。

public class Card{

    public static void shuffle(Object[] array) {

        int noOfCards = array.length;

        for (int i = 0; i < noOfCards; i++) {

            int s = i + (int)(Math.random() * (noOfCards - 1));

            Object temp = array[s]; //this is the first line it says has a problem
            array[s] = array[i];
            array[i] = temp;
        }
    }

    public static void main(String[] args) {

        String[] strOfCards = {"A","B","C","D","E"};

        Card.shuffle(strOfCards); //this is the second line that has a problem
        for(int i = 0; i < strOfCards.length; i++) {
            System.out.println(strOfCards[i] + " ");
        }
    }
}

I have no idea how to change the flawed lines, any suggestions are welcome!我不知道如何更改有缺陷的线条,欢迎提出任何建议! *** i have tried changing the number of letters in the string but then the error changes with it ie "Index 6 out of bounds for length 6" *** 我曾尝试更改字符串中的字母数,但随后错误随之更改,即“索引 6 超出长度 6 的范围”

I'm pretty new to coding so please bear with me, I am just experimenting with ways to shuffle an array for eventual use in shuffling a deck of cards in a card game I am creating in Java.我对编码很陌生,所以请耐心等待,我只是在尝试洗牌的方法,以最终在我用 Java 创建的纸牌游戏中洗牌。 I know that index 5 is out of bounds for length 5 but what is puzzling to me is that the error doesn't ALWAYS pop up.我知道索引 5 超出了长度 5 的范围,但令我感到困惑的是错误并不总是弹出。 Sometimes I try to run my code and it works just fine, other times I get the error even if I haven't changed anything between times that I run it.有时我尝试运行我的代码并且它工作得很好,有时我会收到错误,即使我在运行它的两次之间没有更改任何内容。

public class Card{

    public static void shuffle(Object[] array) {

        int noOfCards = array.length;

        for (int i = 0; i < noOfCards; i++) {

            int s = i + (int)(Math.random() * (noOfCards - 1));

            Object temp = array[s]; //this is the first line it says has a problem
            array[s] = array[i];
            array[i] = temp;
        }
    }

    public static void main(String[] args) {

        String[] strOfCards = {"A","B","C","D","E"};

        Card.shuffle(strOfCards); //this is the second line that has a problem
        for(int i = 0; i < strOfCards.length; i++) {
            System.out.println(strOfCards[i] + " ");
        }
    }
}

I have no idea how to change the flawed lines, any suggestions are welcome!我不知道如何更改有缺陷的线条,欢迎提出任何建议! *** i have tried changing the number of letters in the string but then the error changes with it ie "Index 6 out of bounds for length 6" *** 我曾尝试更改字符串中的字母数,但随后错误随之更改,即“索引 6 超出长度 6 的范围”

M getting the same issue with this code and still no idea how to solve it M 遇到与此代码相同的问题,但仍然不知道如何解决

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.io.IOException;
import java.util.List;
import java.util.ArrayList;
import javax.swing.JOptionPane;

public class Main
{
    public static void main (String[] args)
    {
        while (true)
        {
            String number = JOptionPane.showInputDialog("Please Enter \n"
            + "1 for 'Add new Employee'"
            + "\n 2 for 'Search in employee'"
            + "\n 3 for 'Exit'");
            
            int convertnumber = Integer.parseInt(number);
            
            if (convertnumber == 1)
            {
                addEmployee();
            }
            else if (convertnumber == 2)
            {
                String eId = JOptionPane.showInputDialog("Enter ID to search");
                searchEmplye(eId);
            }
            else if (convertnumber == 3)
            {
                JOptionPane.showMessageDialog(null, "Developed By: BC180401942 SHEHZAD ADEEL");
                System.exit(0);
            }
            else
            {
                JOptionPane.showMessageDialog(null, "Invalid input");
            }
        }
    }
    
    public static void searchEmplye(String eId)
    {
        try
        {
            String tokens[] = null;
            String id, name, dob, qual, expe, pays;
            FileReader fr = new FileReader("empData.txt");
            BufferedReader br = new BufferedReader(fr);
            
            String line = br.readLine();
            
            if (line == null)
            {
                JOptionPane.showMessageDialog(null, "Employee Not found");
            }
            
            while (line != null)
            {
                tokens = line.split (",");
                id = tokens[0];
                name = tokens[1];
                dob = tokens[2];
                qual = tokens[3];
                expe = tokens[4];
                pays = tokens[5];
                
                Employee emp = new Employee (id, name, dob, qual, expe, pays);
                
                ArrayList list = new ArrayList();
                list.add(emp);
                line = br.readLine();
                
                /*
                for (int i = 0; i < list.size(); i++)
                {
                    Employee p = (Employee) list.get(i);
                    if (eId.equals(p.getEmpId()))
                    {
                        JOptionPane.showMessageDialog(null, "Employee: \n"
                        + "Employee ID: " + p.getEmpId()
                        + " \n Employee Name: "  +p.getEmpName()
                        + " \n Employee DOB: " + p.getEmpDoB()
                        + " \n Qualification: " + p.getEmpQualf()
                        + " \n Experience: " + p.getEmpExp()
                        + " \n Pay Scal: " + p.getEmpPSacle()
                        );
                    }
                    */
                for (int i = 0; i < list.size(); i++)
                {
                    Employee p = (Employee) list.get(i);
                    if (eId.equals(p.getEmpId()))
                    {
                        JOptionPane.showMessageDialog( null, "Employee: \n" +
                        "EmpId: " + p.getEmpId() + "\n" +
                        "Name: " + p.getEmpName() + "\n" +
                        "DoB: " + p.getEmpDoB() + "\n" +
                        "Qualification: " + p.getEmpQualf() + "\n" +
                        "Experience: " + p.getEmpExp() + "\n" +
                        "PayScale: " + p.getEmpPScale() );
                    }   
                    else
                    {
                        JOptionPane.showMessageDialog(null, "Employee Not found");
                    }
                }
            }
            br.close();
            fr.close();
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
        }
    }
    
    public static void addEmployee()
    {
        try
        {
            ArrayList list = new ArrayList();
            String id = JOptionPane.showInputDialog("Enter Employee ID: ");
            String name = JOptionPane.showInputDialog("Enter name: ");
            String dob = JOptionPane.showInputDialog("Enter DoB (year): ");
            String qual = JOptionPane.showInputDialog("Enter Qualification: ");
            String exp = JOptionPane.showInputDialog("Enter Employee experience (in months): ");
            String pays = JOptionPane.showInputDialog("Enter Employee Pay Scale (in number): ");
            
            Employee emp = new Employee (id, name, dob, qual, exp, pays);
            list.add(emp);
            /*
            ArrayList list = new ArrayList();
            String id = JOptionPane.showInputDialog("Enter ID ");
            String name = JOptionPane.showInputDialog("Enter Name ");
            String dob = JOptionPane.showInputDialog("Enter DOB ");
            String qualification = JOptionPane.showInputDialog("Enter qualification ");
            String experience = JOptionPane.showInputDialog("Enter Employee Experience");
            String paScale = JOptionPane.showInputDialog("Enter Employee pay scale ");
            
            Employee emp = new Employee(id, name, qualification, experience, paScale);
            list.add(emp);
            */
            
            String line;
            FileWriter fw = new FileWriter("empData.txt");
            PrintWriter pw = new PrintWriter(fw);
            
            for (int i = 0; i < list.size(); i++)
            {
                emp = (Employee) list.get(i);
                //line = emp.getEmpId() + "," + emp.getEmpName() + "," + emp.getEmpDoB() + "," + emp.getEmpQualf() + "," + emp.getEmpExp() + "," + emp.getEmpPSacle();
                
                line = emp.getEmpId() + "," +
                emp.getEmpName() + "," +
                emp.getEmpDoB() + "," +
                emp.getEmpQualf() + "," +
                emp.getEmpPScale();
                
                pw.println(line);
            }
            pw.flush();
            pw.close();
            fw.close();
        }
        catch (IOException ioEx)
        {
            System.out.println(ioEx);
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使数组索引超出范围异常,但不知道为什么? - Getting array index out of bounds exception but don't know why? 我一直越界错误 - I keep Getting an out of bounds error 当我编译时,我继续收到此行的错误信息&#39;.class&#39;this.hand = Card [] hand; 我不知道如何改变它 - When i compile I keep getting this error message '.class' expected for this line this.hand = Card[] hand; I don't know what to do to change it 越界异常,我不知道如何追溯到异常在哪里 - Getting an Out of Bounds exception and I don't know how to trace back to find where the exception is 不明白为什么在填充此2d数组时为什么索引超出范围异常 - Don't understand why I am getting index out of bounds exception when filling this 2d array 为什么在尝试打印单独的星星行时,索引 0 超出长度 0 的范围会出现错误? - Why am I getting an error for index 0 out of bounds for length 0 when trying to print separate rows of stars? 获取错误:索引 3 超出长度 3 错误 | 即使它应该在数组边界内 - Getting Error: Index 3 out of bounds for length 3 Error | Even though it should be within Array Bounds 我不断越界错误如何解决此问题? - I keep getting out of bounds error how do i fix this? 索引 4 超出 4 长度的范围 - index 4 is out of bounds for 4 length 索引 (-1, 5) 超出长度 5 的范围? - Index (-1, 5) out of bounds for length 5?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM