Click here to Skip to main content
15,886,067 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
So this is a tiny assignment that my school gave.
Quite new to programming.

Java
 System.out.println("Special Teams Represent Teams  entering only 1 event");
        System.out.println(" If there are no Special Teams or Individuals Enter 0 when the amount is asked");
        // the start of special teams
        // number of special teams
        int SpecTeamNo;
        System.out.println("-----Special_Teams-----");
        System.out.println("You have " + (4 - teamNo) + " available for special teams");
        do {
            System.out.println("Enter Amount of Teams Entering only 1 EVENT");
            SpecTeamNo = scan.nextInt();
        } while (SpecTeamNo > 4 - teamNo);


        String[] SpecTeamName = new String[SpecTeamNo];
        String[] STevent = new String[1];
        int[] SpecTeamScore = new int[SpecTeamNo];
        int sTeamRank;
// condition checking for number of special teams
        //skip if 0
        if (SpecTeamNo == 0) {
        } else {
            // event for special team
            for (int i = 0; i < 1; i++) {
                System.out.println("Enter Event Name " + (i + 1) + " for the teams");
                STevent[i] = scan.next();
            }

        }
// name and rank of special teams
        for (
                int i = 0;
                i < SpecTeamNo; i++) {
            System.out.println("Enter Name of team " + (i + 1));
            SpecTeamName[i] = scan.next();
        }

            for (int a = 0; a < 1; a++) {
                for (int i=0; i < SpecTeamNo; i++) {
                System.out.println("Enter rank of the team "+SpecTeamName[i]+" on the event");
                sTeamRank = scan.nextInt();
                int stRank = 0;
                // scoring system for special team
                switch (sTeamRank) {
                    case 3:
                        stRank = 60;
                        break;
                    case 2:
                        stRank = 80;
                        break;
                    case 1:
                        stRank = 100;
                        break;
                }
                if (sTeamRank == 0 || sTeamRank >= 4) {
                    System.out.println("This team will not be awarded points");
                } else {
                    SpecTeamScore[i] += stRank;
                    System.out.println(stRank + " points is granted for this event");
                }

                if (scan.hasNextLine()) {
                    scan.nextLine();
                }
            }
            int topSTScore = 0; // this will be used to find the highest value score
            int topSteam = -1; // and this will be the index of the team
            for (int i = 0; i < SpecTeamNo; i++) {
                if (SpecTeamScore[i] > topSTScore) {
                    topSTScore = SpecTeamScore[i]; // the highest score (up to now)
                    topSteam = i; // save the team number for the one with the highest score
                }
            }
            System.out.println(SpecTeamName[topSteam] + " is the winner, with a score of " + topSTScore);
        }


This is the code. Basically , when it requests for amount of special teams , it should accept 0. if the user enters 0, the code will be skipped. and proceeds to another code.

but array index out of length error is showing. even though there are no arrays for the amount.

i am not sure what is causing it. Do you know why?

What I have tried:

I have no clue why .

but this is the error

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index -1 out of bounds for length 0
Posted
Updated 21-Aug-21 5:53am
Comments
Richard MacCutchan 21-Aug-21 12:38pm    
The rest of your code does not handle the situation where specTeamNo is zero.

1 solution

Quote:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index -1 out of bounds for length 0

If you dare to give the complete error message, it will help us to help you. Because the position of error is an indication of problem.
Quote:
for length 0

this part tells you that you try to access an empty array, which is not good. Finding which array is the next step.

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

jdb - The Java Debugger[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900